struts1.x的TOKEN解决重复提交实例

1.新建PrepareInsertAction.java,InsertAction.java,InsertForm.java:
PrepareInsertAction.java:
[code]
public class PrepareInsertAction extends Action{

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//创建一个新的令牌
this.saveToken(request);
return new ActionForward("/insert.jsp");
}
}
[/code]
InsertAction.java:
[code]
public class InsertAction extends Action{

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
InsertForm insertForm = (InsertForm) form;
//32位的随机码 可以判断前后两次提交是否相等 即: isTokenValid(request)
System.out.println(request.getParameter("org.apache.struts.taglib.html.TOKEN"));
//判断当前用户session范围内的令牌值和请求参数中的令牌值是否相等
if(!isTokenValid(request)){//如果两个值相等,即表单重复提交
this.saveToken(request);
return new ActionForward("/insert.jsp");
}else{//当用户首次提交时返!isTokenValid()返回true
this.resetToken(request);//将用户session中的token清空
}
return null;
}

}
[/code]
InsertForm.java:
[code]
public class InsertForm extends ActionForm{

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
[/code]

2.配置struts-config.xml:
[code]
<form-beans >
<form-bean name="insertForm" type="com.and4walker.struts.form.InsertForm" />
</form-beans>
<action-mappings >
<action attribute="insertForm" input="/insert.jsp" name="insertForm"
path="/insert" scope="request" type="com.and4walker.struts.action.InsertAction" />
<action path="/prepareInsert" type="com.and4walker.struts.action.PrepareInsertAction" />
</action-mappings>
[/code]

3.建立index.jsp跟insert.jsp:
index.jsp:
[code]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<body>
先事先保存一个tokenid<br/>
<html:link action="prepareInsert">insert</html:link>
</body>
</html>
[/code]
insert.jsp:
[code]
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<body>
<html:form action="/insert">
内容:<html:text property="name"/><br/>
<html:submit value="提交"/>
</html:form>
</body>
</html>
[/code]

原理:服务器端在处理到达的请求之前,会将请求中包含的令牌值与保存在当前用户会话中的令牌值进行比较,看是否匹配。在处理完该请求后,且在答复发送给客户端之前,将会产生一个新的令牌,该令牌除传给客户端以外,也会将用户会话中保存的旧的令牌进行替换。这样如果用户回退到刚才的提交页面并再次提交的话,客户端传过来的令牌就和服务器端的令牌不一致,从而有效地防止了重复提交的发生。
第一:你需要在请求中有这个令牌值,请求中的令牌值如何保存,其实就和我们平时在页面中保存一些信息是一样的,通过隐藏字段来保存,保存的形式如: 〈input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="6aa35341f25184fd996c4c918255c3ae"〉,这个value是TokenProcessor类中的generateToken()获得的,是根据当前用户的session id和当前时间的long值来计算的。第二:在客户端提交后,我们要根据判断在请求中包含的值是否和服务器的令牌一致,因为服务器每次提交都会生成新的Token,所以,如果是重复提交,客户端的Token值和服务器端的Token值就会不一致。下面就以在数据库中插入一条数据来说明如何防止重复提交。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值