Servlet總結三(HttpSession會話管理)
- 2019 年 12 月 31 日
- 筆記
文章目錄
Servlet總結三(HttpSession會話管理)
簡介
HttpSession
是提供一種方式,跨多個頁面請求或對 Web 站點的多次訪問標識用戶並存儲有關該用戶的資訊。 簡單的來說就是能夠實現全局的共享數據,可以跨多個頁面請求,當然在Servlet
中可以在同一個項目中的不同的Servlet中共享數據
常用方法
void setAttribute(String name, Object value)
綁定對象到此會話上public void removeAttribute(String name)
移除綁定的對象Object getAttribute(String name)
根據指定的屬性名稱獲取指定的值(需要強轉)Enumeration getAttributeNames()
返回一個所有屬性的枚舉對象,可以通過Enumeration得到其中的值public int getMaxInactiveInterval()
返回 servlet 容器在客戶端訪問之間將使此會話保持打開狀態的最大時間間隔,以秒為單位(根據測試,這個默認的值為1800秒,如果在這個默認的時間之內沒有響應,那麼會話將會中斷)public void setMaxInactiveInterval(int interval)
指定在 servlet 容器使此會話失效之前客戶端請求之間的時間間隔,以秒為單位。負數時間指示會話永遠不會超時。
使用
我們可以通過
HttpServletRequest
的方法getSession()
獲取對象,下面我們來使用其中的函數
//Demo1中的doGet方法 public void doGet(HttpServletRequest request,HttpServletResponse response){ request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //獲取對象 HttpSession session=request.getSession(); //設置屬性login的值為auto session.setAttribute("login", "auto"); } //Demo2中的doGet方法 public void doGet(HttpServletRequest request,HttpServletResponse response){ //獲取對象 HttpSession session=request.getSession(); //獲取其中的login的值 String login=session.getAttribute("login"); }
簡單的例子
下面是一個簡單的例子實現自動登錄,在填入用戶名和密碼正確之後,並且勾選其中的自動登錄選項,那麼登錄過一次後在一天之內,如果直接登錄首頁將會直接跳轉到用戶介面,實現自動登錄的功能
index.jsp
文件中實現的是簡單的表單登錄,並沒有加上一些css,js,僅僅是一個例子
<%@ 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> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="Demo3" method="get"> <label>username:</label><input type="text" name="username"></br> <label>password:</label><input type="password" name="password"></br> <label>自動登錄:</label><input type="checkbox" name="login" value="auto"> <input type="submit" value="提交"> </form> </body> </html>
Demo3.java
是用戶的首頁,實現檢測自動登錄,沒有加一些頁面在上面,但是其實是用戶的首頁
package com; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Reader; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class Demo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 獲取HttpSession對象 HttpSession session = request.getSession(); // 設置一天的訪問時間間隔,如果超過這個時間,那麼中斷 session.setMaxInactiveInterval(24 * 60 * 60); // 獲取轉發對象,頁面跳轉 RequestDispatcher dispatcher = request .getRequestDispatcher("HTML/user.html"); // 獲取表單的數據 String username = request.getParameter("username"); String password = request.getParameter("password"); String login = request.getParameter("login"); // 獲取HttpSession中設置的屬性名為login的值,如果為null,表示沒有設置 String value = (String) session.getAttribute("login"); // 如果不為空,表示已經登錄過一次了,並且允許自動登錄,直接跳轉到用戶介面即可 if (value != null) { // 直接跳轉到用戶介面 dispatcher.forward(request, response); } else { // 如果用戶名和密碼正確 if ("chenjiabing".equals(username) && "123456".equals(password)) { // 並且設置了自動登錄 if ("auto".equals(login)) { // 設置session的值 session.setAttribute("login", "auto"); } response.sendRedirect("HTML/user.html"); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
說明:
user.html
是用戶的主頁,這裡沒有給出,可以自己設計