Principle:
Servlet page Code:
1. Each request produces a token (generally timestamp), stored in the session and followed by hidden submission, in the servlet to determine whether the received token and session consistency to determine whether to repeat the submission, if not the recurrence of a Token is stored in the session to overwrite the original token.
2. When the user returns or refreshes the repeated request servlet, the servlet determines whether the token is consistent and, because the requester does not produce a new token, is inconsistent with the new token of the servlet and considers the duplicate submission.
3. When the user in the Request page refresh is again in the request page generated token, when the new token overlay servlet generated token, then token consistent, considered a new request.
JSP page code
<body>
<%
long Token=system.currenttimemillis (); Token
Session.setattribute ("token", token) producing time stamps;
%>
<form action= "isrepeat" method= "POST" >
<input type= "text" name= "username"/>
<input type= "text" name= "password"/> <input type= "hidden" value= "<%=token%>" Name= "
Token "/> <!--submitted as hidden-->
<input type=" Submit "value=" submitted "/>
</form>
</body>
Interface code:
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
throws Servletexception, IOException {
req.setcharacterencoding ("Utf-8");
Resp.setcharacterencoding ("Utf-8");
Resp.setcontenttype ("Text/html,charset=utf-8");
String Username=req.getparameter ("username");
String password=req.getparameter ("password");
Long Token=long.parselong (Req.getparameter ("token"));
Long Tokeninsession=long.parselong (Req.getsession (). getattribute ("token") + "");
if (token==tokeninsession) {
resp.getwriter (). println ("OK");
If this is the first request, a new token
req.getsession () setattribute ("token", System.currenttimemillis ())
is generated. else
{
resp.getwriter (). println ("Do not repeat submit");
}