springMvc 防止重复提交
来源:互联网 发布:淘宝开店安全吗 编辑:程序博客网 时间:2024/06/07 02:17
表单的重复提交:
一、重复提交的情况:
①.在表单提交到一个Servlet中,而Servlet又通过请求转发的方式响应一个JSP页面,此时地址栏还保留着Servlet的那个路径,在相应页面点击"刷新"
②.由于网络原因在相应页面没有到达是重复点击提交表单
③.点击"返回",然后再次点击"提交"
④.重定向还会重现上面②③点描述的情况,但是重定向后地址栏路径会发生改变,故不会出现①的情况
二、不是重复提交的情况
点击"返回","刷新"原表单页面,再"提交",不属于重复提交情况
解决表单的重复提交
使用session设置令牌
产生页面时,服务器为每次产生的Form分配唯一的随机标识号,并且在form的一个隐藏字段中设置这个标识号,
同时在当前用户的Session中保存这个标识号。当提交表单时,服务器比较hidden和session中的标识号是否相同,
相同则继续,处理完后清空Session,否则服务器忽略请求。
案例
自定义实现一个拦截器
实现HandlerInterceptor接口 重写public boolean preHandle(HttpServletRequestrequest,HttpServletResponseresponse, Object handler)方法
public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {String m=request.getParameter("myToken");Object m1=request.getSession().getAttribute("myToken");//当m不为空 说明表单提交 需要判断是否是重复提交if(m!=null){//当m1不为空 session中有值 表单不是重复提交if(m1!=null){//为了防止表单参数被篡改 需要判断隐藏表单和session中的值是否相等if(m.equals(m1)){//把session中的唯一表示符清除request.getSession().removeAttribute("myToken");return true;}else{return false;}}else{return false;}}else{return true;}}
在spring.xml中 配置以下内容
<mvc:interceptors>拦截inter路径<mvc:interceptor><mvc:mapping path="/inter"/><bean class="cn.et.springmvc.lesson05.MyInteractor"></bean></mvc:interceptor>拦截pppp路径<mvc:interceptor><mvc:mapping path="/pppp"/><bean class="cn.et.springmvc.lesson05.KInteractor"></bean></mvc:interceptor></mvc:interceptors>
在WEB-INF中创建一个tags文件夹 放自定义标签 包含生成一个随机数 把随机数放入session中
<%@ tag language="java" import="java.util.*" pageEncoding="UTF-8"%><!-- tokenName值是选填项 --><%@attribute name="tokenName" required="false" %><%//产生一个随机数String ranStr=UUID.randomUUID().toString();//当tokenName值为空时使用myTokenString key=(tokenName==null?"myToken":tokenName);//把随机数放入session中session.setAttribute(key,ranStr);%><!-- 隐藏表单域 --><input type='hidden' name='<%=key %>' value='<%=ranStr %>'>
1、在表单页面引入自定义标签<%@taglib tagdir="/WEB-INF/tags" prefix="my" %>2、在form中引入<form action="${pageContext.request.contextPath}/pppp"> 扣款:<input name="m">引入自定义标签 <my:token></my:token> <input type="submit"></form>
阅读全文
0 0
- springmvc 防止重复提交
- springMvc 防止重复提交
- Springmvc防止重复提交
- SpringMVC防止重复提交
- SpringMVC 防止表单重复提交
- SpringMVC表单防止重复提交
- Springmvc表单防止重复提交
- SpringMVC token 防止表单重复提交
- SpringMVC token 防止表单重复提交
- springmvc 拦截器 防止重复提交表单
- SpringMVC 表单防止重复提交(Ajax提交方式)
- springmvc中使用token来防止表单重复提交
- SpringMVC token 防止表单重复提交,加上注释,帮助理解
- springmvc 用拦截器+token防止重复提交
- springmvc 用拦截器+token防止重复提交
- springmvc 用拦截器+token防止重复提交
- springMVC中基于token防止表单重复提交
- springmvc 用拦截器+token防止重复提交
- __devexit_p宏的功能
- 【C#】反射(Reflection)
- 智能时代 软件赋能——2017中国软件技术大会即将举行
- 写了个简单的爬虫
- 【C#】属性(Property)
- springMvc 防止重复提交
- 【C#】索引器(Indexer)
- ORACLE显示毫秒
- 女性必知的健康正确的洗澡方式
- 【C#】 委托(Delegate)
- np.random
- keras自带的数据集
- springmvc视图解析器,静态资源共享
- 集合