码迷,mamicode.com
首页 > Web开发 > 详细

使用ajax来进行登录验证

时间:2020-06-03 21:58:21      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:button   value   ons   oca   post   long   except   orm   ram   

servlet:

 1 @WebServlet("/login.do")
 2 public class AjaxLoginServlet extends HttpServlet {
 3     private static final long serialVersionUID = 1L;
 4 
 5     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 6 
 7         ResultMsg rm = new ResultMsg();
 8         PrintWriter out = response.getWriter();
 9         Gson gson = new Gson();
10 
11         String name = request.getParameter("userName");
12         String pass = request.getParameter("userPass");
13     
14         //非空校验
15         if(isempty(name)||isempty(pass)){
16             rm.setMsg("user pass is null");
17             rm.setResult("0002");
18             
19             out.println(gson.toJson(rm));
20             return;
21         }
22         
23         //去数据库确认一下用户是不是存在,并且登录成功
24         if("dabu".equals(name)&&"123".equals(pass)){
25             rm.setResult("0000");
26         }else {
27             rm.setMsg("user pass is wrong");
28             rm.setResult("0001");
29         }
30         
31         out.println(gson.toJson(rm));
32         
33         //form表单 a标签;  转发或者重定向
34         //ajax的请求; out.println(json对象);
35     }
36 
37     //判断一个字符串是不是为空
38     private boolean isempty(String str){
39         return null==str||"".equals(str.trim());
40     }
41     
42     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43         // TODO Auto-generated method stub
44         doGet(request, response);
45     }

jsp里面的是重点:

 1 <body>
 2 
 3     <!-- 提交的方式; get  post -->
 4 
 5 
 6     <form action="login.do" method="post">
 7 
 8         <!-- name : 对应我们servlet去获取前台文本框的值的 key -->
 9         用户名:<input id="nm" name="userName" type="text" value="${userName}" /> 
10         密码:<input name="userPass" type="password" />
11         <button type="button">登录</button>
12         <label id="lab1"></label>
13 
14     </form>
15 </body>
16 <script type="text/javascript">
17 
18     $(function(){
19         $("button").click(function(){
20             
21             //使用jQuery发送一个ajax请求
22             var name=$("#nm").val();
23             var pass=$("input[name=‘userPass‘]").val();
24             
25             
26             
27             /* 
28             使用get请求
29             $.get("login.love?userName="+name+"&userPass="+pass,function(obj){ //obj:后台返回给我的哪个json字符串
30             
31             var json = JSON.parse(obj);
32         
33             if(json.result == "0000"){
34                 location.href="http://www.baidu.com";
35             }else if(json.result == "0001"){
36                 alert(json.msg);
37             }else{
38                 alert(json.msg);
39             }
40         }); */
41         
42         /* 
43         使用post请求
44         $.post("login.love",{"userName":name,"userPass":pass},function(obj){ //obj:后台返回给我的哪个json字符串
45             
46             var json = JSON.parse(obj);
47         
48             if(json.result == "0000"){
49                 location.href="http://www.baidu.com";
50             }else if(json.result == "0001"){
51                 alert(json.msg);
52             }else{
53                 alert(json.msg);
54             }
55         }) */
56         
57             
58             
59             $.ajax({
60                 
61                 
62                 url:"login.do",//目标地址
63                 type:"post",//请求方式
64                 data:{"userName":name,"userPass":pass},//请求参数
65                 success:function(obj){//obj:后台返回给我们的一个json的字符串
66                     
67                     var json=JSON.parse(obj);
68                 
69                 if(json.result=="0000"){
70                     location.href="http//www.baidu.com";
71                 }else if (json.result=="0001") {
72                     alert(json.msg);
73                 }else{
74                     alert(json.msg);
75                 }
76                     
77                 }
78                 
79             });
80             
81             
82         });
83     });
84 
85 </script>

技术图片

 

使用ajax来进行登录验证

标签:button   value   ons   oca   post   long   except   orm   ram   

原文地址:https://www.cnblogs.com/dabu/p/13040243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!