session是怎么工作的,session的用法
session的用法
馬克-to-win:到現(xiàn)在為止,我們學(xué)會了一次單獨(dú)的請求和響應(yīng)之間傳遞參數(shù)。但是如何跨越幾次請求響應(yīng)之間傳遞參數(shù)呢?比如我以馬克的身份登錄,這是一次請求響應(yīng)。之后買書又是一次請求響應(yīng)。如何買書的時候還記得買書的人是馬克,而不是張三呢?馬克這個參數(shù)存在哪呢?這是跨越兩次訪問。Sun公司為我們提供了HttpSession這個接口。HttpSession session = request.getSession();通過這句話,你可以得到一個與你的瀏覽器綁定的session對象,存在Tomcat里。這個session對象只認(rèn)你這個瀏覽器,之后只要是你這個瀏覽器發(fā)出的請求,無論跨越多少次請求響應(yīng),這個session對象就對它開放,其它瀏覽器不能訪問。通過session.setAttribute()可以往session里面存值,session.getAttribute可以取值。問題是 session是如何識別你的瀏覽器呢?初學(xué)者可忽略:靠Cookie或者URL改寫:如果瀏覽器支持Cookie,則使用Cookie;如果瀏覽器不支持Cookie或者Cookie功能被關(guān)閉,則自動使用URL改寫方法。拿cookie來說(通常客戶很少見關(guān)閉cookie,即使你關(guān)了,我也可以發(fā)現(xiàn),之后提醒你打開或編程序重寫URL),服務(wù)器往客戶端寫東西時,cookie會帶上sessionid。當(dāng)客戶端再次訪問服務(wù)器時,同一path下,會自動在html請求頭中帶上cookie信息,服務(wù)器可以在_COOKIE域中得取到想要的sessionid。 馬克- to-win:馬克 java社區(qū):防盜版實名手機(jī)尾號: 73203。
有時我們在網(wǎng)絡(luò)購物時,如果有一段時間沒有碰電腦,當(dāng)我們再繼續(xù)購物時,會接到session過期的錯誤信息。這是因為任何session對象,天生就有能過期的特性。我們可以通過類的方法改變失效時長。
根據(jù)剛才的場景,我做了一個例子。由兩支組成,每一支都由一個html和一個Servlet組成??蛻粜彰蓃egister.html提交給 MarkToWinServletHello1。之后MarkToWinServletHello1把姓名存在session當(dāng)中。之后用 response.sendRedirect("buy.html");做了一個自動跳轉(zhuǎn)到buy.html。之后開始買東西?!拘率挚珊雎浴縮endRedirect的實現(xiàn)方法是通過修改回寫回客戶端的html網(wǎng)頁的HTTP協(xié)議的HEADER部分,(比如 response.setHeader("Location", "NewURL");)對瀏覽器下達(dá)重定向指令的,讓瀏覽器對在location中指定的URL提出請求,使瀏覽器顯示重定向網(wǎng)頁的內(nèi)容。
例:6.1
register.html:
<html>
<body>
<FORM ACTION="MarkToWinServletHello1"
METHOD="POST">
First Name:
<INPUT TYPE="TEXT" NAME="firstName"><BR>
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</CENTER>
</FORM>
</body>
</html>
ServletHello1.java
package com;
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 ServletHello1 extends HttpServlet {
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String fn=request.getParameter("firstName");
/*true means whether right now it has session or not, if it does not have, it will create a new one,if it has already, it use the current one.*/
HttpSession session = request.getSession(true);
/*將客戶姓名存入服務(wù)器的session中*/
session.setAttribute("name", fn);
response.sendRedirect("buy.html");
}
}
buy.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<FORM ACTION="Buy" METHOD="POST">
What to buy
<INPUT TYPE="TEXT" NAME="thing"><BR>
<INPUT TYPE="SUBMIT" VALUE="Submit Order">
</CENTER>
</FORM>
</body>
</html>
Buy.java:
package com;
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 Buy extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String thi=request.getParameter("thing");
/*true means whether right now it has session or not, if it does not have, it will create a new one,
if it has already, it use the current one.*/
HttpSession session = request.getSession(true);
/*將客戶姓名,從服務(wù)器的session中取出來*/
String nam=(String) session.getAttribute("name");
PrintWriter pw=response.getWriter();
pw.println(nam+" want to buy "+thi);
}
}