在客户端可以通过JavaScript脚本来控制表单提交的次数 我们可以在JavaScript脚本中设置一个变量 初始将其设为true 当提交后 将它设为false 根据该变量的值为true还是false 来决定是否提交表单
1 index.jsp
核心代码:
2 HandlerServlet.java
- package servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.sunxin.ch19.util.TokenProcessor;
- public class HandlerServlet extends HttpServlet
- {
- int count=0;
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException,IOException
- {
- resp.setContentType("text/html;charset=GBK");
- PrintWriter out=resp.getWriter();
-
- TokenProcessor processor=TokenProcessor.getInstance();
- if(processor.isTokenValid(req))
- {
- try
- {
- Thread.sleep(5000);
- }
- catch(InterruptedException e)
- {
- System.out.println(e);
- }
-
- System.out.println("submit : "+count);
- if(count%2==1)
- count=0;
- else
- count++;
- out.println("success");
- out.close();
- }
- }
不足之处:
1 当现实成功之后 用户点击刷新 将导致表单再次提交
2 当用户点击后退按钮 也会再次提交表单
阅读(1362) | 评论(0) | 转发(0) |