1. 程式人生 > >有關session的登入登出的一個小例子

有關session的登入登出的一個小例子

下面是一個session的應用的小例子,是用來登出登入的
登陸介面的程式碼:
login.html:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>login.html</title>  
  5.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  6.     <meta http-equiv="description" content="this is my page"
    >  
  7.     <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  8.     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  
  9.   </head>  
  10.   <body>  
  11.     <form action="/day07/LoginServlet" method="post">  
  12.           使用者名稱:<input text="text" name="username"
    ><br/>  
  13.           密碼:  <input text="password" name="password"><br/>  
  14.     <input type="submit" value="登陸">  
  15.   </body>  
  16. </html>  


使用者的javaBean
User.java:
  1. package cn.edu.login;  
  2. publicclass User {  
  3.     private String username;  
  4.     private String password;  
  5.     public
     User(String username, String password) {  
  6.         super();  
  7.         this.username = username;  
  8.         this.password = password;  
  9.     }  
  10.     public User(){  
  11.         super();  
  12.     }  
  13.     public String getUsername() {  
  14.         return username;  
  15.     }  
  16.     publicvoid setUsername(String username) {  
  17.         this.username = username;  
  18.     }  
  19.     public String getPassword() {  
  20.         return password;  
  21.     }  
  22.     publicvoid setPassword(String password) {  
  23.         this.password = password;  
  24.     }  
  25. }  


處理登入資訊的Servlet,如果使用者賬號密碼輸入正確,就讓使用者跳轉到歡迎介面,順


便將使用者資訊加入到session中。
LoginServlet:
  1. package cn.edu.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. publicclass LoginServlet extends HttpServlet {  
  11.     publicvoid doGet(HttpServletRequest request, HttpServletResponse   
  12. response)  
  13.             throws ServletException, IOException {  
  14.         response.setCharacterEncoding("UTf-8");  
  15.         response.setContentType("text/html;charset=UTF-8");  
  16.         PrintWriter out=response.getWriter();  
  17.        String username=request.getParameter("username");    
  18.        String password=request.getParameter("password");  
  19.        List<User> list=Db.getAll();//這裡的Db是我自己寫的假資料庫,裡面有一
  20. 些User的賬號密碼資訊,是內部類,在下面有  
  21.        for(User user:list){  
  22.            if(user.getUsername().equals(username)&&user.getPassword  
  23. ().equals(password)){  
  24.                request.getSession().setAttribute("user", user);//登陸成
  25. 功,向session中存入一個登陸標記  
  26.                response.sendRedirect("/day07/index.jsp");        
  27.                return;  
  28.            }  
  29.        }  
  30.        out.write("使用者名稱或者密碼錯誤!");  
  31.     }  
  32.     publicvoid doPost(HttpServletRequest request, HttpServletResponse   
  33. response)  
  34.             throws ServletException, IOException {  
  35.         doGet(request,response);  
  36.     }  
  37. }  
  38. //模擬資料庫(上面提到的)
  39. class Db{  
  40.     privatestatic List<User> list=new ArrayList<User>();  
  41.     static{  
  42.         list.add(new User("aaa","123"));  
  43.         list.add(new User("bbb","123"));  
  44.         list.add(new User("ccc","123"));  
  45.     }  
  46.     publicstatic List getAll(){  
  47.         return list;  
  48.     }  
  49. }  


歡迎介面,可以從session中拿出使用者姓名資訊顯示在主頁上
index.jsp:
  1. <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName  
  5. ()+":"+request.getServerPort()+path+"/";  
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9.   <head>
  10.     <title>My JSP 'index.jsp' starting page</title>
  11.     <matehttp-equiv="content-type"content="text/html;charset=UTF-8">
  12.   </head>
  13.   <body>
  14.      歡迎您!${user.username}   
  15.      <br/>
  16.   <ahref="/day07/login.html">登入</a><ahref="/day07/LogoutServlet">退出  
  17. 登入</a>
  18.   </body>
  19. </html>



//登出時使用的Servlet,將session中加入的使用者資訊清除

LogoutServlet:

  1. package cn.edu.login;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServlet;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import javax.servlet.http.HttpSession;  
  8. //完成使用者登出
  9. publicclass LogoutServlet extends HttpServlet {  
  10.     publicvoid doGet(HttpServletRequest request, HttpServletResponse   
  11. response)  
  12.             throws ServletException, IOException {  
  13.         HttpSession session=request.getSession(false);  
  14.         if(session==null){  
  15.             response.sendRedirect("/day07/index.jsp");  
  16.             return;  
  17.         }  
  18.         session.removeAttribute("user");  
  19.         response.sendRedirect("/day07/index.jsp");  
  20.     }  
  21.     publicvoid doPost(HttpServletRequest request, HttpServletResponse   
  22. response)  
  23.             throws ServletException, IOException {  
  24.        doGet(request,response);  
  25.     }  
  26. }  

這樣,當用戶點選退出登入的時候,session中就沒有使用者的相應資訊,使用者再次進入主頁或登入頁面的時候,就會顯示使用者沒有登入。