纵有疾风起
人生不言弃

作业—>利用session防止表单重复提交

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>

  <head>

    <title>RepeateForm.html</title>

    <meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>

    <meta http-equiv=”description” content=”this is my page”>

    <meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>

    

    <!–<link rel=”stylesheet” type=”text/css” href=”./styles.css”>–>

    <script type=”text/javascript”>

    var iscommitted = false;

    function checkPost(){

      if(!iscommitted){

          document.getElementById(“sub”).disabled =turn; 

          iscimmitted = turn;

          return trun;      

      }else{

        //  alert(“不能重复提交表单!”);

          return false;      

        }

    }

    </script>

  </head>

  

  <body>

    <form action=”/BookHistory/serlevt/RepeateFormServlet” method=”poss” οnsubmit=”return checkPost()”>

    用户名:<input type=”text” name=”username” /><br>

      <input type=”submit” value=”提交” id=”sub”/>

    </form>

  

  </body>

</html>

package com.hbsi.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class FormDealServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

/* String client_token = request.getParameter(“token”);

if(client_token==null){

return false;*/

boolean b = isTokenValidate(request);

if(!b){

System.out.print(“请不要重复提交”);

return;

}

request.getSession().removeAttribute(“token”);

System.out.print(“项数据库注册用户信息。。。”);

}

    private boolean isTokenValidate(HttpServletRequest request){

    String client_token = request.getParameter(“token”);

if(client_token==null){

return false;

    

    }

String server_token=(String) request.getSession().getAttribute(“token”);

if(server_token==null){

return false;

}

if(!client_token.equals(server_token)){

return false;

}

return true;

  }

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

}

}

package com.hbsi.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ForeGenerateServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/heml;charset=UTF-8”);

PrintWriter out = response.getWriter();

//产生表单号

TokenProcessor tp = TokenProcessor.getInstance();

String token= tp.generateToken();

request.getSession().setAttribute(“token”, token); 

out.print(“<form action=’/BookHistory/servlet/FormDealServlet’ method=’post’/>”);

out.print(“<input type=’hidden’ name=’token’ value='”+token+”‘/>”);

out.print(“用户名:<input type=’text’ name=’username’/>”);

out.print(“<input type=’submit’ value=’提交’/>”);

out.print(“</form>”);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

}

}

package com.hbsi.servlet;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Random;

import sun.misc.BASE64Encoder;

public class TokenProcessor {

private TokenProcessor(){

}

private static final TokenProcessor instance=new TokenProcessor();

public static TokenProcessor getInstance(){

return instance;

}

public String generateToken(){

int i= new Random().nextInt();

String token= System.currentTimeMillis()+i+””;

try {

MessageDigest md=MessageDigest.getInstance(“md5”);

        

byte[]md5 = md.digest(token.getBytes());

        

//base64编码

BASE64Encoder encoder = new BASE64Encoder(); 

encoder.encode(md5);

//return new String(md5);

          

} catch (NoSuchAlgorithmException e) {

// TODO Auto-generated catch block

//e.printStackTrace();

throw new RuntimeException(e);

}

return null;

}

}

package com.hbsi.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class RepeateFormServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html;charset=UTF-8”);

PrintWriter out = response.getWriter();

String username = request.getParameter(“username”);

try {

Thread.sleep(5*1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

out.print(“以处理你的信息”);

System.out.println(“向数据库注册信息……”);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request,response);

}

}

转载于:https://www.cnblogs.com/zgl521/archive/2012/11/06/3057778.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/97523872

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » 作业—>利用session防止表单重复提交

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活