当前位置:编程学习 > 网站相关 >>

利用session防止表单重复提交

l表单页面由servlet程序生成,servlet为每次产生的表单页面分配一个唯一的随机标识号,并在FORM表单的一个隐藏字段中设置这个标识号,同时在当前用户的Session域中保存这个标识号。
l当用户提交FORM表单时,负责处理表单提交的serlvet得到表单提交的标识号,并与session中存储的标识号比较,如果相同则处理表单提交,处理完后清除当前用户的Session域中存储的标识号。
l在下列情况下,服务器程序将拒绝用户提交的表单请求:
ü存储Session域中的表单标识号与表单提交的标识号不同
ü当前用户的Session中不存在表单标识号
ü用户提交的表单数据中没有标识号字段
l
l编写工具类生成表单标识号:TokenProcessor
l


package com.csdn.session;


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());


BASE64Encoder encode = new BASE64Encoder();
return encode.encode(md5);


} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}


}


}

 

 

 

 

 


package com.csdn.session;


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 FormGenerateServlet extends HttpServlet {


/**
*
*/
private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();


TokenProcessor tp = TokenProcessor.getInstance();
String token = tp.generateToken();
request.getSession().setAttribute("token", token);


out.print("<form action='FormDealServlet' method='post' >");
out.print("<input type='hidden' name='token' value='" + token + "'/>");
out.print("用户名:<input type='text' name='usename' />");
out.print("<input type='submit' value='提交' />");
out.print("</form>");


}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


doGet(request, response);
}


}

 

 

 

 

 

 

 


package com.csdn.session;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;


public class FormDealServlet extends HttpServlet {

 


/**
*
*/
private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");


boolean b = isFlag(request);
if(!b){
System.out.println("请不要重复提交表单");
return;
}
request.getSession().removeAttribute("token");
System.out.println("更新数据库的用户的信息");

 


}
public boolean isFlag(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 {


doGet(request, response);
}


}

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,