简体   繁体   English

如何将我的requestdispatcher发送到两个不同的jsp页面路径?

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

I have made a registration form with the use of JSP, beans and JDBC (MVC) 我已经使用JSP,Bean和JDBC(MVC)制作​​了注册表。

In my servlet, I have the following code.. 在我的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);

Basically, i want to send my requestDispatcher to two jsp pages so that I can display another form with predefined values inside the form. 基本上,我想将我的requestDispatcher发送到两个jsp页面,以便可以在表单内显示具有预定义值的另一个表单。

eg 例如

<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>

However, the problem is it displays null instead of the value as the requestDispatcher is only sent to one path. 但是,问题在于它显示的是null而不是值,因为requestDispatcher仅发送到一条路径。

Your practice of having two forwards makes no sense 你有两个前锋的做法毫无意义

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

Instead you can set the value to the session , so that you can access it in both the pages (throughout the application session). 相反,您可以将值设置为session ,以便您可以在两个页面中(整个应用程序会话)访问它。

so , 所以,

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

You can get the values from the session and have a single request dispatcher 您可以从会话中获取值,并拥有一个请求分配器

Hope this helps !! 希望这可以帮助 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM