英文:
How to share result data between servlet and display on jsp?
问题
我曾在该网站上查看过类似的问题,但仍对此存在疑虑。
我有一个基本的计算,通过一个名为 add servlet
的servlet调用进行,希望能够获取用户输入,并通过另一个名为 view total servlet
的servlet在同一个JSP页面上显示总计(以表格形式呈现)。
例如,在 addServlet
的 doGet
方法中:
int i = Integer.parseInt(request.getParameter("add"));
int subTotal = 10 + i;
System.out.println("subtotal is (servlet) = " + subTotal);
// 会话管理以共享数据
request.setAttribute("subTotal", subTotal);
RequestDispatcher rd = request.getRequestDispatcher("view_total");
rd.forward(request, response);
view total servlet
的 doGet
方法如下:
// 从 add servlet 获取输入
int subTotal = (int) request.getAttribute("subTotal");
subTotal = 0 * subTotal;
String viewTotal = "frontend/view_total.jsp";
RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
rd.forward(request, response);
JSP 页面的内容如下:
<tr>
<td>1</td>
<td>$10</td>
<td><input type="text" name="add" /></td> <!-- 获取用户输入 -->
<td>${ subTotal }</td> <!-- 如何在此处显示总计值 -->
</tr>
英文:
I've gone through similar questions on this site and still in doubt with it.
I have a basic calculation on one servlet call add servlet
and want to get user input to show the total on the same JSP page via another servlet called view total servlet
(a table view).
E.g. addServlet
on doGet
int i = Integer.parseInt(request.getParameter("add"));
int subTotal = 10 + i;
System.out.println("subtotal is (servlet) = " + subTotal);
// session mgmt to share data
request.setAttribute("subTotal", subTotal);
RequestDispatcher rd = request.getRequestDispatcher("view_total");
rd.forward(request, response);
The `view total servlet` on doGet
// get input from add servlet
int subTotal = (int) request.getAttribute("subTotal");
subTotal = 0 * subTotal;
String viewTotal = "frontend/view_total.jsp";
RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
rd.forward(request, response);
The JSP
<tr>
<td>1</td>
<td>$10</td>
<td><input type="text" name="add" /></td> `// get user input`
<td> ${ subTotal } /td> `// how to show total value here`
</tr>
答案1
得分: 1
你可以检查session
中是否有任何值,如果有的话,获取该值并将新值与旧值相加,然后再次设置新的subTotal
;否则,只需将i
中的值添加到您的会话中。因此,您的代码将类似于下面的样子:
String viewTotal = "frontend/view_total.jsp";
int i = Integer.parseInt(request.getParameter("add"));
int subTotal, newvalue;
// 检查会话中是否有值
if (request.getAttribute("subTotal") != null) {
// 获取该值
subTotal = (int) request.getAttribute("subTotal");
// 将新值添加到总数中,根据您的需求进行更改
newvalue = subTotal + i;
// 用新的总数再次设置属性
request.setAttribute("subTotal", newvalue);
}
else {
// 设置用户发送的值
request.setAttribute("subTotal", i + 10);
}
RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
rd.forward(request, response);
英文:
You can check if there is any value in session
if yes then get that value and add new with old one and then set new subTotal
again else just add the value which is there in i
in your session .So your code will somewhat look like below :
String viewTotal = "frontend/view_total.jsp";
int i = Integer.parseInt(request.getParameter("add"));
int subTotal,
newvalue;
//check if there is value in session
if (request.getAttribute("subTotal") != null) {
//get that
subTotal = (int) request.getAttribute("subTotal");
//add new value to subtotal not sure why you add 10..make change according to your requirements
newvalue = subTotal + i;
//set it again with new total
request.setAttribute("subTotal", newvalue);
}
else {
//set the value which user sends
request.setAttribute("subTotal", i + 10);
}
RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
rd.forward(request, response);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论