我们有时候在点提交请求的时候可能会因为网络等问题迟迟提交提交不了,这时候我们会急躁,就会忍不住的把提交按纽连续的点,这时候入过不进行处理,就有可能造成堵塞等问题,将程序卡死.这时候我们就需要进行表单重复提交的处理.

package test.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/hello")
public class CharServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;
        protected void service(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
            String parameter = request.getParameter("username");//获取name参数
            String  pas = request.getParameter("pas");//获取password参数
        
            HttpSession session = request.getSession();//获取session
            String hidd = request.getParameter("hid");//根据name的值获取请求参数
            String attribute = (String)request.getSession().getAttribute("hid");//设置session
            if(hidd.equals(attribute)){//第一次进来后,随即将session清除
                    session.removeAttribute("hid");
                    PrintWriter writer = response.getWriter();
                    writer.write("session"+pas);    
                    writer.close();
            }
            /*
            request.getSession().setAttribute("name", parameter);
            System.out.println(parameter+"密码:"+pas);
            if("huike".equals(parameter)&&"111".equals(pas)){
                request.getRequestDispatcher("/JSP/login.jsp").forward(request, response);
            }else{
                PrintWriter writer = response.getWriter();
                writer.write("账户或密码错误");    
                writer.close();
            }    
        }
*/
}
<%@ page import="java.util.UUID" %>  <!-- 导入UUID的jar包 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>hello world!!你好</h1>
        <% 
        /* 生成一个随机的UUID作为第一次请求 */
        String take  = UUID.randomUUID().toString();
        session.setAttribute("hid", take);//直接用session点setAttbute设置
        %>
        
    <form action="/Charset/hello" method="POST">
        <input type="hidden" name = "hid" value = "${hid}" /><!-- 隐藏域  -->
        账户:<input type="text" name = "username"   placeholder ="hello world" required/><br/><br/>
        密码:<input type="password" name = "pas" /><br/><br/>
        <input type="submit" />
    </form>
</body>
</html>

 

posted on 2017-03-07 21:45  小手撬地球  阅读(186)  评论(0编辑  收藏  举报