仿struts1的防止表单重复提交  

package com.gc.struts.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 06-10-2010
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class YulanAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
   String token=this.generateToken(request);
      request.setAttribute("clientToken", token);
      request.getSession().setAttribute("token", token);
  return mapping.findForward("ok");//跳转到showExcel.jsp
 }
}

 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>showExcel.jsp Content</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
  <%
     String clintToken=request.getAttribute("clientToken")==null?"":request.getAttribute("clientToken").toString();
   %>
    <form action="showExcel.do" method="post">
    <input type="hidden" value="<%=clintToken %>" name="clientToken"/>
    <input type="submit" value="提交" />
    </form>
  </body>
</html>

 

package com.gc.struts.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.gc.struts.form.TestForm;

/**
 * MyEclipse Struts
 * Creation date: 06-10-2010
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class ShowExcelAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
     TestForm test=(TestForm)form;
     for(int i=0;i<1000000000;i++){
      
     }//这个是为了能多点几次提交
     String clientToken=test.getClientToken();
     Object sessionToken=request.getSession().getAttribute("token");
     System.out.println(sessionToken);
     if(sessionToken!=null&&!sessionToken.toString().equals(clientToken)){
      System.out.println("重复提交了");
      
     }else{
      System.out.println("提交表单");
     }
     String token=this.generateToken(request);
     request.getSession().setAttribute("token", token);
  return null;
 }
}

 

看看打印结果:

9694ec0baa72277bbeb071356b351cd4
提交表单
f55c7a7d71b2635130438a193bd30225
重复提交了
df7c73b27c0582613e7d11f79a15ae58
重复提交了
f2778990b6abbaaec84ba89e4de24dfc
重复提交了
80fb030346bfb023abb596a34eb05d5a
重复提交了
c071865f5efbe2e7aa127ac1d3ef83d3
重复提交了
9b68a90308a5ff4635c575f5a189c86f
重复提交了
62d557df874c638fbdd1172917e2b095
重复提交了
860d5ce47de84e953f69a70b13f6473f
重复提交了
8c310fedb5de55ce53a45e1a0d2aadb8
重复提交了
88b2a367f783475473928ac420d468f3
重复提交了

只提交了一次。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值