标签:asc ken head writer token origin set ack res
跨域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); } }
标签:asc ken head writer token origin set ack res
原文地址:https://www.cnblogs.com/whtt/p/12269014.html