我试图弄清楚如何添加两个nurs并使用Java/jsp在同一页面上获得结果(在本例中 - 在jsp的“结果”区域中。我尝试使用 session 属性,但有我的代码仍然有问题... 我不想使用 javascript 等,但只想使用“纯 java”:)
你能帮忙吗:
Servlet:
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/