跨域,防止表单重复提交

Posted whtt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨域,防止表单重复提交相关的知识,希望对你有一定的参考价值。

跨域1

<body>
    <input type="text" name="uname" id="uname"/>
    <input type="button" value="提交" id="button"/>
</body>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
    <script>
        $(function(){
            /*$("#button").click(function(){
                var uname=$("#uname").val();
                $.ajax({
                    url:"http://www.a.cn:8080/A",
                    type:"POST",
                    data:{"uname":uname},
                    success:function(result){
                        alert(result)
                    },
                    srror:function(){
                        alert(‘错‘);
                    }
                })
            })*/
            $("#button").click(function(){
                var uname=$("#uname").val();
                $.ajax({
                    url:"http://www.a.cn:8080/A?uname="+uname,
                    type:"GET",
                    jsonp:"jsonpCallBack",
                    dataType:"JSONP",
                    success:function(result){
                        alert(result)
                    },
                    srror:function(){
                        alert(‘错‘);
                    }
                })
            })
            /*$("#button").click(function(){
                var uname=$("#uname").val();
                $.ajax({
                    url:"BServlet?uname="+uname,
                    type:"GET",
                    success:function(result){
                        alert(result)
                    },
                    srror:function(){
                        alert(‘错‘);
                    }
                })
            })*/
        })
    </script>
@WebServlet("/A")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收内容
        String uname = req.getParameter("uname");
        System.out.println("接收到的:"+uname);


        //接受ajax数据
        String back = req.getParameter("jsonpCallBack");
        System.out.println("jsonpCallBack"+back);
        String meiChuanKuZi = (String)JSON.toJSONString("MeiChuanKuZi");
        //响应结果
        resp.getWriter().write(back+"("+meiChuanKuZi+")");
            /*resp.getWriter().write("meiChuanKuZi");*/
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*//接收内容
        String uname = req.getParameter("uname");
        System.out.println("接收到的:"+uname);
*//*

        //设置请求头
        resp.setHeader("Access-Control-Allow-Origin","*");
*//*

        //接受ajax数据
        String back = req.getParameter("jsonpCallBack");
        System.out.println("jsonpCallBack"+back);

        String meiChuanKuZi = (String)JSON.toJSONString("MeiChuanKuZi");


        //响应结果
        resp.getWriter().write(back+"("+meiChuanKuZi+")");*/
    }
}

表单重复提交

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        var flag=false;
        function fromSubmit(){
            if(!flag){
                flag=true;
                return true;
            }else{
                return false;
            }
        }

        $(function(){
            $.ajax({
                url:"ToKenServlet",
                type:"POST",
                success:function(token){
                    $("#hiddenToken").val(token);
                }
            })
        })
    </script>
</head>
<body>
    <form action="FromServlet" onsubmit="return fromSubmit()" method="post">
        <input type="text" name="uname" id="uname"/>
        <input type="hidden" id="hiddenToken" name="formToken"/>
        <input type="submit" value="提交"/>
    </form>
</body>
</html>
@WebServlet("/FromServlet")
public class FromServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        System.out.println("进了FromServlet");
        //提取
        String formToken = req.getParameter("formToken");
        //获取会话中的
        String sessionToken = (String)req.getSession().getAttribute("sessionToken");
        //判断
        if (!formToken.equals(sessionToken)){
            resp.getWriter().write("您已重复提交!!!");
            return;
        }

        //接收内容
        String uname = req.getParameter("uname");
        System.out.println("接收到的:"+uname);
        req.getSession().removeAttribute("sessionToken");

        resp.getWriter().write("meiChuanKuZi");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
@WebServlet("/ToKenServlet")
public class ToKenServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进了ToKenServlet");
        //生成令牌
        String token= UUID.randomUUID().toString();
        //保存
        req.getSession().setAttribute("sessionToken",token);
        //响应
        resp.getWriter().write(token);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

以上是关于跨域,防止表单重复提交的主要内容,如果未能解决你的问题,请参考以下文章

JSONP解决跨域问题,防止表单重复提交,防止XSS攻击

HTTP(“跨域问题”和“防止表单重复提交”)

php通过记录IP来防止表单重复提交方法分析

javaweb防止表单重复提交

防止表单重复提交token

防止在jQuery中重复提交表单