Servlet Page code: @ each request generates a token (usually a timestamp), which is stored in the session and submitted with hidden, in the servlet, check whether the received tokens are consistent with those in the session to determine whether repeated submissions are performed. If not, generate a new token and store it in the session to overwrite the original token.
@ When the user returns or refreshes the repeated request servlet, the servlet determines whether the token is consistent. Because the requester does not generate a new token, it is inconsistent with the newly generated token of the servlet and is considered to be submitted repeatedly.
@ When you refresh the request page, that is, generate a token on the request page again, the new token overwrites the token generated by the servlet. In this case, the tokens are consistent and considered as a new request.
@ The requested jsp page code:
<Body>
<%
Long token = System. currentTimeMillis (); // the token that generates the timestamp
Session. setAttribute ("token", token );
%>
<Form action = "isRepeat" method = "post">
<Input type = "text" name = "username"/>
<Input type = "text" name = "password"/>
<Input type = "hidden" value = "<% = token %>" name = "token"/> <! -- Submit as hidden -->
<Input type = "submit" value = "submit"/>
</Form>
</Body>
@ Servlet Page 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 is generated.
Req. getSession (). setAttribute ("token", System. currentTimeMillis ());
}
Else
{
Resp. getWriter (). println ("do not repeat submit ");
}
}
The author is like a play"