- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个关于 servlet 重定向到同一个初始页面的问题。以下是场景:假设一个用户想要购买一件商品,于是他填写了金额并提交了。表单被提交给一个 servlet,可用的数量将根据数据库中的可用数量进行检查。因此,如果订购的商品数量超过可用数量,servlet 将重定向到同一页面,但会显示一条消息,如“商品不可用”。所以我的问题是如何实现这个案例。如何使用错误消息重定向到相同的初始页面。我不想在这里使用ajax。
以下是我对它的看法:1.)如果生成错误,我是否应该设置上下文属性,然后在重定向后在初始页面中再次检查它并显示已设置的消息。
此类 Activity 的最佳做法是什么?
最佳答案
最常见和推荐的方案(用于 Java serlvets/JSP 世界中的服务器端验证)是将一些错误消息设置为请求属性(在请求范围中) 然后使用 Expression Language 在 JSP 中输出此消息(见下面的例子)。如果未设置错误消息 - 将不会显示任何内容。
但是当在请求中存储错误消息时,您应该转发请求到初始页面。重定向时不适合设置请求属性,因为如果您使用重定向,它将是一个完全新请求并且请求属性在请求之间重置。
如果您想将请求重定向到引用页面(您从中提交数据的页面),那么您可以在 session 中存储一条错误消息(在 session 范围),即设置一个 session 属性。但在这种情况下,您还需要在提交的请求正确时从 session 中删除该属性,否则只要 session 存在,就会出现错误消息。
至于context 属性,它意味着可用于整个网络应用程序(应用程序范围)和所有用户,而且它与网络一样存在应用程序生命周期,这对您的情况几乎没有用。如果您将错误消息设置为应用程序属性,它将对所有用户可见,而不仅仅是提交错误数据的用户。
好的,这是一个原始的例子。
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>
order.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>
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);
}
}
}
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"));
}
}
}
相关阅读:
关于java - Servlet 重定向到同一页面并显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14632252/
我正在尝试使用谷歌浏览器的 Trace Event Profiling Tool分析我正在运行的 Node.js 应用程序。选择点样本后,我可以在三种 View 之间进行选择: 自上而下(树) 自上而
对于一个可能是菜鸟的问题,我们深表歉意,但尽管在 SO 上研究了大量教程和其他问题,但仍找不到答案。 我想做的很简单:显示一个包含大量数据库存储字符串的 Android ListView。我所说的“很
我已经开始了一个新元素的工作,并决定给 Foundation 5 一个 bash,看看它是什么样的。在创建带有水平字段的表单时,我在文档中注意到的第一件事是它们使用大量 div 来设置样式。所以我在下
我有一个 Windows 窗体用户控件,其中包含一个使用 BeginInvoke 委托(delegate)调用从单独线程更新的第 3 方图像显示控件。 在繁重的 CPU 负载下,UI 会锁定。当我附加
我有一堆严重依赖dom元素的JS代码。我目前使用的测试解决方案依赖于 Selenium ,但 AFAIK 无法正确评估 js 错误(addScript 错误不会导致您的测试失败,而 getEval 会
我正在制作一款基于滚动 2D map /图 block 的游戏。每个图 block (存储为图 block [21][11] - 每个 map 总共 231 个图 block )最多可以包含 21 个
考虑到以下情况,我是前端初学者: 某个 HTML 页面应该包含一个沉重的图像(例如 - 动画 gif),但我不想强制客户缓慢地等待它完全下载才能享受一个漂亮的页面,而是我更愿意给他看一个轻量级图像(例
我正在设计一个小软件,其中包括: 在互联网上获取资源, 一些用户交互(资源的快速编辑), 一些处理。 我想使用许多资源(它们都列在列表中)来这样做。每个都独立于其他。由于编辑部分很累,我想让用户(可能
我想比较两个理论场景。为了问题的目的,我简化了案例。但基本上它是您典型的生产者消费者场景。 (我关注的是消费者)。 我有一个很大的Queue dataQueue我必须将其传输给多个客户端。 那么让我们
我有一个二元分类问题,标签 0 和 1(少数)存在巨大不平衡。由于测试集带有标签 1 的行太少,因此我将训练测试设置为至少 70-30 或 60-40,因此仍然有重要的观察结果。由于我没有过多地衡量准
我是一名优秀的程序员,十分优秀!