有關session的登入登出的一個小例子
阿新 • • 發佈:2018-12-25
下面是一個session的應用的小例子,是用來登出登入的
登陸介面的程式碼:
login.html:
使用者的javaBean
User.java:
處理登入資訊的Servlet,如果使用者賬號密碼輸入正確,就讓使用者跳轉到歡迎介面,順
便將使用者資訊加入到session中。
LoginServlet:
歡迎介面,可以從session中拿出使用者姓名資訊顯示在主頁上
index.jsp:
//登出時使用的Servlet,將session中加入的使用者資訊清除
這樣,當用戶點選退出登入的時候,session中就沒有使用者的相應資訊,使用者再次進入主頁或登入頁面的時候,就會顯示使用者沒有登入。
登陸介面的程式碼:
login.html:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>login.html</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page"
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
- </head>
- <body>
- <form action="/day07/LoginServlet" method="post">
- 使用者名稱:<input text="text" name="username"
- 密碼: <input text="password" name="password"><br/>
- <input type="submit" value="登陸">
- </body>
- </html>
使用者的javaBean
User.java:
- package cn.edu.login;
- publicclass User {
- private String username;
- private String password;
- public
- super();
- this.username = username;
- this.password = password;
- }
- public User(){
- super();
- }
- public String getUsername() {
- return username;
- }
- publicvoid setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- publicvoid setPassword(String password) {
- this.password = password;
- }
- }
處理登入資訊的Servlet,如果使用者賬號密碼輸入正確,就讓使用者跳轉到歡迎介面,順
便將使用者資訊加入到session中。
LoginServlet:
- package cn.edu.login;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- publicclass LoginServlet extends HttpServlet {
- publicvoid doGet(HttpServletRequest request, HttpServletResponse
- response)
- throws ServletException, IOException {
- response.setCharacterEncoding("UTf-8");
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out=response.getWriter();
- String username=request.getParameter("username");
- String password=request.getParameter("password");
- List<User> list=Db.getAll();//這裡的Db是我自己寫的假資料庫,裡面有一
- 些User的賬號密碼資訊,是內部類,在下面有
- for(User user:list){
- if(user.getUsername().equals(username)&&user.getPassword
- ().equals(password)){
- request.getSession().setAttribute("user", user);//登陸成
- 功,向session中存入一個登陸標記
- response.sendRedirect("/day07/index.jsp");
- return;
- }
- }
- out.write("使用者名稱或者密碼錯誤!");
- }
- publicvoid doPost(HttpServletRequest request, HttpServletResponse
- response)
- throws ServletException, IOException {
- doGet(request,response);
- }
- }
- //模擬資料庫(上面提到的)
- class Db{
- privatestatic List<User> list=new ArrayList<User>();
- static{
- list.add(new User("aaa","123"));
- list.add(new User("bbb","123"));
- list.add(new User("ccc","123"));
- }
- publicstatic List getAll(){
- return list;
- }
- }
歡迎介面,可以從session中拿出使用者姓名資訊顯示在主頁上
index.jsp:
- <%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName
- ()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'index.jsp' starting page</title>
- <matehttp-equiv="content-type"content="text/html;charset=UTF-8">
- </head>
- <body>
- 歡迎您!${user.username}
- <br/>
- <ahref="/day07/login.html">登入</a><ahref="/day07/LogoutServlet">退出
- 登入</a>
- </body>
- </html>
//登出時使用的Servlet,將session中加入的使用者資訊清除
LogoutServlet:
- package cn.edu.login;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- //完成使用者登出
- publicclass LogoutServlet extends HttpServlet {
- publicvoid doGet(HttpServletRequest request, HttpServletResponse
- response)
- throws ServletException, IOException {
- HttpSession session=request.getSession(false);
- if(session==null){
- response.sendRedirect("/day07/index.jsp");
- return;
- }
- session.removeAttribute("user");
- response.sendRedirect("/day07/index.jsp");
- }
- publicvoid doPost(HttpServletRequest request, HttpServletResponse
- response)
- throws ServletException, IOException {
- doGet(request,response);
- }
- }
這樣,當用戶點選退出登入的時候,session中就沒有使用者的相應資訊,使用者再次進入主頁或登入頁面的時候,就會顯示使用者沒有登入。