Serervlet | 慕课课程实战 | 编写登录逻辑

Users.java

package com.po;

public class Users {
    private String username;
    private String password;
    
    public Users()
    {
        
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 

LoginServlet.java

处理post请求 + 判定用户 + 页面跳转

 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import com.po.Users;
12 @WebServlet(value="/servlet/LoginServlet")
13 public class LoginServlet extends HttpServlet{
14 
15     /**
16      * Constructor of the object.
17      */
18     public LoginServlet() {
19         super();
20     }
21 
22     /**
23      * Destruction of the servlet. <br>
24      */
25     public void destroy() {
26         super.destroy(); // Just puts "destroy" string in log
27         // Put your code here
28     }
29 
30     /**
31      * The doGet method of the servlet. <br>
32      *
33      * This method is called when a form has its tag value method equals to get.
34      * 
35      * @param request the request send by the client to the server
36      * @param response the response send by the server to the client
37      * @throws ServletException if an error occurred
38      * @throws IOException if an error occurred
39      */
40     public void doGet(HttpServletRequest request, HttpServletResponse response)
41             throws ServletException, IOException {
42 
43         doPost(request,response);
44     }
45 
46     /**
47      * The doPost method of the servlet. <br>
48      *
49      * This method is called when a form has its tag value method equals to post.
50      * 
51      * @param request the request send by the client to the server
52      * @param response the response send by the server to the client
53      * @throws ServletException if an error occurred
54      * @throws IOException if an error occurred
55      */
56     public void doPost(HttpServletRequest request, HttpServletResponse response)
57             throws ServletException, IOException {
58 
59         Users u = new Users();
60         String username = request.getParameter("username");
61         String password = request.getParameter("password");
62         u.setUsername(username);
63         u.setPassword(password);
64         
65         if(u.getUsername().equals("admin")&&u.getPassword().equals("admin"))
66         {
67             response.sendRedirect(request.getContextPath()+"/login_success.jsp");
68         }
69         else
70         {
71             response.sendRedirect(request.getContextPath()+"/login_failure.jsp");
72         }
73     }
74 
75     /**
76      * Initialization of the servlet. <br>
77      *
78      * @throws ServletException if an error occurs
79      */
80     public void init() throws ServletException {
81         // Put your code here
82     }
83 }

 

posted @ 2018-10-16 12:54  听说这是最长的名字了  阅读(189)  评论(0)    收藏  举报