1. 程式人生 > >servlet web會話管理session

servlet web會話管理session

 

小案例

package cn.cast.session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//購買
public class SessionDemo2 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession  session = request.getSession();
		session.setAttribute("name", "洗衣機");
        //摧毀程式碼session
        //session.invalidate();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
package cn.cast.session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//結賬servlet
public class SessionDemo3 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession  session = request.getSession();
        //只獲取不建立session,在檢視購物車時節省效能,假設使用者沒有購買商品(沒有建立session),檢視購物車,使用下面的程式碼就不會建立session,r如果使用上面程式碼,就會建立session,效果一樣但是效能上有優劣。
        HttpSession  session = request.getSession(false);
		String value = (String) session.getAttribute("name");
		out.println("你購買了"+value);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

session的生命週期,session是會話級別的,開啟多個瀏覽器建立的是隔離的session

並不是開啟瀏覽器就建立,session的建立需要觸發,使用者點選某個超連結時由servlet建立,在不使用時,預設30分鐘後銷燬,使用者關閉瀏覽器後session不會立即銷燬,也要等到30分鐘以後才銷燬。

在web.xml中配置session的銷燬時間。單位是min

  <session-config>
  <session-timeout>10</session-timeout>
  </session-config>

程式中也能使用程式碼摧毀session

session.invalidate();

session的實現原理基於cookie

使用者訪問伺服器建立session的時候,伺服器會返回一個帶有id號的cookie給瀏覽器,瀏覽器再次訪問瀏覽器時會帶cookie給伺服器,如果沒有攜帶cookie給伺服器,伺服器會建立session,並再次回寫cookie.這個cookie沒有設有效時間,當瀏覽器關閉時,cookie就會被摧毀,所以下次開啟瀏覽器訪問時會重新建立session.

基於上面的原理,要實現使用者在關閉瀏覽器重新開啟的時候,購物資訊任然存在就需要設定回寫cookie的存在時間(30min).

只需要重寫demo2

package cn.cast.session;

import java.io.IOException;
import java.io.PrintWriter;

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 SessionDemo2 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession  session = request.getSession();
		
		String sessionid = session.getId();
		Cookie cookie = new Cookie("JSESSIONID", sessionid);
		cookie.setMaxAge(30*60);
		cookie.setPath("/WEB");
		response.addCookie(cookie);
		
		session.setAttribute("name", "洗衣機");
		//session.invalidate();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

場景:當瀏覽器禁止使用cookie時,伺服器獲取不到session

應對方法,當瀏覽器訪問網站的時候伺服器就會建立一個session.當用戶購買商品時,超連結後面會新增sessionid號,伺服器根據這個id號存取資料。

package cn.cast.session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WelcomeSeverlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		request.getSession();
		
		//sun公司提供的帶sessionid號的url獲取方法,當用戶沒有禁用cookie時,方法不會重寫url
		String url1= response.encodeURL("/WEB/servlet/SessionDemo2");
		String url2= response.encodeURL("/WEB/servlet/SessionDemo3");
		
		out.println("<a href='"+url1+"'>購買</a>");
		out.println("<a href='"+url2+"'>結賬</a>");
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

禁用cookie後關閉瀏覽器,開啟瀏覽器時就沒辦法儲存資料了。