dayjavaweb之文件和課件so easy系列session教程_第1頁(yè)
dayjavaweb之文件和課件so easy系列session教程_第2頁(yè)
dayjavaweb之文件和課件so easy系列session教程_第3頁(yè)
dayjavaweb之文件和課件so easy系列session教程_第4頁(yè)
dayjavaweb之文件和課件so easy系列session教程_第5頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

SoEasySessionSessionSession一般譯為會(huì)話(huà),是解決Http協(xié)議的無(wú)狀態(tài)問(wèn)題的方案,可以將一次會(huì)話(huà)中的數(shù)據(jù)在IDSessionIDSession信息,進(jìn)行進(jìn)一步地操作。在JavaEE中提供了javax.servlet.http.HttpSession接口,通過(guò)該接口可以將共享的數(shù)據(jù)內(nèi)容HttpSessionHttpSession直接翻譯成中文比較,一般都譯成時(shí)域。在計(jì)算機(jī)專(zhuān)業(yè)術(shù)語(yǔ)中,Session是指一個(gè)終端用戶(hù)與交互系統(tǒng)進(jìn)行通信的時(shí)間間隔,通常指從進(jìn)入系統(tǒng)到注銷(xiāo)退出系統(tǒng)之間所經(jīng)過(guò)的時(shí)間。具體到Web中的Session指的就是用戶(hù)在瀏覽某個(gè)時(shí),從進(jìn)入到關(guān)閉這個(gè)所經(jīng)過(guò)的這段時(shí)間,也就是用戶(hù)瀏覽這個(gè)所花費(fèi)的時(shí)間。因此從上述的定義中我們可以看到,Session實(shí)Session的概念需要包括特定的客戶(hù)端,特定的服務(wù)器端以及不中斷的操作Session。起到交換數(shù)據(jù)包的作用機(jī)制,session在網(wǎng)絡(luò)協(xié)議(例如 net或FTP)中是非常重要的部分。維持需要依靠在傳輸數(shù)據(jù)中的高級(jí)別程序。例如,在瀏覽器和主機(jī)之間的HTTP傳輸中sessionIDSession Session 都是解決Http協(xié)議的無(wú)狀態(tài)問(wèn)題,但是兩者之間還是存在一定區(qū)別的 數(shù)據(jù)在客戶(hù)端的瀏覽器內(nèi)存中或本地緩存文件中,Session數(shù)據(jù)在服務(wù)器端的數(shù)據(jù)安全性較低,Session數(shù)據(jù)安全性較高,Session數(shù)據(jù)在服務(wù)器端內(nèi)存中增多時(shí),降低服務(wù)器端性能。而 , 的數(shù)據(jù)最大是4KB,一個(gè)只能20個(gè) 。Session則沒(méi)有這個(gè)問(wèn)Session在關(guān)閉瀏覽器時(shí)失效,而持 則可以更長(zhǎng)有效時(shí)間總的來(lái)說(shuō),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 打開(kāi)瀏覽器,在地址欄中輸入在地址欄中再輸入Session在JavaEE的javax.servlet.http.HttpSessionMethodReturnsthetimewhenthissessionwascreated,measuredinmillisecondsmidnightJanuary1,1970ReturnsastringcontainingtheuniqueidentifierassignedtothisReturnsthelasttimethesentarequestassociatedwiththissession,asthenumberofmillisecondssincemidnightJanuary1,1970GMT,andmarkedbythetimethereceivedtheReturnstheumtimeinterval,inseconds,thattheservletcontainerwillthissessionopenbetweenReturnstheServletContexttowhichthissessionInvalidatesthissessionthenunbindsanyobjectsboundtoReturnstrueifthedoesnotyetknowaboutthesessionorifthechoosesnottojointhesession. Specifiesthetime,inseconds,betweenrequestsbeforetheservletwillinvalidatethisStringgetId()longgetCreationTime()sessionlonggetLastAccessedTime()sessionisNew()sessionIdsession的intgetMaxInactiveInterval()session可以的最大不活動(dòng)時(shí)間(秒30session30Tomcatsession1session1session就會(huì)被移除;voidinvalidate():讓session失效!調(diào)用這個(gè)方被session失效,當(dāng)session失效后,客戶(hù)端再次請(qǐng)求,服務(wù)器會(huì)給客戶(hù)端創(chuàng)建一個(gè)新的session,并在響應(yīng)中給客戶(hù)端新session的下面我們通過(guò)一個(gè)Servlet將上述中的一些方法進(jìn)試這里理解性就好后面案例中用到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" 打開(kāi)瀏覽器,在地址欄中輸入ServletHttpSession對(duì)象的基本使用方法,到目前為止,ServletServletHttpServletRequest對(duì)象、ServletContextHttpSessionServletContext中的數(shù)據(jù)就可以共享;SessionSessionHttpSession對(duì)象實(shí)現(xiàn)了在多次會(huì)話(huà)中共享數(shù)據(jù)內(nèi)容,HttpSession對(duì)象底層到底是如何實(shí)現(xiàn)MyEclipseDebugrequest.getSession()Debug模式啟動(dòng)Tomcat服務(wù)器,并對(duì)應(yīng)的Servlet,抓取獲取的Session內(nèi)容org.apache.catalina.session.StandardSessionTomcat服SessionID值。IEHttpWatchServletIEHttpWatch會(huì)發(fā)現(xiàn)客戶(hù)端向服務(wù)器端發(fā)送請(qǐng)求時(shí),向服務(wù)器端發(fā)送了一 :JSESSIONID=0BD17B07E383FA86703B370560E823F2;Path=/11_session/;: 信息。進(jìn)而可以總結(jié)出Session的實(shí)現(xiàn)原理如下:因?yàn)镾ession使用的是會(huì)話(huà) ,所以當(dāng)瀏覽器關(guān)閉后,Session會(huì)失效。重新打開(kāi)瀏覽器對(duì)應(yīng)Servlet時(shí)服務(wù)器端會(huì)重新創(chuàng)建Session對(duì)象可以使用持 來(lái)延長(zhǎng)Session的有效時(shí)間 通過(guò)Session的實(shí)現(xiàn)原理可以知道,Session的實(shí)現(xiàn)是基于的。如果瀏覽器禁用話(huà),Session是否還是有效的呢?下面我們具體操作來(lái)看一看。打開(kāi)IE瀏覽器的“工具”->“Internet選項(xiàng)”->“隱私”選項(xiàng),所 這時(shí)重新啟動(dòng)Tomcat服務(wù)器,對(duì)應(yīng)的ServletURLResponseencodeURL()URLStringStringurl=request.getRequestURI();url=response.encodeURL(url);response.getWriter().println("<ahref='"+url+打開(kāi)瀏覽器,在對(duì)應(yīng)頁(yè)面中點(diǎn)擊禁用解決Session的問(wèn)題,這種解決方案是具有理論意義的,但不具備實(shí)際意義。因?yàn)榇蟛糠值亩际腔谕瓿蓴?shù)據(jù)共享的例如京東或淘寶等如果瀏覽器禁用,Session關(guān)于SessioinSessionSessionSession Session信息過(guò)期時(shí)(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è)計(jì)模式<ahref="/11_session/show?id=2"></a><br>Java語(yǔ)言<ahref="/11_session/show?id=3"></a><br>Oracle數(shù)據(jù)庫(kù)<ahref="/11_session/show?id=4"></a><br>MySQL數(shù)據(jù)庫(kù)<ahref="/11_session/show?id=5"></a><br>購(gòu)物車(chē)記錄Map<String,Integer>map=if(map==Set<String>keySet=for(StringproductName:intnumber out.println("商品名稱(chēng):"+productName+", 數(shù)量:"+number+"<br/>");}}}ServletpublicclassShowServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//獲 商品Stringid=獲得idString[]names={"Java編程思想","Java設(shè)計(jì)模式","Java語(yǔ)言 "Oracle數(shù)據(jù)庫(kù)","MySQL數(shù)據(jù)庫(kù)"};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}//給用戶(hù)提示}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 打開(kāi)瀏覽器,在地址欄中輸入進(jìn)試所謂一次,就是生成后,只能使用一次,不管成功或者失敗,都將失效。具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)建一個(gè)Servlet用于生成publicclassCheckImgServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{intwidth=intheight=BufferedImagebufferedImage=newBufferedImage(width,//步驟 繪制背景顏色通過(guò)繪圖對(duì)GraphicsgraphicsbufferedImage.getGraphics();//筆//繪制任何圖形之前都必須指定一個(gè)顏色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();//生成隨機(jī)數(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());//生成隨機(jī)數(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論