session是怎么工作的,session的用法
session的用法
馬克-to-win:到現(xiàn)在為止,我們學(xué)會(huì)了一次單獨(dú)的請(qǐng)求和響應(yīng)之間傳遞參數(shù)。但是如何跨越幾次請(qǐng)求響應(yīng)之間傳遞參數(shù)呢?比如我以馬克的身份登錄,這是一次請(qǐng)求響應(yīng)。之后買書又是一次請(qǐng)求響應(yīng)。如何買書的時(shí)候還記得買書的人是馬克,而不是張三呢?馬克這個(gè)參數(shù)存在哪呢?這是跨越兩次訪問(wèn)。Sun公司為我們提供了HttpSession這個(gè)接口。HttpSession session = request.getSession();通過(guò)這句話,你可以得到一個(gè)與你的瀏覽器綁定的session對(duì)象,存在Tomcat里。這個(gè)session對(duì)象只認(rèn)你這個(gè)瀏覽器,之后只要是你這個(gè)瀏覽器發(fā)出的請(qǐng)求,無(wú)論跨越多少次請(qǐng)求響應(yīng),這個(gè)session對(duì)象就對(duì)它開放,其它瀏覽器不能訪問(wèn)。通過(guò)session.setAttribute()可以往session里面存值,session.getAttribute可以取值。問(wèn)題是 session是如何識(shí)別你的瀏覽器呢?初學(xué)者可忽略:靠Cookie或者URL改寫:如果瀏覽器支持Cookie,則使用Cookie;如果瀏覽器不支持Cookie或者Cookie功能被關(guān)閉,則自動(dòng)使用URL改寫方法。拿cookie來(lái)說(shuō)(通常客戶很少見(jiàn)關(guān)閉cookie,即使你關(guān)了,我也可以發(fā)現(xiàn),之后提醒你打開或編程序重寫URL),服務(wù)器往客戶端寫東西時(shí),cookie會(huì)帶上sessionid。當(dāng)客戶端再次訪問(wèn)服務(wù)器時(shí),同一path下,會(huì)自動(dòng)在html請(qǐng)求頭中帶上cookie信息,服務(wù)器可以在_COOKIE域中得取到想要的sessionid。 馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
有時(shí)我們?cè)诰W(wǎng)絡(luò)購(gòu)物時(shí),如果有一段時(shí)間沒(méi)有碰電腦,當(dāng)我們?cè)倮^續(xù)購(gòu)物時(shí),會(huì)接到session過(guò)期的錯(cuò)誤信息。這是因?yàn)槿魏蝧ession對(duì)象,天生就有能過(guò)期的特性。我們可以通過(guò)類的方法改變失效時(shí)長(zhǎng)。
根據(jù)剛才的場(chǎng)景,我做了一個(gè)例子。由兩支組成,每一支都由一個(gè)html和一個(gè)Servlet組成??蛻粜彰蓃egister.html提交給 MarkToWinServletHello1。之后MarkToWinServletHello1把姓名存在session當(dāng)中。之后用 response.sendRedirect("buy.html");做了一個(gè)自動(dòng)跳轉(zhuǎn)到buy.html。之后開始買東西。【新手可忽略】sendRedirect的實(shí)現(xiàn)方法是通過(guò)修改回寫回客戶端的html網(wǎng)頁(yè)的HTTP協(xié)議的HEADER部分,(比如 response.setHeader("Location", "NewURL");)對(duì)瀏覽器下達(dá)重定向指令的,讓瀏覽器對(duì)在location中指定的URL提出請(qǐng)求,使瀏覽器顯示重定向網(wǎng)頁(yè)的內(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中取出來(lái)*/
String nam=(String) session.getAttribute("name");
PrintWriter pw=response.getWriter();
pw.println(nam+" want to buy "+thi);
}
}