如题所述
ããHTTPæ¯ä¸ç§æ è¿æ¥çåè®®ï¼å¦æä¸ä¸ªå®¢æ·ç«¯åªæ¯å纯å°è¯·æ±ä¸ä¸ªæ件ï¼HTMLæGIFï¼ï¼æå¡å¨ç«¯å¯ä»¥ååºç»å®¢æ·ç«¯ï¼å¹¶ä¸éè¦ç¥éä¸è¿ä¸²ç请æ±æ¯å¦æ¥èªäºç¸åç客æ·ç«¯ï¼èä¸ä¹ä¸éè¦æ
å¿å®¢æ·ç«¯æ¯å¦å¤å¨è¿æ¥ç¶æãä½æ¯è¿æ ·çéä¿¡å议使å¾æå¡å¨ç«¯é¾ä»¥å¤ææè¿æ¥ç客æ·ç«¯æ¯å¦æ¯åä¸ä¸ªäººãå½è¿è¡Webç¨åºå¼åæ¶ï¼æ们å¿
é¡»æ³åæ³å°ç¸å
³ç请æ±ç»åä¸èµ·ï¼å¹¶ä¸åªåç»´æç¨æ·çç¶æå¨æå¡å¨ä¸ï¼è¿å°±å¼åºäºä¼è¯è¿½è¸ªï¼session trackingï¼ã
ãã1:ä¼è¯ä¸ä¼è¯è¿½è¸ª
ããsessionä¸æç»å¸¸ç¿»è¯ä¸ºâä¼è¯âï¼å ¶æ¬æ¥çå«ä¹æ¯ææå§æç»çä¸ç³»åå¨ä½ææ¶æ¯ï¼æ¯å¦æçµè¯æ¶ä»æ¿èµ·çµè¯æ¨å·å°ææçµè¯è¿ä¸é´çä¸ç³»åè¿ç¨å¯ä»¥ç§°ä¹ä¸ºä¸ä¸ªsessionãææ¶åå¯ä»¥çå°è¿æ ·çè¯âå¨ä¸ä¸ªæµè§å¨ä¼è¯æé´â¦â¦âï¼è¿éçä¼è¯ä¸è¯ç¨çå°±æ¯å ¶æ¬ä¹ï¼æ¯æä»ä¸ä¸ªæµè§å¨çªå£æå¼å°å ³éè¿ä¸ªæé´ï¼å¦æ说âç¨æ·å¨ä¸æ¬¡ä¼è¯æé´â¦â¦âè¿æ ·ä¸å¥è¯ï¼å®æç¨æ·çä¸ç³»åå¨ä½ï¼æ¯å¦ä»ç»å½å°éè´ååå°ç»è´¦ç»åºè¿æ ·ä¸ä¸ªç½ä¸è´ç©çè¿ç¨ï¼ç¶èææ¶åä¹å¯è½ä» ä» æ¯æä¸æ¬¡è¿æ¥ãsessionçå«ä¹å¾å¤ï¼å ¶ä¸çå·®å«åªè½é ä¸ä¸ææ¥æ¨æãsession trackingï¼ä¼è¯è¿½è¸ªï¼æ¯æä¸ç±»ç¨æ¥å¨å®¢æ·ç«¯ä¸æå¡å¨ä¹é´ä¿æç¶æç解å³æ¹æ¡ï¼ç®åå°è¯´ï¼å½ä¸ä¸ªå®¢æ·å¨å¤ä¸ªé¡µé¢é´åæ¢æ¶ï¼æå¡å¨ä¼ä¿å该ç¨æ·çä¿¡æ¯ã
ãã2:å®ç°ä¼è¯è¿½è¸ªç4ç§æ¹å¼ä¼è¯è¿½è¸ªçå®ç°æ¹å¼æä¸å4ç§æ¹å¼ï¼
ããï¼1ï¼ä½¿ç¨æç»Cookiesï¼Persistent Cookiesï¼ã
ããï¼2ï¼éåå å«é¢å¤åæ°çURLï¼URL Rewritingï¼ã
ããï¼3ï¼å»ºç«å«ææ°æ®çéè表åå段ï¼Hidden Form Fieldï¼ã
ããï¼4ï¼ä½¿ç¨å 建session对象ã
ããåä¸ç§ä¼è¯è¿½è¸ªæ¹å¼æ¯ä¼ ç»çåæ³ï¼æ¯ç§åæ³é½æ缺ç¹ãæåä¸ç§æ¹æ³æ¯ç®åæ常ç¨ï¼ä¹æ¯æææç解å³æ¹æ¡ï¼å æ¤å¨è¿éå°æ讨论éå¿æ¾å¨ç¬¬4ç§ä¼è¯è¿½è¸ªæ¹å¼ä¸ï¼ç¶è为æ±å½»åºäºè§£ä¼è¯è¿½è¸ªçæºå¶ï¼è¿æ¯å å°ä¼ ç»çä¼è¯è¿½è¸ªæ¹å¼å åä¸çªä»ç»ã(è¿éåæçç解ä¸å¤ªä¸æ ·ï¼è®°å½ä¸æçç解ï¼Sessionçæºå¶æ¯ Java Servlet è§èè§å®çï¼ètomcat containerå®ç°äºè¿ä¸ªè§å®ï¼tomcat æ¯éè¿cookie å url rewritingçæ¹å¼æ¥å®ç°çï¼ä¹å°±æ¯éè¿cookie æurl rewriting ä¿åä¸ä¸ªseesionID, è¿æ ·å¨å é¨tomcat æè¿ä¸ªsesionIDåä¸ä¸ªMapè系起æ¥ï¼è¾¾å°å°åéåsessionè系起æ¥çç®çãæ以第ä¸å第äºä¸æ¹æ³æ¯tomcatæå ¶ä»servlet containerå®ç°sessionæºå¶çæ段ï¼å½ç¶æ们ä¹å¯ä»¥èªå·±å®ç°ãè第ä¸ç§æ¹æ³åªæ¯å¨ä¸¤ä¸ªé¡µé¢è·³è½¬æ¶ä¼ éåéçä¸ç§æ¹å¼ï¼è¦æ³ç¨è¿ç§æ¹å¼å®ç°sesionæºå¶è¿æ¯ä¸å¤ªç°å®ï¼è¦æ¯ä¸ªé¡µé¢é½åä¸hiddenæ°æ®ï¼èä¸è¦åä¸ææè¦ä¼ çåéã)
ãã2.1:使ç¨Cookie
ããCookieæ¯ä¸ä¸ªå°å°çææ¬æ件ï¼å®æ¯å°ä¼è¯ä¿¡æ¯è®°å½å¨è¿ä¸ªææ¬æ件å ï¼æ¯ä¸ªé¡µé¢é½å»Cookieä¸æå以åçä¼è¯ä¿¡æ¯ãä¾å¦ï¼
ããString sessionID = makeUniqueString();
ããHashMap sessionInfo = new HashMap();
ããHashMap globalTable = findTableStoringSessions();
ããglobalTable.put(sessionID, sessionInfo);
ããCookie sessionCookie =new Cookie("JSESSIONID", sessionID);
ããsessionCookie.setPath("/");
ããresponse.addCookie(sessionCookie);
ããä¸é¢è¿æ®µä»£ç å å°ä¼è¯ä¿¡æ¯è®°å½å¨HashMapä¸ï¼ä¿åå¨æå¡å¨ç«¯ï¼å¹¶ç¨sessionIDæ è¯ï¼ç¶åæsessionIDä¿åå¨å为âJSESSIONIDâçCookieä¸ã
ããCookie[] cookies = request.getCookies();
ããString sessionid = null;
ããHashMap sessionInfo = null;
ããHashMap globalTable = findTableStoringSessions();
ããif(cookies!=null){
ããfor(int i=0;i<cookies.length;i++){
ããif(cookies[i].getName().equals("JSESSIONID")){
ããsessionid = cookies[i].getValue();
ããbreak;
ãã}
ãã}
ããif(sessionid!=null){
ããsessionInfo = globalTable.get(sessionid);
ãã//We can use the sessionInfo to get value that we want
ãã}
ãã}
ããç¨æ·è¯·æ±å°è¾¾æå¡å¨åï¼å ä»Cookieä¸ååºsessionIDï¼ç¶åä»HashMapä¸ååºä¼è¯ä¿¡æ¯ãè¿æ ·å°±å®ç°äºä¼è¯è¿½è¸ªã
ããè½ç¶Cookie强大ä¸æç»æ§é«ï¼ä½æ¯ç±äºæäºç¨æ·å 为æ å¿Cookie对个人éç§çå¨èï¼ä¼å ³éCookieï¼ä¸æ¦å¦æ¤ï¼ä¾¿æ æ³å©ç¨Cookieæ¥è¾¾å°ä¼è¯è¿½è¸ªçåè½ã
ãã2.2:URLéå
ããURLéåæ¯å©ç¨GETçæ¹æ³ï¼å¨URLçå°¾é¨æ·»å ä¸äºé¢å¤çåæ°æ¥è¾¾å°ä¼è¯è¿½è¸ªï¼session trackingï¼çç®çï¼æå¡å¨å°è¿ä¸ªæ è¯ç¬¦ä¸å®æåå¨çæå ³ä¼è¯çæ°æ®å ³èèµ·æ¥ãURLçèµ·æ¥å¦ä¸ï¼
ããhttp://host/path/file.html;jsessionid=1234,
ãã使ç¨URLéåçä¼ç¹æ¯Cookie被ç¦ç¨æè æ ¹æ¬ä¸æ¯æçæ åµä¸ä¾æ§è½å¤å·¥ä½ãä½ä¹æå¾å¤ç¼ºç¹ï¼
ãã1. å¿ é¡»å¯¹æææåæ¨çç½ç«çURLè¿è¡ç¼ç ã
ãã2. ææ页é¢å¿ é¡»å¨æçæã
ãã3. ä¸è½ä½¿ç¨é¢å è®°å½ä¸æ¥çURLè¿è¡è®¿é®ï¼æè ä»å ¶ä»ç½ç«é¾æ¥è¿è¡è®¿é®ã
ãã2.3:éè表åå段
ããéè表åå段çæ¹æ³ï¼æ¯å©ç¨HTMLå hiddençå±æ§ï¼æ客æ·ç«¯çä¿¡æ¯ï¼å¨ç¨æ·ä¸å¯è§çæ å½¢ä¸ï¼å·å·å°éç请æ±ä¸èµ·ä¼ éç»å°æå¡å¨å¤çï¼è¿æ ·ä¸æ¥ï¼å°±å¯ä»¥è¿è¡ä¼è¯è·è¸ªçä»»å¡äºãå¯ä»¥ä¸åçæ¹æ³æ¥åéè表åå段çä¼è¯è¿½è¸ªã
ãã<input type="hidden" name="userID" value="15">
ããç¶åå°éè¦çç¨æ·ä¿¡æ¯ï¼å¦IDä¹ç±»ç¬ä¸æ äºçæ°æ®ï¼ä»¥éèå段çæ¹å¼ä¼ éç»æå¡å¨ãéèå段çä¼ç¹å¨äºsessionæ°æ®ä¼ éå°æå¡å¨ç«¯æ¶ï¼å¹¶ä¸è±¡GETçæ¹æ³ï¼ä¼å°sessionæ°æ®ä¿é²å¨URLä¹ä¸ãä¸è¿è¿ç§åæ³è¿æ¯æå®ç缺ç¹ï¼ä¸æ¦sessionæ°æ®å¨åå¨éèå段ä¸ï¼å°±ä»ç¶ææ´é²æ°æ®çå±æºï¼å 为åªè¦ç¨æ·ç´æ¥è§çHTMLçæºæ件ï¼sessionæ°æ®å°ä¼æ´é²æ çãè¿å°é æå®å ¨ä¸çæ¼æ´ï¼ç¹å«å½ç¨æ·æ°æ®æ¯ä¾èµäºç¨æ·IDãå¯ç æ¥åå¾çæ¶åï¼å°ä¼æ被çç¨çå±é©ãå¦å¤è¿ç§æ¹æ³åªéç¨ç¹å®çä¸ä¸ªæµç¨ï¼ä¸éç¨äºé常æä¹çä¼è¯è·è¸ªã
ãã
ãã2.4:使ç¨å 建session对象
ããä¼ ç»çä¼è¯è¿½è¸ªæ¹å¼ä½¿ç¨æ¯è¾éº»ç¦ï¼Servletçä¼è¯æºå¶åºäºCookieæURLéåææ¯ï¼èåäºè¿ä¸¤ç§ææ¯çä¼ç¹ãå½å®¢æ·ç«¯å 许使ç¨Cookieæ¶ï¼å 建session对象使ç¨Cookieè¿è¡ä¼è¯è¿½è¸ªï¼å¦æ客æ·ç«¯ç¦ç¨Cookieï¼åéæ©ä½¿ç¨URLéåã
ãã
ããï¼1ï¼è·åsession对象ä¾å¦æè´ç©è½¦ä½ä¸ºå±æ§åå¨å¨sessionä¸ï¼å¨å ¶ä»JSP页é¢ä¸å¯ä»¥éè¿sessionåè·å¾è´ç©è½¦ã
ãã// å¨JSP页é¢ä¸å¯ä»¥ç´æ¥ä½¿ç¨session
ããShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
ããå 建çsession对象æ¯javax.servlet.http.HttpSessionç±»çå®ä¾ï¼å¦æå¨JavaBeanæè Servletä¸ä½¿ç¨sessionå°±éè¦å ä»å½åçrequest对象ä¸åå¾ï¼ä¾å¦ï¼
ãã// å¾å°ç¨æ·sessionåè´ç©ç¯®
ããHttpSession session = request.getSession();
ããShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
ãã
ããï¼2ï¼è¯»åsessionä¸çæ°æ®åsessionä¸åå ¥å¯¹è±¡ä½¿ç¨setAttributeæ¹æ³ï¼éè¿getAttributeæ¹æ³è¯»å对象ãä»sessionè¿åçå¼æ³¨æè¦è½¬æ¢æåéçç±»åï¼è¦æ³¨ææ£æ¥ç» ææ¯å¦ä¸ºnullãä¾å¦ä¸é¢ä¸æ®µä»£ç ï¼
ããHttpSession session = request.getSession();
ããSomeClass value = (SomeClass)session.getAttribute("someID");
ããif (value == null) {
ããvalue = new SomeClass(...);
ããsession.setAttribute("someID", value);
ãã}
ããdoSomethingWith(value);
ãã
ããï¼3ï¼åºå¼sessionæ°æ®è°ç¨removeAttributeåºå¼sessionä¸çå¼ï¼å³ç§»é¤ä¸åç§°å ³èçå¼ã
ããè°ç¨invalidateåºå¼æ´ä¸ªsessionï¼å³åºå¼å½åçsessionã
ããå¦æç¨æ·æ³¨é离å¼ç«ç¹ï¼æ³¨æåºå¼ä¸ç¨æ·ç¸å ³èçææsessionã
ãã
ããï¼4ï¼sessionççå½å¨æç±äºæ²¡æåæ³ç¥éHTTP客æ·ç«¯æ¯å¦ä¸åéè¦sessionï¼å æ¤æ¯ä¸ªsessioné½å ³èä¸ä¸ªæ¶é´æé使å®çèµæºå¯ä»¥è¢«åæ¶ãsetMaxInactiveInterval(int secondsToLive)
ãã
ããï¼5ï¼æå¡å¨ä½¿ç¨sessionæ¶,é»è®¤ä½¿ç¨Cookieææ¯è¿è¡ä¼è¯è¿½è¸ªï¼é常ï¼ä¼è¯ç®¡çæ¯éè¿æå¡å¨å° Session ID ä½ä¸ºä¸ä¸ª cookie åå¨å¨ç¨æ·ç Web æµè§å¨ä¸ï¼å¹¶ç¨å®æ¥å¯ä¸æ è¯æ¯ä¸ªç¨æ·ä¼è¯ãå¦æ客æ·ç«¯ä¸æ¥åCookieçæ¶åï¼æå¡å¨å¯ä»¥å©ç¨URLéåçæ¹å¼å°sessionIDä½ä¸ºåæ°éå¨URLåé¢ï¼æ¥å®ç°ä¼è¯ç®¡çã
ãã
ããå½æ们å¨è¿è¡forward,redirectæ¶ï¼ä¸å®è¦è°ç¨ä¸è¾¹ä¸¤ä¸ªæ¹æ³ï¼ä»¥ä¿æsessionä¸ç´ææï¼å¦æä¸è°ç¨çè¯ï¼é£ä¹ä½ å°äºæ°ç页é¢æ¶session就失æäºï¼å 为session ID没æä¼ è¿æ¥ï¼
ããServletä¸Interface HttpServletResponse è§å®äºä¸¤ä¸ªæ¹æ³ï¼response.encodeURL()æresponse.encodeRedirectURL()æ¹æ³ï¼è¿ä¸¤ä¸ªæ¹æ³é¦å å¤æCookiesæ¯å¦è¢«æµè§å¨æ¯æï¼å¦ææ¯æï¼ååæ°URL被åæ ·è¿åï¼session IDå°éè¿Cookiesæ¥ç»´æï¼å¦åè¿å带æsessionIDçURLãTomcatæå¡å¨å®ç°äºè¿ä¸¤ä¸ªæ¹æ³ã
ããä¸é¢æ¯ä½¿ç¨encodeURLæ¹æ³ç示ä¾ï¼ä¸¤ä¸ªæ件hello1.jspåhello2.jspã
ããa: hello1.jspçå®æ´ç¨åºä»£ç å¦ä¸ï¼
ãã<%@ page contentType="text/html;charset=gb2312"%>
ãã<%String url =response.encodeURL("hello2.jsp");%>
ãã<a href='<%=url%>'>è¿å ¥å°hello2.jsp</a>
ããb: 解é:
ããhello1.jspå©ç¨äºresponse对象å çencodeURLæ¹æ³ï¼å°URLåäºä¸ä¸ªç¼ç å¨ä½ãç¼ç ä¸æ¯è¿éå ³å¿çéç¹ï¼éç¹æ¯å¦ææµè§å¨çcookie被ç¦ç¨çè¯é£ä¹è±¡;jsessionid=A09F3A5583825EE787580106CC62A1E8 è¿æ ·å符串就ä¼è¢«æ·»å å° hello2.jsp çåé¢ï¼ä¹å°±æ¯åè¯äºä¸ä¸ä¸ªé¡µé¢sessionçä¿¡æ¯ã
ãã
ããb: è¥è¦ä½¿ç¨éå®åï¼ä¾å¦:
ããresponse.sendRedirect("hello2.jsp");ä¹åºè¯¥æ¹ä¸º:response.sendRedirect(response.encodeRedirectURL("hello2.jsp"));åæ¶éè¦æ³¨æçæ¯ï¼å°sessionçID以URLçç¼ç æ¹å¼è¿è¡æ¶ï¼éå°æ¯ä¸é¡µé½ç¼ç ï¼æè½ä¿çä½sessionçIDãå¦æéå°æ²¡æç¼ç çURLï¼åæ æ³è¿è¡ä¼è¯è·è¸ªã
ãã
ããc: hello2.jspçå®æ´ç¨åºä»£ç å¦ä¸ï¼
ãã<%@ page contentType="text/html;charset=gb2312"%>
ãã<% out.println("sessionID is "+session.getId());%>
ããd: å¯ä»¥çå°å¦ææå¡å¨ä½¿ç¨URLéåï¼å®å°ä¼è¯ä¿¡æ¯éå å°URLä¸ï¼å¦ä¸æ示ï¼
ããhttp://localhost:8080/ch09/hello2.jsp;jsessionid=A09F3A5583825EE787580106CC62A1E8
ãã
ããe: å®è´¨ä¸ URL éåæ¯éè¿å URL è¿æ¥æ·»å åæ°ï¼å¹¶æ session ID ä½ä¸ºå¼å å«å¨è¿æ¥ä¸,以便åºç¨æå¡å¨å¯ä»¥æ ¹æ®sessionIDä»cacheä¸çååsession.
ãã
ãã
ããTOMCATæå¡å¨
ããSESSIONå®ç°ä¼è¯è·è¸ªé常æ¯cookieåurléåï¼å¦ææµè§å¨ä¸ç¦æ¢cookieçè¯ï¼tomcatä¼å 使ç¨cookieå®ç°ï¼å¦åå®å°ä½¿ç¨URLéåæ¥æ¯æSESSION.
ããURLéåçé¢å¤æ°æ®æ¯æå¡å¨èªå¨æ·»å çï¼é£ä¹æå¡å¨æ¯æä¹æ·»å çå¢ï¼Tomcatå¨è¿åResponseçæ¶åï¼æ£æ¥JSP页é¢ä¸ææçURLï¼å æ¬ææçé¾æ¥ï¼å FormçActionå±æ§ï¼å¨è¿äºURLåé¢å ä¸â;jsessionid=xxxxxxâãæ·»å urlåç¼ç代ç ç段å¦ä¸ï¼
ããorg.apache.coyote.tomcat5.CoyoteResponseç±»çtoEncoded()æ¹æ³æ¯æURLéåã
ãã1 StringBuffer sb = new StringBuffer(path);
ãã2 if( sb.length() > 0 ) { // jsessionid can't be first.
ãã3 sb.append(";jsessionid=");
ãã4 sb.append(sessionId);
ãã5 }
ãã6 sb.append(anchor);
ãã7 sb.append(query);
ãã8 return (sb.toString());
ãã1:ä¼è¯ä¸ä¼è¯è¿½è¸ª
ããsessionä¸æç»å¸¸ç¿»è¯ä¸ºâä¼è¯âï¼å ¶æ¬æ¥çå«ä¹æ¯ææå§æç»çä¸ç³»åå¨ä½ææ¶æ¯ï¼æ¯å¦æçµè¯æ¶ä»æ¿èµ·çµè¯æ¨å·å°ææçµè¯è¿ä¸é´çä¸ç³»åè¿ç¨å¯ä»¥ç§°ä¹ä¸ºä¸ä¸ªsessionãææ¶åå¯ä»¥çå°è¿æ ·çè¯âå¨ä¸ä¸ªæµè§å¨ä¼è¯æé´â¦â¦âï¼è¿éçä¼è¯ä¸è¯ç¨çå°±æ¯å ¶æ¬ä¹ï¼æ¯æä»ä¸ä¸ªæµè§å¨çªå£æå¼å°å ³éè¿ä¸ªæé´ï¼å¦æ说âç¨æ·å¨ä¸æ¬¡ä¼è¯æé´â¦â¦âè¿æ ·ä¸å¥è¯ï¼å®æç¨æ·çä¸ç³»åå¨ä½ï¼æ¯å¦ä»ç»å½å°éè´ååå°ç»è´¦ç»åºè¿æ ·ä¸ä¸ªç½ä¸è´ç©çè¿ç¨ï¼ç¶èææ¶åä¹å¯è½ä» ä» æ¯æä¸æ¬¡è¿æ¥ãsessionçå«ä¹å¾å¤ï¼å ¶ä¸çå·®å«åªè½é ä¸ä¸ææ¥æ¨æãsession trackingï¼ä¼è¯è¿½è¸ªï¼æ¯æä¸ç±»ç¨æ¥å¨å®¢æ·ç«¯ä¸æå¡å¨ä¹é´ä¿æç¶æç解å³æ¹æ¡ï¼ç®åå°è¯´ï¼å½ä¸ä¸ªå®¢æ·å¨å¤ä¸ªé¡µé¢é´åæ¢æ¶ï¼æå¡å¨ä¼ä¿å该ç¨æ·çä¿¡æ¯ã
ãã2:å®ç°ä¼è¯è¿½è¸ªç4ç§æ¹å¼ä¼è¯è¿½è¸ªçå®ç°æ¹å¼æä¸å4ç§æ¹å¼ï¼
ããï¼1ï¼ä½¿ç¨æç»Cookiesï¼Persistent Cookiesï¼ã
ããï¼2ï¼éåå å«é¢å¤åæ°çURLï¼URL Rewritingï¼ã
ããï¼3ï¼å»ºç«å«ææ°æ®çéè表åå段ï¼Hidden Form Fieldï¼ã
ããï¼4ï¼ä½¿ç¨å 建session对象ã
ããåä¸ç§ä¼è¯è¿½è¸ªæ¹å¼æ¯ä¼ ç»çåæ³ï¼æ¯ç§åæ³é½æ缺ç¹ãæåä¸ç§æ¹æ³æ¯ç®åæ常ç¨ï¼ä¹æ¯æææç解å³æ¹æ¡ï¼å æ¤å¨è¿éå°æ讨论éå¿æ¾å¨ç¬¬4ç§ä¼è¯è¿½è¸ªæ¹å¼ä¸ï¼ç¶è为æ±å½»åºäºè§£ä¼è¯è¿½è¸ªçæºå¶ï¼è¿æ¯å å°ä¼ ç»çä¼è¯è¿½è¸ªæ¹å¼å åä¸çªä»ç»ã(è¿éåæçç解ä¸å¤ªä¸æ ·ï¼è®°å½ä¸æçç解ï¼Sessionçæºå¶æ¯ Java Servlet è§èè§å®çï¼ètomcat containerå®ç°äºè¿ä¸ªè§å®ï¼tomcat æ¯éè¿cookie å url rewritingçæ¹å¼æ¥å®ç°çï¼ä¹å°±æ¯éè¿cookie æurl rewriting ä¿åä¸ä¸ªseesionID, è¿æ ·å¨å é¨tomcat æè¿ä¸ªsesionIDåä¸ä¸ªMapè系起æ¥ï¼è¾¾å°å°åéåsessionè系起æ¥çç®çãæ以第ä¸å第äºä¸æ¹æ³æ¯tomcatæå ¶ä»servlet containerå®ç°sessionæºå¶çæ段ï¼å½ç¶æ们ä¹å¯ä»¥èªå·±å®ç°ãè第ä¸ç§æ¹æ³åªæ¯å¨ä¸¤ä¸ªé¡µé¢è·³è½¬æ¶ä¼ éåéçä¸ç§æ¹å¼ï¼è¦æ³ç¨è¿ç§æ¹å¼å®ç°sesionæºå¶è¿æ¯ä¸å¤ªç°å®ï¼è¦æ¯ä¸ªé¡µé¢é½åä¸hiddenæ°æ®ï¼èä¸è¦åä¸ææè¦ä¼ çåéã)
ãã2.1:使ç¨Cookie
ããCookieæ¯ä¸ä¸ªå°å°çææ¬æ件ï¼å®æ¯å°ä¼è¯ä¿¡æ¯è®°å½å¨è¿ä¸ªææ¬æ件å ï¼æ¯ä¸ªé¡µé¢é½å»Cookieä¸æå以åçä¼è¯ä¿¡æ¯ãä¾å¦ï¼
ããString sessionID = makeUniqueString();
ããHashMap sessionInfo = new HashMap();
ããHashMap globalTable = findTableStoringSessions();
ããglobalTable.put(sessionID, sessionInfo);
ããCookie sessionCookie =new Cookie("JSESSIONID", sessionID);
ããsessionCookie.setPath("/");
ããresponse.addCookie(sessionCookie);
ããä¸é¢è¿æ®µä»£ç å å°ä¼è¯ä¿¡æ¯è®°å½å¨HashMapä¸ï¼ä¿åå¨æå¡å¨ç«¯ï¼å¹¶ç¨sessionIDæ è¯ï¼ç¶åæsessionIDä¿åå¨å为âJSESSIONIDâçCookieä¸ã
ããCookie[] cookies = request.getCookies();
ããString sessionid = null;
ããHashMap sessionInfo = null;
ããHashMap globalTable = findTableStoringSessions();
ããif(cookies!=null){
ããfor(int i=0;i<cookies.length;i++){
ããif(cookies[i].getName().equals("JSESSIONID")){
ããsessionid = cookies[i].getValue();
ããbreak;
ãã}
ãã}
ããif(sessionid!=null){
ããsessionInfo = globalTable.get(sessionid);
ãã//We can use the sessionInfo to get value that we want
ãã}
ãã}
ããç¨æ·è¯·æ±å°è¾¾æå¡å¨åï¼å ä»Cookieä¸ååºsessionIDï¼ç¶åä»HashMapä¸ååºä¼è¯ä¿¡æ¯ãè¿æ ·å°±å®ç°äºä¼è¯è¿½è¸ªã
ããè½ç¶Cookie强大ä¸æç»æ§é«ï¼ä½æ¯ç±äºæäºç¨æ·å 为æ å¿Cookie对个人éç§çå¨èï¼ä¼å ³éCookieï¼ä¸æ¦å¦æ¤ï¼ä¾¿æ æ³å©ç¨Cookieæ¥è¾¾å°ä¼è¯è¿½è¸ªçåè½ã
ãã2.2:URLéå
ããURLéåæ¯å©ç¨GETçæ¹æ³ï¼å¨URLçå°¾é¨æ·»å ä¸äºé¢å¤çåæ°æ¥è¾¾å°ä¼è¯è¿½è¸ªï¼session trackingï¼çç®çï¼æå¡å¨å°è¿ä¸ªæ è¯ç¬¦ä¸å®æåå¨çæå ³ä¼è¯çæ°æ®å ³èèµ·æ¥ãURLçèµ·æ¥å¦ä¸ï¼
ããhttp://host/path/file.html;jsessionid=1234,
ãã使ç¨URLéåçä¼ç¹æ¯Cookie被ç¦ç¨æè æ ¹æ¬ä¸æ¯æçæ åµä¸ä¾æ§è½å¤å·¥ä½ãä½ä¹æå¾å¤ç¼ºç¹ï¼
ãã1. å¿ é¡»å¯¹æææåæ¨çç½ç«çURLè¿è¡ç¼ç ã
ãã2. ææ页é¢å¿ é¡»å¨æçæã
ãã3. ä¸è½ä½¿ç¨é¢å è®°å½ä¸æ¥çURLè¿è¡è®¿é®ï¼æè ä»å ¶ä»ç½ç«é¾æ¥è¿è¡è®¿é®ã
ãã2.3:éè表åå段
ããéè表åå段çæ¹æ³ï¼æ¯å©ç¨HTMLå hiddençå±æ§ï¼æ客æ·ç«¯çä¿¡æ¯ï¼å¨ç¨æ·ä¸å¯è§çæ å½¢ä¸ï¼å·å·å°éç请æ±ä¸èµ·ä¼ éç»å°æå¡å¨å¤çï¼è¿æ ·ä¸æ¥ï¼å°±å¯ä»¥è¿è¡ä¼è¯è·è¸ªçä»»å¡äºãå¯ä»¥ä¸åçæ¹æ³æ¥åéè表åå段çä¼è¯è¿½è¸ªã
ãã<input type="hidden" name="userID" value="15">
ããç¶åå°éè¦çç¨æ·ä¿¡æ¯ï¼å¦IDä¹ç±»ç¬ä¸æ äºçæ°æ®ï¼ä»¥éèå段çæ¹å¼ä¼ éç»æå¡å¨ãéèå段çä¼ç¹å¨äºsessionæ°æ®ä¼ éå°æå¡å¨ç«¯æ¶ï¼å¹¶ä¸è±¡GETçæ¹æ³ï¼ä¼å°sessionæ°æ®ä¿é²å¨URLä¹ä¸ãä¸è¿è¿ç§åæ³è¿æ¯æå®ç缺ç¹ï¼ä¸æ¦sessionæ°æ®å¨åå¨éèå段ä¸ï¼å°±ä»ç¶ææ´é²æ°æ®çå±æºï¼å 为åªè¦ç¨æ·ç´æ¥è§çHTMLçæºæ件ï¼sessionæ°æ®å°ä¼æ´é²æ çãè¿å°é æå®å ¨ä¸çæ¼æ´ï¼ç¹å«å½ç¨æ·æ°æ®æ¯ä¾èµäºç¨æ·IDãå¯ç æ¥åå¾çæ¶åï¼å°ä¼æ被çç¨çå±é©ãå¦å¤è¿ç§æ¹æ³åªéç¨ç¹å®çä¸ä¸ªæµç¨ï¼ä¸éç¨äºé常æä¹çä¼è¯è·è¸ªã
ãã
ãã2.4:使ç¨å 建session对象
ããä¼ ç»çä¼è¯è¿½è¸ªæ¹å¼ä½¿ç¨æ¯è¾éº»ç¦ï¼Servletçä¼è¯æºå¶åºäºCookieæURLéåææ¯ï¼èåäºè¿ä¸¤ç§ææ¯çä¼ç¹ãå½å®¢æ·ç«¯å 许使ç¨Cookieæ¶ï¼å 建session对象使ç¨Cookieè¿è¡ä¼è¯è¿½è¸ªï¼å¦æ客æ·ç«¯ç¦ç¨Cookieï¼åéæ©ä½¿ç¨URLéåã
ãã
ããï¼1ï¼è·åsession对象ä¾å¦æè´ç©è½¦ä½ä¸ºå±æ§åå¨å¨sessionä¸ï¼å¨å ¶ä»JSP页é¢ä¸å¯ä»¥éè¿sessionåè·å¾è´ç©è½¦ã
ãã// å¨JSP页é¢ä¸å¯ä»¥ç´æ¥ä½¿ç¨session
ããShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
ããå 建çsession对象æ¯javax.servlet.http.HttpSessionç±»çå®ä¾ï¼å¦æå¨JavaBeanæè Servletä¸ä½¿ç¨sessionå°±éè¦å ä»å½åçrequest对象ä¸åå¾ï¼ä¾å¦ï¼
ãã// å¾å°ç¨æ·sessionåè´ç©ç¯®
ããHttpSession session = request.getSession();
ããShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
ãã
ããï¼2ï¼è¯»åsessionä¸çæ°æ®åsessionä¸åå ¥å¯¹è±¡ä½¿ç¨setAttributeæ¹æ³ï¼éè¿getAttributeæ¹æ³è¯»å对象ãä»sessionè¿åçå¼æ³¨æè¦è½¬æ¢æåéçç±»åï¼è¦æ³¨ææ£æ¥ç» ææ¯å¦ä¸ºnullãä¾å¦ä¸é¢ä¸æ®µä»£ç ï¼
ããHttpSession session = request.getSession();
ããSomeClass value = (SomeClass)session.getAttribute("someID");
ããif (value == null) {
ããvalue = new SomeClass(...);
ããsession.setAttribute("someID", value);
ãã}
ããdoSomethingWith(value);
ãã
ããï¼3ï¼åºå¼sessionæ°æ®è°ç¨removeAttributeåºå¼sessionä¸çå¼ï¼å³ç§»é¤ä¸åç§°å ³èçå¼ã
ããè°ç¨invalidateåºå¼æ´ä¸ªsessionï¼å³åºå¼å½åçsessionã
ããå¦æç¨æ·æ³¨é离å¼ç«ç¹ï¼æ³¨æåºå¼ä¸ç¨æ·ç¸å ³èçææsessionã
ãã
ããï¼4ï¼sessionççå½å¨æç±äºæ²¡æåæ³ç¥éHTTP客æ·ç«¯æ¯å¦ä¸åéè¦sessionï¼å æ¤æ¯ä¸ªsessioné½å ³èä¸ä¸ªæ¶é´æé使å®çèµæºå¯ä»¥è¢«åæ¶ãsetMaxInactiveInterval(int secondsToLive)
ãã
ããï¼5ï¼æå¡å¨ä½¿ç¨sessionæ¶,é»è®¤ä½¿ç¨Cookieææ¯è¿è¡ä¼è¯è¿½è¸ªï¼é常ï¼ä¼è¯ç®¡çæ¯éè¿æå¡å¨å° Session ID ä½ä¸ºä¸ä¸ª cookie åå¨å¨ç¨æ·ç Web æµè§å¨ä¸ï¼å¹¶ç¨å®æ¥å¯ä¸æ è¯æ¯ä¸ªç¨æ·ä¼è¯ãå¦æ客æ·ç«¯ä¸æ¥åCookieçæ¶åï¼æå¡å¨å¯ä»¥å©ç¨URLéåçæ¹å¼å°sessionIDä½ä¸ºåæ°éå¨URLåé¢ï¼æ¥å®ç°ä¼è¯ç®¡çã
ãã
ããå½æ们å¨è¿è¡forward,redirectæ¶ï¼ä¸å®è¦è°ç¨ä¸è¾¹ä¸¤ä¸ªæ¹æ³ï¼ä»¥ä¿æsessionä¸ç´ææï¼å¦æä¸è°ç¨çè¯ï¼é£ä¹ä½ å°äºæ°ç页é¢æ¶session就失æäºï¼å 为session ID没æä¼ è¿æ¥ï¼
ããServletä¸Interface HttpServletResponse è§å®äºä¸¤ä¸ªæ¹æ³ï¼response.encodeURL()æresponse.encodeRedirectURL()æ¹æ³ï¼è¿ä¸¤ä¸ªæ¹æ³é¦å å¤æCookiesæ¯å¦è¢«æµè§å¨æ¯æï¼å¦ææ¯æï¼ååæ°URL被åæ ·è¿åï¼session IDå°éè¿Cookiesæ¥ç»´æï¼å¦åè¿å带æsessionIDçURLãTomcatæå¡å¨å®ç°äºè¿ä¸¤ä¸ªæ¹æ³ã
ããä¸é¢æ¯ä½¿ç¨encodeURLæ¹æ³ç示ä¾ï¼ä¸¤ä¸ªæ件hello1.jspåhello2.jspã
ããa: hello1.jspçå®æ´ç¨åºä»£ç å¦ä¸ï¼
ãã<%@ page contentType="text/html;charset=gb2312"%>
ãã<%String url =response.encodeURL("hello2.jsp");%>
ãã<a href='<%=url%>'>è¿å ¥å°hello2.jsp</a>
ããb: 解é:
ããhello1.jspå©ç¨äºresponse对象å çencodeURLæ¹æ³ï¼å°URLåäºä¸ä¸ªç¼ç å¨ä½ãç¼ç ä¸æ¯è¿éå ³å¿çéç¹ï¼éç¹æ¯å¦ææµè§å¨çcookie被ç¦ç¨çè¯é£ä¹è±¡;jsessionid=A09F3A5583825EE787580106CC62A1E8 è¿æ ·å符串就ä¼è¢«æ·»å å° hello2.jsp çåé¢ï¼ä¹å°±æ¯åè¯äºä¸ä¸ä¸ªé¡µé¢sessionçä¿¡æ¯ã
ãã
ããb: è¥è¦ä½¿ç¨éå®åï¼ä¾å¦:
ããresponse.sendRedirect("hello2.jsp");ä¹åºè¯¥æ¹ä¸º:response.sendRedirect(response.encodeRedirectURL("hello2.jsp"));åæ¶éè¦æ³¨æçæ¯ï¼å°sessionçID以URLçç¼ç æ¹å¼è¿è¡æ¶ï¼éå°æ¯ä¸é¡µé½ç¼ç ï¼æè½ä¿çä½sessionçIDãå¦æéå°æ²¡æç¼ç çURLï¼åæ æ³è¿è¡ä¼è¯è·è¸ªã
ãã
ããc: hello2.jspçå®æ´ç¨åºä»£ç å¦ä¸ï¼
ãã<%@ page contentType="text/html;charset=gb2312"%>
ãã<% out.println("sessionID is "+session.getId());%>
ããd: å¯ä»¥çå°å¦ææå¡å¨ä½¿ç¨URLéåï¼å®å°ä¼è¯ä¿¡æ¯éå å°URLä¸ï¼å¦ä¸æ示ï¼
ããhttp://localhost:8080/ch09/hello2.jsp;jsessionid=A09F3A5583825EE787580106CC62A1E8
ãã
ããe: å®è´¨ä¸ URL éåæ¯éè¿å URL è¿æ¥æ·»å åæ°ï¼å¹¶æ session ID ä½ä¸ºå¼å å«å¨è¿æ¥ä¸,以便åºç¨æå¡å¨å¯ä»¥æ ¹æ®sessionIDä»cacheä¸çååsession.
ãã
ãã
ããTOMCATæå¡å¨
ããSESSIONå®ç°ä¼è¯è·è¸ªé常æ¯cookieåurléåï¼å¦ææµè§å¨ä¸ç¦æ¢cookieçè¯ï¼tomcatä¼å 使ç¨cookieå®ç°ï¼å¦åå®å°ä½¿ç¨URLéåæ¥æ¯æSESSION.
ããURLéåçé¢å¤æ°æ®æ¯æå¡å¨èªå¨æ·»å çï¼é£ä¹æå¡å¨æ¯æä¹æ·»å çå¢ï¼Tomcatå¨è¿åResponseçæ¶åï¼æ£æ¥JSP页é¢ä¸ææçURLï¼å æ¬ææçé¾æ¥ï¼å FormçActionå±æ§ï¼å¨è¿äºURLåé¢å ä¸â;jsessionid=xxxxxxâãæ·»å urlåç¼ç代ç ç段å¦ä¸ï¼
ããorg.apache.coyote.tomcat5.CoyoteResponseç±»çtoEncoded()æ¹æ³æ¯æURLéåã
ãã1 StringBuffer sb = new StringBuffer(path);
ãã2 if( sb.length() > 0 ) { // jsessionid can't be first.
ãã3 sb.append(";jsessionid=");
ãã4 sb.append(sessionId);
ãã5 }
ãã6 sb.append(anchor);
ãã7 sb.append(query);
ãã8 return (sb.toString());
温馨提示:答案为网友推荐,仅供参考