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<User> 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<User> 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<User> 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. <!DOCTYPE html>  
  124. <html>  
  125.   <head>  
  126.     <title>Index.html</title>  
  127.       
  128.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  129.     <meta http-equiv="description" content="this is my page">  
  130.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  131.       
  132.     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  
  133.   
  134.   </head>  
  135.     
  136.   <body>  
  137.       
  138.   <form action="/Session/Login">  
  139.       
  140.     用户名:<input type="text" name="userName"/><br/>  
  141.     密码:<input type="password" name="password"/><br/>  
  142.     <input type="submit" value="登录" name="login"/>  
  143.   </form>  
  144.           
  145.   </body>  
  146. </html>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值