gpt4 book ai didi

java - JSP - 在同一表单/同一页面中获取添加结果

转载 作者:可可西里 更新时间:2023-11-01 16:33:41 26 4
gpt4 key购买 nike

我想弄清楚如何添加两个数字并使用 Java/jsp 在同一页面上获得结果(在这种情况下 - 在 jsp 的“结果”区域中。我尝试使用 session 属性,但是有我的代码仍然有问题......我不想使用 javascript 等,但只使用“纯 java”:)

你能帮忙吗:

小服务程序:

package wprowadzenie;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/AddServlet.html")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private Integer getInt(String s) {
try {
Integer a = new Integer(s);
return a;
} catch (NumberFormatException e) {
return null;
}
}

public void serviceRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

RequestDispatcher rd = request.getRequestDispatcher("/add.jsp");
rd.forward(request, response);

String no1 = request.getParameter("a");
String no2 = request.getParameter("b");
Integer one = getInt(no1);
Integer two = getInt(no2);

if (one != null && two != null) {
int result = one + two;
System.out.println(result);
HttpSession session = request.getSession();
session.setAttribute("result", result);

}

}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

System.out.println("get");
serviceRequest(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

System.out.println("post");
serviceRequest(request, response);
}

}

JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="AddServlet.html" method="get">
<table>
<tr>
<td>B</td>
<td><input type="text" name="a" /></td>
</tr>
<tr>
<td>A</td>
<td><input type="text" name="b" /></td>
</tr>
<tr>
<td>Result:</td>
<td><input type="text" name="result" disabled="disabled"/></td>
</tr>
</table>
<input type="submit" value="add"/>



</form>

</body>
</html>

最佳答案

这是修改后的代码

package wprowadzenie;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/AddServlet.html")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private Integer getInt(String s) {
try {
Integer a = new Integer(s);
return a;
} catch (NumberFormatException e) {
return null;
}
}

public void serviceRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

RequestDispatcher rd = request.getRequestDispatcher("/add.jsp");
rd.forward(request, response);

String no1 = request.getParameter("a");
String no2 = request.getParameter("b");
Integer one = getInt(no1);
Integer two = getInt(no2);

if (one != null && two != null) {
int result = one + two;
System.out.println(result);
request.setAttribute("result", result);

}

}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

System.out.println("get");
serviceRequest(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

System.out.println("post");
serviceRequest(request, response);
}

}

你的 JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="AddServlet.html" method="get">
<table>
<tr>
<td>B</td>
<td><input type="text" name="a" /></td>
</tr>
<tr>
<td>A</td>
<td><input type="text" name="b" /></td>
</tr>
<tr>
<td>Result:</td>
<c:choose>
<c:when test="${request.result ne null}">
<td><input type="text" name="result" value="${request.result}" /></td>
</c:when>
<c:otherwise>

</c:otherwise>
</c:choose>

</tr>
</table>
<input type="submit" value="add"/>



</form>

</body>
</html>

关于java - JSP - 在同一表单/同一页面中获取添加结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com