如何在Servlet之间共享结果数据并在JSP上进行显示?

huangapple go评论151阅读模式
英文:

How to share result data between servlet and display on jsp?

问题

我曾在该网站上查看过类似的问题,但仍对此存在疑虑。

我有一个基本的计算,通过一个名为 add servlet 的servlet调用进行,希望能够获取用户输入,并通过另一个名为 view total servlet 的servlet在同一个JSP页面上显示总计(以表格形式呈现)。

例如,在 addServletdoGet 方法中:

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 servletdoGet 方法如下:

// 从 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(&quot;add&quot;));

		int subTotal = 10 + i;

		System.out.println(&quot;subtotal is (servlet) = &quot; + subTotal);

		// session mgmt to share data
		request.setAttribute(&quot;subTotal&quot;, subTotal);

		RequestDispatcher rd = request.getRequestDispatcher(&quot;view_total&quot;);
		rd.forward(request, response);


The `view total servlet` on doGet

// get input from add servlet
		int subTotal = (int) request.getAttribute(&quot;subTotal&quot;);
		subTotal = 0 * subTotal;

		String viewTotal = &quot;frontend/view_total.jsp&quot;;
		RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
		rd.forward(request, response);

The JSP

			&lt;tr&gt;
				&lt;td&gt;1&lt;/td&gt;
				&lt;td&gt;$10&lt;/td&gt;
				&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;add&quot; /&gt;&lt;/td&gt; `// get user input`
				&lt;td&gt; ${ subTotal } /td&gt; `// how to show total value here`
			&lt;/tr&gt;

答案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 = &quot;frontend/view_total.jsp&quot;;
int i = Integer.parseInt(request.getParameter(&quot;add&quot;));
int subTotal,
newvalue;
//check if there is value in session
if (request.getAttribute(&quot;subTotal&quot;) != null) {
  //get that
  subTotal = (int) request.getAttribute(&quot;subTotal&quot;);
  //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(&quot;subTotal&quot;, newvalue);
}
else {
  //set the value which user sends
  request.setAttribute(&quot;subTotal&quot;, i + 10);
}

RequestDispatcher rd = request.getRequestDispatcher(viewTotal);
rd.forward(request, response);

huangapple
  • 本文由 发表于 2020年10月26日 10:45:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64530917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定