【问题标题】:How to get values from one jsp page into two different jsp pages after submitting the form?提交表单后如何将一个jsp页面的值获取到两个不同的jsp页面?
【发布时间】:2016-02-28 14:51:10
【问题描述】:

我有 5 个 jsp 页面,每个页面上都有表单。当我单击每个页面上的提交按钮时,表单被提交到一个 servlet(Reg) 并且值存储在一个类(bean)中。我使用 requestdispatcher 在 step2 中从 step1 获取文本字段,并在 step3 中使用 step2 值等等。

现在我想要 step4.jsp 中文本字段 dob 和 regDate(在我的 step1.jsp 中)的值。我怎么做?下面是我编写的代码示例。

step1.jsp

<div>
<form name="step1">
<input type="text" name="dob" id="dob">
<input type="text" name="regDate" id="regDate">
<input type="submit" name="step1nxt" value="submit">
</form>
</div>

Step4.jsp

<div>
<form name="step4">
<input type="text" name="dod" id="dod">
<input type="submit" name="step4nxt" value="submit">
</form>
</div>

Reg.java (Servlet)

if(step1nxt!=null){
        //call function to set values in bean
        request.getRequestDispatcher("step2.jsp").forward(request,response);
    }
    if(step2next!=null){
    //call function to set values
        request.getRequestDispatcher("step3.jsp").forward(request,response);
    }
    //similar 'if' statement for other pages
    if(step4.nxt!=null){
    //call function to set values
        request.getRequestDispatcher("step5.jsp").forward(request,response);
    }

bean.java

String dob;
    String regDate;
    //getters and setters
    //function to setvalues of dob and regDate
    //function to print the values

【问题讨论】:

标签: java html jsp servlets javabeans


【解决方案1】:

您可以使用每个表单中的隐藏字段将值传递给下一个 jsp,而不是将其存储在会话范围内。所以 step2 看起来像这样

<div>
<form name="step2">
    <input type="hidden" value='<%=request.getParameter("dob")%>' name="dob" id="dob"/>
    <input type="hidden" value='<%=request.getParameter("regDate")%>' name="regDate" id="regDate"/>
    <input type="submit" name="step1nxt" value="submit">
</form>
</div>

请注意,不推荐在 jsp 中使用 scriptlet,因此您应该考虑使用 MVC 框架,例如 Struts2 或 Spring MVC

另一种方法是将值存储在所有主要浏览器都支持的localStorage 中,并且您可以存储至少 5 Mb。

或者你也可以使用cookies

【讨论】:

  • 感谢您的建议!我需要两页的值。第二步和第四步。
  • 如果您不想将它们存储在会话中,那么您需要在每个请求中提交值。最简单的方法是将要传递的任何内容插入&lt;form&gt;,以便将它们作为参数(GET)或http标头(POST)传递到url中
  • 我还编辑了我的答案。我建议你看看 localStorage
  • 是的,我看到了编辑。 LocalStorage 似乎对我有帮助。谢谢
猜你喜欢
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多