Java Web Session 登录实例

  1. package cn.com.login;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13.   
  14. public class Login extends HttpServlet {  
  15.     private static final long serialVersionUID = 1L;  
  16.      
  17.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  18.           
  19.         response.setCharacterEncoding("UTF-8");  
  20.         response.setContentType("text/html;charset=UTF-8");  
  21.           
  22.         String userName=request.getParameter("userName");  
  23.         String password=request.getParameter("password");  
  24.         PrintWriter out=response.getWriter();  
  25.           
  26.         List list=Db.getAll();  
  27.         for(User user:list)  
  28.         {  
  29.             if(user.getUserName().equals(userName)&&user.getPassword().equals(password))  
  30.             {  
  31.                 request.getSession().setAttribute("user", user);  
  32.                 response.sendRedirect("/Session/index.jsp");  
  33.                 return ;  
  34.             }  
  35.               
  36.         }  
  37.         out.write("用户名或者密码错误!");  
  38.           
  39.     }  
  40.   
  41.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  42.         doGet(request,response);  
  43.     }  
  44.   
  45. }  
  46. class Db  
  47. {  
  48.     public static List list=new ArrayList();  
  49.     static  
  50.     {  
  51.         list.add(new User("aaa","123"));  
  52.         list.add(new User("bbb","123"));  
  53.         list.add(new User("ccc","123"));  
  54.     }  
  55.     public static List getAll()  
  56.     {  
  57.         return list;  
  58.     }  
  59. }  
  60. package cn.com.login;  
  61.   
  62. public class User {  
  63.       
  64.     private String userName;  
  65.     private String password;  
  66.       
  67.     public User() {  
  68.         super();  
  69.         // TODO Auto-generated constructor stub  
  70.     }  
  71.     public User(String userName, String password) {  
  72.         super();  
  73.         this.userName = userName;  
  74.         this.password = password;  
  75.     }  
  76.     public String getUserName() {  
  77.         return userName;  
  78.     }  
  79.     public void setUserName(String userName) {  
  80.         this.userName = userName;  
  81.     }  
  82.     public String getPassword() {  
  83.         return password;  
  84.     }  
  85.     public void setPassword(String password) {  
  86.         this.password = password;  
  87.     }  
  88.       
  89.   
  90. }  
  91. package cn.com.login;  
  92.   
  93. import java.io.IOException;  
  94. import javax.servlet.ServletException;  
  95. import javax.servlet.http.HttpServlet;  
  96. import javax.servlet.http.HttpServletRequest;  
  97. import javax.servlet.http.HttpServletResponse;  
  98. import javax.servlet.http.HttpSession;  
  99.   
  100. /** 
  101.  * Servlet implementation class LogOut 
  102.  */  
  103. public class LogOut extends HttpServlet {  
  104.     private static final long serialVersionUID = 1L;  
  105.   
  106.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  107.         HttpSession session=request.getSession(false);  
  108.         if(session==null)  
  109.         {  
  110.             response.sendRedirect("/Session/index.jsp");  
  111.             return ;  
  112.         }  
  113.         session.removeAttribute("user");  
  114.         response.sendRedirect("/Session/index.jsp");  
  115.     }  
  116.   
  117.       
  118.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  119.         doGet(request,response);  
  120.     }  
  121.   
  122. }  
  123.   
  124.   
  125.     
  126.     Index.html  
  127.       
  128.     "keywords" content="keyword1,keyword2,keyword3">  
  129.     "description" content="this is my page">  
  130.     "content-type" content="text/html; charset=UTF-8">  
  131.       
  132.       
  133.   
  134.     
  135.     
  136.     
  137.       
  138.   "/Session/Login">  
  139.       
  140.     用户名:"text" name="userName"/>
      
  141.     密码:"password" name="password"/>
      
  142.     "submit" value="登录" name="login"/>  
  143.     
  144.           
  145.     
  146.   

你可能感兴趣的:(Java Web Session 登录实例)