在JSP中使用隐藏表单持久存储数据 [英] Persisting Data Using Hidden Forms in JSP

查看:119
本文介绍了在JSP中使用隐藏表单持久存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入表单(index.jsp):

I have an input form (index.jsp):

<form action="process-mobile-number.jsp" method="post">
<table>
    <tr>
    <td>Enter a mobile number:</td>
    <td>
        <input type="number" name="telco" maxlength="4" style="width: 20%" required title="Please enter your 4-digit prefix."/>
         - 
        <input type="text" name="mobile" maxlength="7" style="width: 70%" required title="Please enter your 7-digit number."/>  
    </td>
    </tr>
    <tr><td><input type="submit" value="Submit" style="width: 50%"/></td></tr>
</table>
</form>

这是一个servlet,它可以统计使用系统的次数:

This is the servlet that keeps count on how many times the system has been used:

@WebServlet("/process-mobile-number.jsp")
...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

...

// initialize counters
int smartCtr = 0;
int globeCtr = 0;
int sunCtr = 0;

...

switch(telcoProvider) {
    case "SMART":
        smartCtr++;
        break;

    case "GLOBE":
        globeCtr++;
        break;

    case "SUN":
        sunCtr++;
        break;
}

...

// assemble counter bean
Counter counter = CounterAssembler.getInstance(smartCtr, globeCtr, sunCtr);

// create session
HttpSession session = request.getSession();

// bind objects to session
session.setAttribute("smartCtr", counter.getSmartCtr());
session.setAttribute("globeCtr", counter.getGlobeCtr());
session.setAttribute("sunCtr", counter.getSunCtr());

// redirect to results jsp
response.sendRedirect("mobile-number-result.jsp");
}

这是输出JSP:

<form action="index.jsp" method="post">
<table>
    ...
    <tr><td style="font-weight: bold">Your number of successful attempts are:</td></tr>
    <tr><td>SMART: <%=session.getAttribute("smartCtr") %></td></tr>
    <tr><td>GLOBE: <%=session.getAttribute("globeCtr") %></td>  </tr>
    <tr><td>SUN: <%=session.getAttribute("sunCtr") %></td></tr>
    <tr>
        <td>
        <input type="hidden" name="smartctr" value="<%=session.getAttribute("smartCtr") %>"/>
        <input type="hidden" name="globectr" value="<%=session.getAttribute("globeCtr") %>"/>
        <input type="hidden" name="sunctr" value="<%=session.getAttribute("sunCtr") %>"/>
        <input type="submit" value="Go Back" style="width: 18%"/>
        </td>
    </tr>
</table>
</form>



问题:
但是,当我按"Go Back"返回index.jsp并开始另一笔交易时,计数器始终重置为0.
如何使它们与隐藏的表单字段保持一致?



QUESTION:
However, when I press "Go Back" to return to index.jsp and begin another transaction, the counters are always reset to 0.
How do I make them persist with the hidden form fields?

谢谢.

推荐答案

由于每次调用dopost方法时,您都要初始化值为0的变量.相反,您可以简单地检查session属性是否存在,否则可以设置该属性

Because each time your dopost method is called, you're initializing variable with value 0. Instead simply you can check if the session attribute exist, else set the attribute.

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
  HttpSession session = req.getSession(true);
  // get counter variables
  Integer smartCtr = session.getAttribute("smart") == null? 0: session.getAttribute("smart");
  Integer globeCtr = session.getAttribute("globe") == null? 0: session.getAttribute("globe");
  Integer sunCtr = session.getAttribute("sun") == null? 0: session.getAttribute("globe");
  //your logic
  ....
  //set incremented values back
  session.setAttribute("smart", smartCtr);
  .....
}

这篇关于在JSP中使用隐藏表单持久存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆