今天讲的主要是:利用Session防止表单重复提交
首先你可以创建一个html表单;用script
<scripttype="text/javascript">
variscommited=false;
functioncheckPost(){
if(!iscommited){
iscommited=true;
returntrue;
}else{
returnfalse;
}
}
</script>
</head>
<body>
<formaction="/book/FormServlet"method="post"onsubmit="returncheckPost()">
用户名:<inputtype="text"name="name"/>
<br>
<inputtype="submit"value="提交"/>
</form>
</body>
创建一个servlet类,如果注册成功就跳转,
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
Stringname=request.getParameter("name");
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
out.print("注册成功");
System.out.println("正在向服务器发送。。。。。");
}
但是这种情况有缺陷,用户单击”刷新”,或单击”后退”再次提交表单,将导致表单重复提交;
为了防止表单重复提交,使用随机标识符
表单页面由servlet程序生成,servlet为每次产生的表单页面分配一个唯一的随机标识号,并在FORM表单的一个隐藏字段中设置这个标识号,同时在当前用户的Session域中保存这个标识号。
当用户提交FORM表单时,负责处理表单提交的serlvet得到表单提交的标识号,并与session中存储的标识号比较,如果相同则处理表单提交,处理完后清除当前用户的Session域中存储的标识号。
在下列情况下,服务器程序将拒绝用户提交的表单请求:
存储Session域中的表单标识号与表单提交的标识号不同
当前用户的Session中不存在表单标识号
用户提交的表单数据中没有标识号字段
首先创建一个隐藏java类;获取随机标识符
publicclassTokenDemo{
privateTokenDemo(){
}
privatestaticfinalTokenDemotoken=newTokenDemo();
publicstaticTokenDemogetToken(){
returntoken;
}
publicStringgenerateToken(){
inti=newRandom().nextInt();
Stringvalue= System.currentTimeMillis()+i+"";
try{
MessageDigestmd=MessageDigest.getInstance("md5");
byte[]bt=md.digest(value.getBytes());
BASE64Encoderbase=newBASE64Encoder();
returnbase.encode(bt);
}catch(NoSuchAlgorithmExceptione){
thrownewRuntimeException();
}
}
}
//创建一个表单号:
publicclassFormGenerateServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
TokenDemotd=TokenDemo.getToken();
Stringtoken=td.generateToken();
request.getSession().setAttribute("token",token);
out.print("<formaction='/book/FormDealServlet'method='post'>");
out.print("<inputtype='hidden'name='token'value='"+token+"'/>");
out.print("用户名:<inputtype='text'name='name'/><br/>");
out.print("<inputtype='submit'value='提交'/>");
out.print("</form>");
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
}
//判断标识符是否相同
publicclassFormDealServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriterout=response.getWriter();
booleanb=isTokenValidate(request);
if(!b){
System.out.println("请不要重复");
return;
}
request.getSession().removeAttribute("token");
System.out.println("向服务器注册信息");
}
privatebooleanisTokenValidate(HttpServletRequestrequest){
Stringname=request.getParameter("token");
if(name==null){
returnfalse;
}
Stringserver_token=(String)request.getSession().getAttribute("token");
if(server_token==null){
returnfalse;
}
if(!name.equals(server_token)){
returnfalse;
}
returntrue;
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
}