How to avoid repeated HTML Forms

Source: Internet
Author: User

I. What is repeated submission?

Imagine a ticket booking site with a very large volume of traffic. When the buyer clicks the submit button of the ticket booking, the server may be slow to process, and the buyer may fail to see the result, believing that his/her purchase request failed, if the submit button is clicked over and over again, all these requests will be processed by the server without processing, resulting in incorrect results.

Ii. prevent repeated submission

Take a look at web. XML (download the Directory and file of the entire example)

<? XML version = "1.0"?>
<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 //" 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>

Let's look at the form 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>

<%
// At the first submission, set an ID attribute in the session object and delete it after the submission is successful. Future submission checks whether the attribute value is null to determine whether the request is submitted repeatedly.
If (session. isnew ()){
Session. setattribute ("ID", session. GETID ());
}
%>

<Center>

<Form method = "get" Action = "/resubmit/controllerservlet">
<Center>
<Table border = "1" width = "50%">
<Tr>
<TD> Number of tickets: </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> stadium level: </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> ticket price: </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 12 </option>
<Option value = "$17" <% IF (resubmit. getticketprice () = "$17") out. println ("selected"); % >>17 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 50 </option>
</SELECT>
</TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center"> <input type = "Submit" value = "Submit"> </TD>
</Tr>
</Table>
</Center>

</Form>

</Body>
</Html>

The following is the 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) {// processing of the first submission
Session. removeattribute (session_id );
Getservletconfig (). getservletcontext (). getnameddispatcher ("success"). Forward (req, Res );

} Processing of repeated else {//
Getservletconfig (). getservletcontext (). getnameddispatcher ("resubmit"). Forward (req, Res );

}
} Else {

Getservletconfig (). getservletcontext (). getnameddispatcher ("form"). Forward (req, Res );

}

}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.