最常见和推荐的场景(对于 Java serlvets/JSP 世界中的服务器端验证)是将一些错误消息设置为请求属性(在请求范围内),然后使用表达式语言在 JSP 中输出此消息(请参阅下面的示例)。未设置错误消息时 - 不会显示任何内容。
但是,在请求中存储错误消息时,应将请求转发到初始页。在重定向时设置请求属性是不合适的,因为如果您使用重定向,它将是一个全新的请求,并且请求属性将在请求之间重置。
如果要将请求重定向到引用页面(您从中提交数据的页面),则可以在会话中存储错误消息(在会话范围内),即设置会话属性。但在这种情况下,当提交的请求正确时,您还需要从会话中删除该属性,因为否则只要会话有效,就会有错误消息可用。
至于上下文属性,它意味着可供整个Web应用程序(应用程序范围)和所有用户使用,并且只要Web应用程序存在,它就存在,这在你的情况下几乎没有用处。如果将错误消息设置为应用程序属性,则该消息对所有用户都可见,而不仅仅是提交错误数据的用户。
好的,这是一个原始示例。
网.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Test application</display-name>
<servlet>
<servlet-name>Order Servlet</servlet-name>
<servlet-class>com.example.TestOrderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Order Servlet</servlet-name>
<url-pattern>/MakeOrder.do</url-pattern>
</servlet-mapping>
</web-app>
订单.jsp
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Test page</h1>
<form action="MakeOrder.do" method="post">
<div style="color: #FF0000;">${errorMessage}</div>
<p>Enter amount: <input type="text" name="itemAmount" /></p>
<input type="submit" value="Submit Data" />
</form>
</body>
</html>
选项 No1:将错误消息设置为请求属性
package com.example;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
public class TestOrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int amount = 0;
try {
amount = Integer.parseInt(request.getParameter("itemAmount"));
} catch (NumberFormatException e) {
// do something or whatever
}
if ((amount > 0) && (amount < 100)) { // an amount is OK
request.getRequestDispatcher("/index.jsp").forward(request, response);
} else { // invalid amount
// Set some error message as a request attribute.
if (amount <= 0) {
request.setAttribute("errorMessage", "Please submit an amount of at least 1");
}
if (amount > 100){
request.setAttribute("errorMessage", "Amount of items ordered is too big. No more than 100 is currently available.");
}
// get back to order.jsp page using forward
request.getRequestDispatcher("/order.jsp").forward(request, response);
}
}
}
选项 No2:将错误消息设置为会话属性
TestOrderServlet.java
package com.example;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
public class TestOrderServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int amount = 0;
try {
amount = Integer.parseInt(request.getParameter("itemAmount"));
} catch (NumberFormatException e) {
// do something or whatever
}
if ((amount > 0) && (amount < 100)) { // an amount is OK
// If the session does not have an object bound with the specified name, the removeAttribute() method does nothing.
request.getSession().removeAttribute("errorMessage");
request.getRequestDispatcher("/index.jsp").forward(request, response);
} else { // invalid amount
// Set some error message as a Session attribute.
if (amount <= 0) {
request.getSession().setAttribute("errorMessage", "Please submit an amount of at least 1");
}
if (amount > 100){
request.getSession().setAttribute("errorMessage", "Amount of items ordered is too big. No more than 100 is currently available.");
}
// get back to the referer page using redirect
response.sendRedirect(request.getHeader("Referer"));
}
}
}
相关阅读: