使用ajax来进行登录验证

2021-02-11 16:15

阅读:725

标签: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 
 2 
 3     
 4 
 5 
 6     
"login.do" method="post"> 7 8 9 用户名:"nm" name="userName" type="text" value="${userName}" /> 10 密码:"userPass" type="password" /> 11 12 13 14
15 16

技术图片

 

使用ajax来进行登录验证

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

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


评论


亲,登录后才可以留言!