2014-07-11 55 views
1

我與使用JSP,豆類和JDBC(MVC)如何將我的requestdispatcher發送到兩個不同的jsp頁面路徑?

在我的servlet,我有以下的代碼登記表..

 if ("editRegister".equalsIgnoreCase(action)) { 
     StudentBean user = new StudentBean(); 

    user.setName(Name); 
    user.setStudID(ID); 
    user.setCourse(Course); 
    user.setEmail(Email); 
      db.addRecord(Name, ID, Name, Email); 
      set the result into the attribute 
      request.setAttribute("StudentBean", user); 
      RequestDispatcher rd; 
      rd = getServletContext().getRequestDispatcher("/DisplayRegistry.jsp"); 
      rd = getServletContext().getRequestDispatcher("/UpdateRegistry.jsp"); 
      rd.forward(request, response); 

基本上,我想給我的RequestDispatcher到兩個jsp頁面,以便我可以在窗體中顯示具有預定義值的另一個表單。

例如

<jsp:useBean id="StudentBean" scope="request" class="ict.bean.StudentBean"/> 


<% String email = StudentBean.getEmail() != null ? StudentBean.getEmail() : ""; %> 
<form method="get" action="updateregistry"> 
<input type="text" name="email" maxlength="10" size="15" value="<%=email%>"> 
</form> 

但是,問題是它顯示null而不是值,因爲requestDispatcher只發送到一個路徑。

+0

所以你想轉發兩個視圖到客戶端。這沒有任何意義。在調用'rd.forward(request,response);' – Braj

+0

你想實現的內容之後,不要調用'PrintWriter out = response.getWriter();'。多解釋一下。 – Braj

+0

我有一個註冊表單,它接受來自用戶的輸入並將其保存到數據庫中。現在我想要創建另一個表單,它可以更新以前的註冊表單,並且我希望先前的輸入顯示在文本字段中,以便用戶可以更新 – user3829075

回答

0

你有兩個前鋒的做法是沒有意義的

rd = getServletContext().getRequestDispatcher("/DisplayRegistry.jsp"); 
rd = getServletContext().getRequestDispatcher("/UpdateRegistry.jsp") 

相反,你可以將該值設置爲session,這樣就可以在這兩個頁面訪問它(在整個應用程序會話)。

所以,

HttpSession session=request.getSession(false); 
session.setAttribute("StudentBean", user); 

你可以從會話中值,並有一個請求調度

希望這有助於!

+0

謝謝!工作 – user3829075

+0

快樂它幫助! –

相關問題