Jsp prevents secondary submission (repeated submission)

Source: Internet
Author: User

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"
 

Related Article

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.