【问题标题】:How can I send my requestdispatcher to two different jsp pages path?如何将我的 requestdispatcher 发送到两个不同的 jsp 页面路径?
【发布时间】:2014-07-11 11:01:40
【问题描述】:

我已经使用 JSP、beans 和 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 而不是 value,因为 requestDispatcher 只发送到一个路径。

【问题讨论】:

  • 所以你想将两个视图转发给客户端。这没有任何意义。拨打rd.forward(request, response);后不要拨打PrintWriter out = response.getWriter();
  • 你想要达到的目标。再解释一下。
  • 我有一个注册表单,它接受用户的输入并将其保存到数据库中。现在我想创建另一个可以更新以前的注册表单的表单,并且我希望以前的输入显示在文本字段中,以便用户可以更新
  • 但它在输入字段中显示 null 而不是以前输入中的值

标签: java jsp servlets javabeans


【解决方案1】:

你有两个前锋的做法毫无意义

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

相反,您可以将值设置为 session ,以便您可以在两个页面中访问它(在整个应用程序会话中)。

所以,

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

您可以从会话中获取值并拥有一个请求调度程序

希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多