簡體   English   中英

如何將我的requestdispatcher發送到兩個不同的jsp頁面路徑?

[英]How can I send my requestdispatcher to two different jsp pages path?

我已經使用JSP,Bean和JDBC(MVC)制作​​了注冊表。

在我的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僅發送到一條路徑。

你有兩個前鋒的做法毫無意義

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

相反,您可以將值設置為session ,以便您可以在兩個頁面中(整個應用程序會話)訪問它。

所以,

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

您可以從會話中獲取值,並擁有一個請求分配器

希望這可以幫助 !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM