Token prevents repeated submission of forms.

Source: Internet
Author: User

Token prevents repeated submission of forms.

Three situations of repeated form submission:

1. slow server response. The user clicks the submit button multiple times.

2. Refresh the page after the submission is successful.

3. After successful submission, return to the form page and click submit again.

Package com. jalja. token; import java. io. IOException; import java. io. printWriter; import java. util. UUID; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class UserServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws Servl EtException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); String contextPath = request. getContextPath (); String requestURI = request. getRequestURI (); String path = requestURI. substring (contextPath. lengt H () + 1, requestURI. length (); String token = ""; if (path. equals ("index. do ") {token = UUID. randomUUID (). toString (); // create the token System. out. println ("token generated in FormServlet:" + token); request. getSession (). setAttribute ("token", token); // use session on the server to save the token (token) request. getRequestDispatcher ("/index. jsp "). forward (request, response); // jump to form. jsp page} if (path. equals ("token. do ") {String name = request. getParameter (" Username "); String tokenValue = request. getParameter ("tokenValue"); // obtain the Token System of the client. out. println ("Get client token:" + tokenValue); String server_token = (String) request. getSession (). getAttribute ("token"); // get the token if (tokenValue! = Null & server_token! = Null & server_token.equals (tokenValue) {System. out. println ("processing requests; get name =" + name); try {Thread. sleep (3*1000); // simulate network latency} catch (InterruptedException e) {e. printStackTrace () ;}} else {System. out. println ("not processed");} request. getSession (). removeAttribute ("token"); // the token must be removed for each playback request }}}
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html> 
<?xml version="1.0" encoding="UTF-8"?>  <web-app version="2.5"       xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">     <servlet>          <servlet-name>token</servlet-name>          <servlet-class>com.jalja.token.UserServlet</servlet-class>          <load-on-startup>1</load-on-startup>      </servlet>      <servlet-mapping>          <servlet-name>token</servlet-name>          <url-pattern>*.do</url-pattern>      </servlet-mapping>   </web-app>  

 


 

 

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.