这是More Java Pitfalls中文版上的一个例子。
一、何谓重复提交
设想一个访问量非常大的订票站点,当购票者点击订票的提交按钮后,服务器的处理速度可能会迟缓,购票者迟迟见不到结果,以为自己的购买请求失败,于是一遍又一遍地点击Submit按钮,如果不加处理,这些请求都会被服务器处理,从而导致错误的结果。
二、防止重复提交
先看一下web.xml(整个例子的目录和文件请下载)
再看表单ticketForm.jsp
<%@ page contentType="text/html; charset=GBK" %>
<TITLE> Resubmit Demonstration </TITLE>
<jsp:useBean id="resubmit" class="org.javapitfalls.item36.formBean" scope="request">
<jsp:setProperty name="resubmit" property="*"/>
</jsp:useBean>
<%
//第一次提交时,在session对象中设置一个id属性,提交成功后将删除。以后的提交将检测这个属性值是否为null来判断是重复提交。
if(session.isNew()) {
session.setAttribute("id",session.getId());
}
%>
<center><H1> 奥齐音乐会在线订票 </H1></center>
<form method="get" action="/resubmit/ControllerServlet">
<center>
<table border="1" width="50%">
<tr>
<td>票的数目:</td>
<td>
<select name="numTickets">
<option value="Please enter a Ticket #">Please enter a Ticket #</option>
<option value="1 Ticket" <% if (resubmit.getNumTickets() == "1 Ticket") out.println("SELECTED"); %>>1 Ticket</option>
<option value="2 Tickets" <% if (resubmit.getNumTickets() == "2 Tickets") out.println("SELECTED"); %>>2 Tickets</option>
<option value="3 Tickets" <% if (resubmit.getNumTickets() == "3 Tickets") out.println("SELECTED"); %>>3 Tickets</option>
<option value="4 Tickets" <% if (resubmit.getNumTickets() == "4 Tickets") out.println("SELECTED"); %>>4 Tickets</option>
<option value="5 Tickets" <% if (resubmit.getNumTickets() == "5 Tickets") out.println("SELECTED"); %>>5 Tickets</option>
<option value="6 Tickets" <% if (resubmit.getNumTickets() == "6 Tickets") out.println("SELECTED"); %>>6 Tickets</option>
<option value="7 Tickets" <% if (resubmit.getNumTickets() == "7 Tickets") out.println("SELECTED"); %>>7 Tickets</option>
<option value="8 Tickets" <% if (resubmit.getNumTickets() == "8 Tickets") out.println("SELECTED"); %>>8 Tickets</option>
<option value="9 Tickets" <% if (resubmit.getNumTickets() == "9 Tickets") out.println("SELECTED"); %>>9 Tickets</option>
</select>
</td>
</tr>
<tr>
<td>体育场的等级:</td>
<td>
<select name="stadiumTier">
<option value="Please enter a Stadium Tier">Please enter a Stadium Tier</option>
<option value="Tier A" <% if (resubmit.getStadiumTier() == "Tier A") out.println("SELECTED"); %>>Tier A</option>
<option value="Tier B" <% if (resubmit.getStadiumTier() == "Tier B") out.println("SELECTED"); %>>Tier B</option>
<option value="Tier C" <% if (resubmit.getStadiumTier() == "Tier C") out.println("SELECTED"); %>>Tier C</option>
<option value="Tier D" <% if (resubmit.getStadiumTier() == "Tier D") out.println("SELECTED"); %>>Tier D</option>
<option value="Tier E" <% if (resubmit.getStadiumTier() == "Tier E") out.println("SELECTED"); %>>Tier E</option>
</select>
</td>
</tr>
<tr>
<td>票的价格:</td>
<td>
<select name="ticketPrice">
<option value="Please enter a Ticket Price">Please enter a Ticket Price</option>
<option value="$12" <% if (resubmit.getTicketPrice() == "$12") out.println("SELECTED"); %>>$12</option>
<option value="$17" <% if (resubmit.getTicketPrice() == "$17") out.println("SELECTED"); %>>$17</option>
<option value="$25" <% if (resubmit.getTicketPrice() == "$25") out.println("SELECTED"); %>>$25</option>
<option value="$35" <% if (resubmit.getTicketPrice() == "$35") out.println("SELECTED"); %>>$35</option>
<option value="$50" <% if (resubmit.getTicketPrice() == "$50") out.println("SELECTED"); %>>$50</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><INPUT TYPE="submit" VALUE="Submit"></td>
</tr>
</table>
</center>
</form>
</BODY>
</HTML>
下面是处理表单的ControllerServlet.java
package org.javapitfalls.item36;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.javapitfalls.item36.*;
public class ControllerServlet extends HttpServlet {
private static String SESSION_ID;
public void destroy() {}
public void init() throws ServletException {
SESSION_ID = getInitParameter("id");
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
process(req, res);
}
protected void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession(false);
String numTickets = req.getParameter("numTickets");
String stadiumTier = req.getParameter("stadiumTier");
String ticketPrice = req.getParameter("ticketPrice");
if(session == null) {
if( (numTickets == null) || (stadiumTier == null) || (ticketPrice == null) ) {
getServletConfig().getServletContext().getNamedDispatcher("form").forward(req, res);
} else {
throw new ServletException("[form] Page Not Found");
}
} else {
if ( (!numTickets.equals("Please enter a Ticket #")) &&
(!stadiumTier.equals("Please enter a Stadium Tier")) && (!ticketPrice.equals("Please enter a Ticket Price")) ) {
String sessionValidatorID = (String)session.getAttribute(SESSION_ID);
if(sessionValidatorID != null ) { //第一次提交的处理
session.removeAttribute(SESSION_ID);
getServletConfig().getServletContext().getNamedDispatcher("success").forward(req, res);
} else { //重复提交的处理
getServletConfig().getServletContext().getNamedDispatcher("resubmit").forward(req, res);
}
} else {
getServletConfig().getServletContext().getNamedDispatcher("form").forward(req, res);
}
}
}
}
一、何谓重复提交
设想一个访问量非常大的订票站点,当购票者点击订票的提交按钮后,服务器的处理速度可能会迟缓,购票者迟迟见不到结果,以为自己的购买请求失败,于是一遍又一遍地点击Submit按钮,如果不加处理,这些请求都会被服务器处理,从而导致错误的结果。
二、防止重复提交
先看一下web.xml(整个例子的目录和文件请下载)
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<display-name>ControllerServlet</display-name>
<description>ControllerServlet</description>
<servlet-class>org.javapitfalls.item36.ControllerServlet</servlet-class>
<init-param>
<param-name>id</param-name>
<param-value>id</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>form</servlet-name>
<jsp-file>/ticketForm.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>success</servlet-name>
<jsp-file>/success.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>resubmit</servlet-name>
<jsp-file>/resubmitError.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/ControllerServlet</url-pattern>
</servlet-mapping>
</web-app>
再看表单ticketForm.jsp
<%@ page contentType="text/html; charset=GBK" %>
<TITLE> Resubmit Demonstration </TITLE>
<jsp:useBean id="resubmit" class="org.javapitfalls.item36.formBean" scope="request">
<jsp:setProperty name="resubmit" property="*"/>
</jsp:useBean>
<%
//第一次提交时,在session对象中设置一个id属性,提交成功后将删除。以后的提交将检测这个属性值是否为null来判断是重复提交。
if(session.isNew()) {
session.setAttribute("id",session.getId());
}
%>
<center><H1> 奥齐音乐会在线订票 </H1></center>
<form method="get" action="/resubmit/ControllerServlet">
<center>
<table border="1" width="50%">
<tr>
<td>票的数目:</td>
<td>
<select name="numTickets">
<option value="Please enter a Ticket #">Please enter a Ticket #</option>
<option value="1 Ticket" <% if (resubmit.getNumTickets() == "1 Ticket") out.println("SELECTED"); %>>1 Ticket</option>
<option value="2 Tickets" <% if (resubmit.getNumTickets() == "2 Tickets") out.println("SELECTED"); %>>2 Tickets</option>
<option value="3 Tickets" <% if (resubmit.getNumTickets() == "3 Tickets") out.println("SELECTED"); %>>3 Tickets</option>
<option value="4 Tickets" <% if (resubmit.getNumTickets() == "4 Tickets") out.println("SELECTED"); %>>4 Tickets</option>
<option value="5 Tickets" <% if (resubmit.getNumTickets() == "5 Tickets") out.println("SELECTED"); %>>5 Tickets</option>
<option value="6 Tickets" <% if (resubmit.getNumTickets() == "6 Tickets") out.println("SELECTED"); %>>6 Tickets</option>
<option value="7 Tickets" <% if (resubmit.getNumTickets() == "7 Tickets") out.println("SELECTED"); %>>7 Tickets</option>
<option value="8 Tickets" <% if (resubmit.getNumTickets() == "8 Tickets") out.println("SELECTED"); %>>8 Tickets</option>
<option value="9 Tickets" <% if (resubmit.getNumTickets() == "9 Tickets") out.println("SELECTED"); %>>9 Tickets</option>
</select>
</td>
</tr>
<tr>
<td>体育场的等级:</td>
<td>
<select name="stadiumTier">
<option value="Please enter a Stadium Tier">Please enter a Stadium Tier</option>
<option value="Tier A" <% if (resubmit.getStadiumTier() == "Tier A") out.println("SELECTED"); %>>Tier A</option>
<option value="Tier B" <% if (resubmit.getStadiumTier() == "Tier B") out.println("SELECTED"); %>>Tier B</option>
<option value="Tier C" <% if (resubmit.getStadiumTier() == "Tier C") out.println("SELECTED"); %>>Tier C</option>
<option value="Tier D" <% if (resubmit.getStadiumTier() == "Tier D") out.println("SELECTED"); %>>Tier D</option>
<option value="Tier E" <% if (resubmit.getStadiumTier() == "Tier E") out.println("SELECTED"); %>>Tier E</option>
</select>
</td>
</tr>
<tr>
<td>票的价格:</td>
<td>
<select name="ticketPrice">
<option value="Please enter a Ticket Price">Please enter a Ticket Price</option>
<option value="$12" <% if (resubmit.getTicketPrice() == "$12") out.println("SELECTED"); %>>$12</option>
<option value="$17" <% if (resubmit.getTicketPrice() == "$17") out.println("SELECTED"); %>>$17</option>
<option value="$25" <% if (resubmit.getTicketPrice() == "$25") out.println("SELECTED"); %>>$25</option>
<option value="$35" <% if (resubmit.getTicketPrice() == "$35") out.println("SELECTED"); %>>$35</option>
<option value="$50" <% if (resubmit.getTicketPrice() == "$50") out.println("SELECTED"); %>>$50</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><INPUT TYPE="submit" VALUE="Submit"></td>
</tr>
</table>
</center>
</form>
</BODY>
</HTML>
下面是处理表单的ControllerServlet.java
package org.javapitfalls.item36;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.javapitfalls.item36.*;
public class ControllerServlet extends HttpServlet {
private static String SESSION_ID;
public void destroy() {}
public void init() throws ServletException {
SESSION_ID = getInitParameter("id");
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
process(req, res);
}
protected void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession(false);
String numTickets = req.getParameter("numTickets");
String stadiumTier = req.getParameter("stadiumTier");
String ticketPrice = req.getParameter("ticketPrice");
if(session == null) {
if( (numTickets == null) || (stadiumTier == null) || (ticketPrice == null) ) {
getServletConfig().getServletContext().getNamedDispatcher("form").forward(req, res);
} else {
throw new ServletException("[form] Page Not Found");
}
} else {
if ( (!numTickets.equals("Please enter a Ticket #")) &&
(!stadiumTier.equals("Please enter a Stadium Tier")) && (!ticketPrice.equals("Please enter a Ticket Price")) ) {
String sessionValidatorID = (String)session.getAttribute(SESSION_ID);
if(sessionValidatorID != null ) { //第一次提交的处理
session.removeAttribute(SESSION_ID);
getServletConfig().getServletContext().getNamedDispatcher("success").forward(req, res);
} else { //重复提交的处理
getServletConfig().getServletContext().getNamedDispatcher("resubmit").forward(req, res);
}
} else {
getServletConfig().getServletContext().getNamedDispatcher("form").forward(req, res);
}
}
}
}