使用sessionID和时间戳作为标识,关键代码如下:

复制代码 代码如下:
public class SswpdjAction extends BaseAction{

      public String execute(){
        /**业务代码**/
        ................
        //设置标识
        this.setSessionToken();
        //转到添加页面
        return "toAdd";
      }
  
      public String reSave(){
        if(this.token != null && this.token.equals(this.getSessionToken())){
          /**设置新标识**/
          this.setSessionToken();
      
          /**业务代码**/
          ..............
          return "toAdd";
        }else{
          printWriter out = null;
          try{
            httpServletResponse.setContentType("text/html;charset=UTF-8");
            out = httpServletResponse.getWriter();
            out.println("<script>alert('刷新提交表单!');</script>");
            out.flush();
        
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(out != null){
                    out.close();
                }
            }
        }
        return null;
  }
}

public class BaseAction extends ActionSupport{

    /**jsp页面标识**/
    protected String token;

    public String getToken(){
        return token;
    }

    public void setToken(String token){
        this.token = token;
    }

    public String getSessionToken(){
        if(null != httpSession.getAttribute("Token")){
            return httpSession.getAttribute("Token");
        }else{
            return null;
        }
    }

    /**标识生成**/
    public void setSessionToken(){
        String flag = useMd5(httpSession.getId() + System.currentTimeMillis());
        httpSession.setAttribute("Token", flag);
        httpServletRequest.setAttribute("SessionToken", flag);
    }

    /**MD5加密**/
    private String useMd5(String str){
        byte[] bs = str.getBytes();
        String result = null;
        try{
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(bs);
            result = md5.digest().toString();
        }catch(NoSuchAlgorithmException e){
            e.printStackTrace();
        }finally{

        }
        return result;
    }
}

JSP页面设置标识隐藏域:

复制代码 代码如下:
<form>
  <input type="hidden" name="token" value="${SessionToken}" />
</form>

标签:
JSP,页面,重复提交

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com