




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Java WebJava Web開發開發第四課第四課 跟蹤客戶狀態跟蹤客戶狀態講師:李玉明講師:李玉明目標l掌握cookie技術l掌握會話跟蹤技術l掌握URL重寫技術l了解隱藏表單技術跟蹤客戶狀態l用Cookie來傳送用于跟蹤客戶狀態的數據;l使用會話(Session)機制;l重寫URL,使它包含用于跟蹤客戶狀態的數據;l在HTML表單中加入隱藏字段,它包含用于跟蹤客戶狀態的數據。CookielCookie是在客戶端訪問Web服務器時,服務器在客戶端硬盤上存放的信息。服務器可以根據cookie來跟蹤客戶狀態,對于需要區別客戶的場合(如電子商務)特別有用。lTomcat對Cookie提供了良好的
2、支持;l寫cookie:Cookie theCookie=new Cookie(“username”,”Tom”);response.addCookie(theCookie);l讀cookie:Cookie cookies=request.getCookies();Cookie的有效期l當Servlet向客戶端寫Cookie時,可以通過Cookie類的setMaxAge(int expiry)方法來設置Cookie的有效期,單位:秒如果expiry大于零,就指示瀏覽器在客戶端硬盤上保存Cookie的時間為expiry秒;如果expiry等于零,就指示瀏覽器刪除當前Cookie;如果expiry
3、小于零,就指示瀏覽器不要把Cookie保存到客戶端硬盤,Cookie僅僅存在于當前的瀏覽器進程中,當瀏覽器進程關閉,Cookie也就消失。Cookie默認有效期為-1.CookieServlet.javapublic class CookieServlet extends HttpServlet int count=0;public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException response.setContentType(text
4、/plain);PrintWriter out = response.getWriter();/獲取cookieCookie cookies=request.getCookies();if(cookies!=null)for(int i=0;icookies.length;i+)out.println(Cookie name:+cookiesi.getName();out.println(Cookie name:+cookiesi.getValue();out.println(Cookie name:+cookiesi.getMaxAge()+rn);elseout.println(No Co
5、okie.);/寫入cookieresponse.addCookie(new Cookie(cookieName+count,cookieValue+count);count+;Web.xml內容 cookie lesson3.CookieServlet cookie /cookie 新瀏覽器進程public class Cookie1Servlet extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOEx
6、ception Cookie cookie=null;response.setContentType(text/plain);PrintWriter out = response.getWriter();/獲取cookieCookie cookies=request.getCookies();if(cookies!=null)for(int i=0;icookies.length;i+)out.println(Cookie name:+cookiesi.getName();out.println(Cookie name:+cookiesi.getValue();if(cookiesi.getN
7、ame().equals(username)cookie=cookiesi;elseout.println(No Cookie.);if(cookie=null)cookie=new Cookie(username,Tom);cookie.setMaxAge(60*60);response.addCookie(cookie);else if(cookie.getValue().equals(Tom)cookie.setValue(Jack);response.addCookie(cookie);else if(cookie.getValue().equals(Jack)cookie.setMa
8、xAge(0);response.addCookie(cookie);Web.xml內容 cookie1 lesson3.Cookie1Servlet cookie1 /cookie1 Session l會話指在一段時間內,單個客戶與Web應用的一連串相關的交互過程。在一個會話中,客戶可能會多次請求訪問Web應用的同一個網頁,也有可能請求訪問同一個Web應用中的多個網頁;l在Servlet API中定義了代表會話的javax.servlet.http.HttpSession接口,Servlet容器必須實現這一接口;l當一個會話開始時,Servlet容器將創建一個HttpSession對象,在該
9、對象中存放表示客戶狀態的信息,Servlet容器為每個HttpSession對象分配一個唯一標示符,稱為Session ID。Sessionl默認情況下,JSP網頁都支持會話,顯示聲明:l如果一個Web組件支持會話,表示:當客戶請求訪問該組件時,Servelet容器會自動查找HTTP請求中表示SessionID的Cookie,以及向HTTP響應結果中添加表示SessionID的Cookie;Web組件可以訪問代表當前會話的Httpsession對象。%Cookie cookies=request.getCookies();if(cookies=null)out.println(no cooki
10、e);return;for(int i=0;iCookie name:Cookie value:max age in seconds:HttpSession的生命周期l開始新的會話,即開始新的會話,即Servlet容器創建一個新的容器創建一個新的HttpSession對象:對象:一個瀏覽器進程第一次訪問一個瀏覽器進程第一次訪問Web應用中支持會話的任意一個網頁。應用中支持會話的任意一個網頁。當瀏覽器進程與當瀏覽器進程與Web應用的一次會話被銷毀后,再次訪問應用的一次會話被銷毀后,再次訪問Web應用中支應用中支持會話的任意一個網頁。持會話的任意一個網頁。l會話銷毀,即會話銷毀,即Servlet容
11、器使容器使HttpSession對象結束生命周期,且存對象結束生命周期,且存放在會話范圍內的共享數據也都被銷毀:放在會話范圍內的共享數據也都被銷毀:瀏覽器進程終止;瀏覽器進程終止;服務器端執行服務器端執行httpSession對象的對象的invalidate()方法;()方法;會話過期。會話過期。使用會話的jsplServlet容器為JSP提供了隱含的HttpSession對象,JSP可以直接通過固定引用變量session來引用HttpSession對象。maillogin.jspmaillogin歡迎光臨郵件系統Session ID:User Name:input type=text nam
12、e=username value=Password: mailcheck.jspmailcheck登錄注銷當前用戶為:你的信箱中有10封郵件maillogout.jspmaillogout,再見!重新登錄郵件系統使用會話的ServletlHttpServlet默認不支持會話;l在HttpRequest接口中提供了2種與會話相關的方法:getSession():是當前HttpServlet支持會話。若會話已存在,返回相應的httpSession對象,否則創建一個新的對象并返回。getSession(boolean create):如果為true,等價于方法1;如果為false,
13、若會話已存在,返回相應的httpSession對象,否則返回null。public class ShoppingCart implements Serializable Mapitems = new HashMap();int numberOfItems=0;public synchronized void add(String itemName)if(items.containsKey(itemName)Integer itemCount=(Integer)items.get(itemName);items.put(itemName, new Integer(itemCount+1);els
14、e items.put(itemName, new Integer(1);numberOfItems+;public synchronized int getNumberOfItems()return numberOfItems;public synchronized Map getItems()return items;public class ShoppingServlet extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletE
15、xception, IOException String itemNames=彩電,冰箱,電視機;/獲取httpsession對象HttpSession session = request.getSession(true);ShoppingCart cart = (ShoppingCart)session.getAttribute(cart);if(cart=null)cart = new ShoppingCart();session.setAttribute(cart, cart);response.setContentType(text/html;charset=GB2312);Print
16、Writer out = response.getWriter();/讀取表單數據String itemSelected;String itemIndex;/商品索引String itemName;/商品名稱itemSelected=request.getParameterValues(item);/讀取界面復選框值if(itemSelected!=null)for(int i=0;i續/輸出out.println(購物車內容);out.println(SessionID:+session.getId()+);out.println(你的購物車有+cart.getNumberOfItems()
17、+個商品:);Map items=cart.getItems();IteratorMap.Entry it=items.entrySet().iterator();while(it.hasNext()Map.Entry entry=it.next();out.println(entry.getKey()+: +entry.getValue()+);out.println(繼續購物);out.println();out.close();public void doPost(HttpServletRequest request,HttpServletResponse response)throws
18、 ServletException, IOException doGet(request,response);Web.xml增加內容 shopping lesson4.ShoppingServlet shopping /shopping shopping.html選購商品百貨商場選購商品第一種:彩電第二種:冰箱第三種:電視機重寫URLqURL(統一資源定位) 重寫技術 當客戶端不接受Cookie的時候,可以使用URL重寫機制將一個唯一的會話 ID 添加到 URL 結尾,以標識該會話。Servlet容器解釋URL,取出SessionID,根據SessionID將請求與特定的Session關聯。重寫URLl瀏覽器禁用遠程Cookie:隱私高級重寫URLl瀏覽器禁用本地服務器Cookie:安全本地重寫URLl如果瀏覽器不支持cookie,Servlet容器可以重寫Web組件的URL,把sessionID添加到URL信息中。lHttpServletResponse提供了重寫URL的方法:public String encodeURL(String
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030中國大理石行業市場發展狀況及發展趨勢與投資前景研究報告
- 磨難造就人才作文800字(14篇)
- 華北平原冬小麥凈初級生產力對干旱的響應機制及未來氣候變化應對策略研究
- 高二英語備課組家校溝通計劃
- 門窗安裝承包合同
- 新員工職業道德培訓心得體會
- 關于心靈美麗的初三作文(15篇)
- 2024年中考一模 英語(江西專用)(參考答案及評分標準)
- 信息技術在培訓行業的應用心得體會
- 能源行業財務風險評估與防控措施
- 2019瀘州中考化學試題及答案
- 五人制足球規則(教學)
- 學校食堂“三同三公開”制度實施方案
- 2025年福建福州地鐵集團有限公司招聘筆試參考題庫含答案解析
- 人工智能在新聞媒體領域的應用
- 【MOOC】儒家倫理-南京大學 中國大學慕課MOOC答案
- 銀保部三年規劃
- 2024治安調解協議書樣式
- 零工市場(驛站)運營管理 投標方案(技術方案)
- 小學二年級數學找規律練習題及答案
- 智研咨詢重磅發布:2024年中國航運行業供需態勢、市場現狀及發展前景預測報告
評論
0/150
提交評論