Java Web Session 登录实例

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

标签