![dayjavaweb之文件和課件so easy系列session教程_第1頁](http://file4.renrendoc.com/view/d23fd3d66cd86654219bf979f0b0ecfe/d23fd3d66cd86654219bf979f0b0ecfe1.gif)
![dayjavaweb之文件和課件so easy系列session教程_第2頁](http://file4.renrendoc.com/view/d23fd3d66cd86654219bf979f0b0ecfe/d23fd3d66cd86654219bf979f0b0ecfe2.gif)
![dayjavaweb之文件和課件so easy系列session教程_第3頁](http://file4.renrendoc.com/view/d23fd3d66cd86654219bf979f0b0ecfe/d23fd3d66cd86654219bf979f0b0ecfe3.gif)
![dayjavaweb之文件和課件so easy系列session教程_第4頁](http://file4.renrendoc.com/view/d23fd3d66cd86654219bf979f0b0ecfe/d23fd3d66cd86654219bf979f0b0ecfe4.gif)
![dayjavaweb之文件和課件so easy系列session教程_第5頁](http://file4.renrendoc.com/view/d23fd3d66cd86654219bf979f0b0ecfe/d23fd3d66cd86654219bf979f0b0ecfe5.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
SoEasySessionSessionSession一般譯為會話,是解決Http協(xié)議的無狀態(tài)問題的方案,可以將一次會話中的數(shù)據(jù)在IDSessionIDSession信息,進行進一步地操作。在JavaEE中提供了javax.servlet.http.HttpSession接口,通過該接口可以將共享的數(shù)據(jù)內(nèi)容HttpSessionHttpSession直接翻譯成中文比較,一般都譯成時域。在計算機專業(yè)術(shù)語中,Session是指一個終端用戶與交互系統(tǒng)進行通信的時間間隔,通常指從進入系統(tǒng)到注銷退出系統(tǒng)之間所經(jīng)過的時間。具體到Web中的Session指的就是用戶在瀏覽某個時,從進入到關(guān)閉這個所經(jīng)過的這段時間,也就是用戶瀏覽這個所花費的時間。因此從上述的定義中我們可以看到,Session實Session的概念需要包括特定的客戶端,特定的服務(wù)器端以及不中斷的操作Session。起到交換數(shù)據(jù)包的作用機制,session在網(wǎng)絡(luò)協(xié)議(例如 net或FTP)中是非常重要的部分。維持需要依靠在傳輸數(shù)據(jù)中的高級別程序。例如,在瀏覽器和主機之間的HTTP傳輸中sessionIDSession Session 都是解決Http協(xié)議的無狀態(tài)問題,但是兩者之間還是存在一定區(qū)別的 數(shù)據(jù)在客戶端的瀏覽器內(nèi)存中或本地緩存文件中,Session數(shù)據(jù)在服務(wù)器端的數(shù)據(jù)安全性較低,Session數(shù)據(jù)安全性較高,Session數(shù)據(jù)在服務(wù)器端內(nèi)存中增多時,降低服務(wù)器端性能。而 , 的數(shù)據(jù)最大是4KB,一個只能20個 。Session則沒有這個問Session在關(guān)閉瀏覽器時失效,而持 則可以更長有效時間總的來說,Session與 SessionSessionMethod ReturnstheobjectboundMethod Returnstheobjectboundwiththespecifiednameinthissession,ornullifnoobjectisboundunderthename.ReturnsanEnumerationofStringobjectscontainingthenamesofalltheobjectsboundtothissession. Removestheobjectboundwiththespecifiednamefromthis name, Bindsanobjecttothissession,usingthenameRequestHttpSessionHttpSessionHttpSessionsession=HttpSessionStringname=SessionSessionHttpSessionServletHttpSessionServletHttpSessionpublicclassFirstServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{HttpSessionsession=request.getSession(); }publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}}publicpublicclassSecondServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{PrintWriterout=response.getWriter();HttpSessionsession=Stringname=}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}}Webweb.xml<?xmlversion="1.0"encoding="UTF-<web-app 打開瀏覽器,在地址欄中輸入在地址欄中再輸入Session在JavaEE的javax.servlet.http.HttpSessionMethodReturnsthetimewhenthissessionwascreated,measuredinmillisecondsmidnightJanuary1,1970ReturnsastringcontainingtheuniqueidentifierassignedtothisReturnsthelasttimethesentarequestassociatedwiththissession,asthenumberofmillisecondssincemidnightJanuary1,1970GMT,andmarkedbythetimethereceivedtheReturnstheumtimeinterval,inseconds,thattheservletcontainerwillthissessionopenbetweenReturnstheServletContexttowhichthissessionInvalidatesthissessionthenunbindsanyobjectsboundtoReturnstrueifthedoesnotyetknowaboutthesessionorifthechoosesnottojointhesession. Specifiesthetime,inseconds,betweenrequestsbeforetheservletwillinvalidatethisStringgetId()longgetCreationTime()sessionlonggetLastAccessedTime()sessionisNew()sessionIdsession的intgetMaxInactiveInterval()session可以的最大不活動時間(秒30session30Tomcatsession1session1session就會被移除;voidinvalidate():讓session失效!調(diào)用這個方被session失效,當session失效后,客戶端再次請求,服務(wù)器會給客戶端創(chuàng)建一個新的session,并在響應(yīng)中給客戶端新session的下面我們通過一個Servlet將上述中的一些方法進試這里理解性就好后面案例中用到ServletHttpSessionpublicclassThreeServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{DateFormatformatter=newSimpleDateFormat("yyyy-MM-ddCalendarcalendar=HttpSessionsession=request.getSession();}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}}Webweb.xml<?<?xmlversion="1.0"encoding="UTF-<web-app" 打開瀏覽器,在地址欄中輸入ServletHttpSession對象的基本使用方法,到目前為止,ServletServletHttpServletRequest對象、ServletContextHttpSessionServletContext中的數(shù)據(jù)就可以共享;SessionSessionHttpSession對象實現(xiàn)了在多次會話中共享數(shù)據(jù)內(nèi)容,HttpSession對象底層到底是如何實現(xiàn)MyEclipseDebugrequest.getSession()Debug模式啟動Tomcat服務(wù)器,并對應(yīng)的Servlet,抓取獲取的Session內(nèi)容org.apache.catalina.session.StandardSessionTomcat服SessionID值。IEHttpWatchServletIEHttpWatch會發(fā)現(xiàn)客戶端向服務(wù)器端發(fā)送請求時,向服務(wù)器端發(fā)送了一 :JSESSIONID=0BD17B07E383FA86703B370560E823F2;Path=/11_session/;: 信息。進而可以總結(jié)出Session的實現(xiàn)原理如下:因為Session使用的是會話 ,所以當瀏覽器關(guān)閉后,Session會失效。重新打開瀏覽器對應(yīng)Servlet時服務(wù)器端會重新創(chuàng)建Session對象可以使用持 來延長Session的有效時間 通過Session的實現(xiàn)原理可以知道,Session的實現(xiàn)是基于的。如果瀏覽器禁用話,Session是否還是有效的呢?下面我們具體操作來看一看。打開IE瀏覽器的“工具”->“Internet選項”->“隱私”選項,所 這時重新啟動Tomcat服務(wù)器,對應(yīng)的ServletURLResponseencodeURL()URLStringStringurl=request.getRequestURI();url=response.encodeURL(url);response.getWriter().println("<ahref='"+url+打開瀏覽器,在對應(yīng)頁面中點擊禁用解決Session的問題,這種解決方案是具有理論意義的,但不具備實際意義。因為大部分的都是基于完成數(shù)據(jù)共享的例如京東或淘寶等如果瀏覽器禁用,Session關(guān)于SessioinSessionSessionSession Session信息過期時(Session30分鐘SessionSessionJSP<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-<%@page<%@page<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01<title>MyJSP'show.jsp'starting<metahttp-equiv="pragma"content="no-<metahttp-equiv="cache-control"content="no-<metahttp-equiv="expires"<metahttp- "<metahttp-equiv="description"content="Thisismy商品列表Java編程思想<ahref="/11_session/show?id=1"></a><br>Java設(shè)計模式<ahref="/11_session/show?id=2"></a><br>Java語言<ahref="/11_session/show?id=3"></a><br>Oracle數(shù)據(jù)庫<ahref="/11_session/show?id=4"></a><br>MySQL數(shù)據(jù)庫<ahref="/11_session/show?id=5"></a><br>購物車記錄Map<String,Integer>map=if(map==Set<String>keySet=for(StringproductName:intnumber out.println("商品名稱:"+productName+", 數(shù)量:"+number+"<br/>");}}}ServletpublicclassShowServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//獲 商品Stringid=獲得idString[]names={"Java編程思想","Java設(shè)計模式","Java語言 "Oracle數(shù)據(jù)庫","MySQL數(shù)據(jù)庫"};StringproductName=names[Integer.parseInt(id)-HttpSessionsession=request.getSession();Map<String,Integer>cart=(Map<String,Integer>)if(cart==null)cart=newHashMap<String,}//判 if(cart.containsKey(productName))intnumbercart.get(productName);cart.put(productName,number1);數(shù)量+1}else}//給用戶提示}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}}ServletpublicpublicclassClearServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{HttpSessionsession=跳轉(zhuǎn)}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}}Webweb.xml<?xmlversion="1.0"encoding="UTF-<web-app 打開瀏覽器,在地址欄中輸入進試所謂一次,就是生成后,只能使用一次,不管成功或者失敗,都將失效。具JSP<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01<title>MyJSP'login.jsp'starting<scriptfunction ementById("myimg").src="/11_session/checkimg?timeStamp="+newDate().getTime();<h2style="color:red;">${requestScope.msg<formaction="/11_session/login"<td><inputtype="text"name="username" <td><inputtype="password"name="password"/> <td><inputtype="text"name="checkcode"/><imgsrc="/11_session/checkimg"onclick="change();"id="myimg"style="cursor:pointer;"/></td><tdcolspan="2"><inputtype="submit"value="登陸創(chuàng)建一個Servlet用于生成publicclassCheckImgServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{intwidth=intheight=BufferedImagebufferedImage=newBufferedImage(width,//步驟 繪制背景顏色通過繪圖對GraphicsgraphicsbufferedImage.getGraphics();//筆//繪制任何圖形之前都必須指定一個顏色graphics.setColor(getRandColor(200,250));graphics.fillRect(0,0,width,height);graphics.drawRect(0,0,width-1,height-1);Graphics2Dgraphics2d=(Graphics2D)graphics2d.setFont(newFont("宋體"Font.BOLD18));Stringwords= Randomrandom=newRandom();//生成隨機數(shù)//為了 StringBufferbuffer=new定義xintx=for(inti=0;i<4;i++)graphics2d.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));-3030intjiaodu=random.nextInt(60)-doubletheta=jiaodu*Math.PI/intindexrandom.nextInt(words.length());//生成隨機數(shù)0}
charc=將cgraphics2d.rotate(theta,x,graphics2d.drawString(String.valueOf(c),x,graphics2d.rotate(-theta,x,x+=// //步驟五繪制干擾線graphics.setColor(getRandColor(160,200));intx1;intx2;inty1;intfor(inti=0;i<30;i++){x1=random.nextInt(width);x2=random.nextInt(12);y1=random.nextInt(height);y2=random.nextInt(12);graphics.drawLine(x1,y1,x1+x2,x2+}//將上 輸出到瀏覽器ImageIO.write(bufferedImage,"jpg",}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,}*@param@param@returnprivateColorgetRandColor(intfc,intbc)Randomrandom=newif(fc>255){fc=255;}if(bc>255){bc=255;}intr=fc+random.nextInt(bc-fc);intg=fc+random.nextInt(bc-fc);intb=fc+random.nextInt(bc-fc);returnnewColor(r,g,b);}}ServletpublicclassLoginServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOExcepti
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學三年級口算試題
- 電力設(shè)施升級合同(2篇)
- 2024-2025年高中化學課時分層作業(yè)7化學反應(yīng)速率的表示方法含解析蘇教版選修4
- 2024-2025學年新教材高中化學第二章海水中的重要元素-鈉和氯9氯氣的性質(zhì)練習含解析新人教版必修第一冊
- 一級警衛(wèi)接待方案
- 一年級數(shù)學下冊教學計劃
- 人事專員年終工作總結(jié)
- 幼兒園健康促進工作計劃范文
- 人事行政工作總結(jié)
- 酒店跟企業(yè)的合作協(xié)議
- 牙外傷的遠期并發(fā)癥監(jiān)測
- DL-T-1846-2018變電站機器人巡檢系統(tǒng)驗收規(guī)范
- 2025年高考語文作文備考:議論文萬能模板
- 重大事故隱患判定標準與相關(guān)事故案例培訓課件(建筑)
- 《我的寒假生活》
- DZ/T 0430-2023 固體礦產(chǎn)資源儲量核實報告編寫規(guī)范(正式版)
- (高清版)WST 442-2024 臨床實驗室生物安全指南
- 歷史時間軸全
- 高速行業(yè)網(wǎng)絡(luò)安全與維護
- 2024年能源電力行業(yè)數(shù)字化轉(zhuǎn)型解決方案
- (2024年)房地產(chǎn)銷售人員心態(tài)培訓
評論
0/150
提交評論