Statement: This article is an article from the blog site. When you query the relevant information, you will see the same results... It should have been seen, but I did not identify the true and false, so I copied it... Very speechless... Again, I hope that you can see if there is anything wrong when publishing an article... Do not blindly copy the file to cause some minor problems...
Set a random number on the JSP page, save it to the session, and pass the random number to the servlet using a hidden field;
During the first submission, the randomly generated data transmission will be handed to the servlet. In this case, the servlet can get the value saved in the session and save it in the request object,
The two values are equal. Before the jump, the values in the session are removed. The values in the request are not removed;
When the session is submitted again, compare the value in the session with the value in the request, and the result must be different. Therefore, we can determine that this submission is a repeated submission,
Then you can use the corresponding processor system for processing. The Code is as follows (pay attention to the red part ):
JSP part:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<%
Double D = math. Random ();
String flag = double. tostring (d );
Session. setattribute ("flag", flag );
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> my JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>
<Body>
<Form action = "addservlet" method = "Post">
<Input type = "hidden" name = "flag" value = "<% = Flag %>">
<Input type = "text" name = "name"/>
<Input type = "Submit" value = "first servlet example"/>
</Form>
</Body>
</Html>
Corresponding servlet:
Package com. softeem. servlet;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Public class addservlet extends httpservlet {
/**
*
*/
Private Static final long serialversionuid = 1l;
@ Override
Protected void doget (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Dopost (req, resp );
}
@ Override
Protected void dopost (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Httpsession session = Req. getsession ();
String flag = (string) Session. getattribute ("flag ");
Printwriter PW = new printwriter (resp. getwriter ());
String F = Req. getparameter ("flag ");
If (F. Equals (FLAG )){
String name = Req. getparameter ("name ");
System. Out. println ("Call the dopost method..." + name );
Session. removeattribute ("flag ");
PW. println ("submit success ...");
PW. Flush ();
} Else {
Session. removeattribute ("flag ");
PW. println ("don't submit repeatly !!! ");
PW. Flush ();
}
}
}
Web. xml:
<Servlet>
<Servlet-Name> addservlet </servlet-Name>
<Servlet-class> com. softeem. servlet. addservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> addservlet </servlet-Name>
<URL-pattern>/addservlet </url-pattern>
</Servlet-mapping>