講義721大三上學(xué)習(xí)new_第1頁(yè)
講義721大三上學(xué)習(xí)new_第2頁(yè)
講義721大三上學(xué)習(xí)new_第3頁(yè)
講義721大三上學(xué)習(xí)new_第4頁(yè)
講義721大三上學(xué)習(xí)new_第5頁(yè)
已閱讀5頁(yè),還剩195頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、 Penny Shen2 2021-8-16 nServlet in big picture of J2EE nServlet request PrintWriter out = response.getWriter(); out.println(Hello World!); . Penny Shen9 2021-8-16 Penny Shen10 2021-8-16 Servlet Written in C, C+, Visual Basic and Perl Difficult to maintain, non- scalable, non- manageable Prone to sec

2、urity problems of programming language Resource intensive and inefficient Platform and application-specific Written in Java Improves scalability, reusability (component based) Powerful, reliable, and efficient Leverages built-in security of Java programming language Platform independent and portable

3、 CGI Penny Shen11 2021-8-16 CGI Based Webserver CGI Based Webserver Request CGI1 Child for CGI1 CGI Based Webserver Servlet Based Webserver JVM Request CGI1 Child for CGI1 Request Servlet1 CGI Based Webserver Servlet Based Webserver JVM Servlet1 Request CGI1 Child for CGI1 Request CGI2 Request Servl

4、et1 CGI Based Webserver Child for CGI2 Servlet Based Webserver JVM Servlet1 Request CGI1 Child for CGI1 Request CGI2 Request Servlet1 Request Servlet2 CGI Based Webserver Child for CGI2 Servlet Based Webserver JVM Servlet1 Servlet2 Request CGI1 Child for CGI1 Request CGI2 Request CGI1 Request Servle

5、t1 Request Servlet2 CGI Based Webserver Child for CGI2 Child for CGI1 Servlet Based Webserver JVM Servlet1 Servlet2 Request CGI1 Child for CGI1 Request CGI2 Request CGI1 Request Servlet1 Request Servlet2 Request Servlet1 CGI Based Webserver Child for CGI2 Child for CGI1 Servlet Based Webserver JVM S

6、ervlet1 Servlet2 Request CGI1 Child for CGI1 Penny Shen12 2021-8-16 nNo CGI limitations nAbundant third-party tools and Web servers supporting Servlet nAccess to entire family of Java APIs nReliable, better performance and scalability nPlatform and server independent nSecure nMost servers allow auto

7、matic reloading of Servlets by administrative action Penny Shen13 2021-8-16 nA text-based document capable of returning dynamic content to a client browser nContains both static and dynamic content Static content: HTML, XML Dynamic content: programming code, and JavaBeans, custom tags Penny Shen14 2

8、021-8-16 nEnables separation of business logic from presentation Presentation is in the form of HTML or XML/XSLT Business logic is implemented as Java Beans or custom tags Better maintainability, reusability nExtensible via custom tags nBuilds on Servlet technology Penny Shen15 2021-8-16 Hello World

9、! Today is Day of month: Year: Penny Shen16 2021-8-16 Comparison Penny Shen17 2021-8-16 nContent and display logic are separated nSimplify development with JSP, JavaBeans and custom tags nSupports software reuse through the use of components nRecompile automatically when changes are made to the sour

10、ce file nEasier to author web pages nPlatform-independent Penny Shen18 2021-8-16 nExtend the functionality of a Web server such as supporting a new file format nGenerate objects that do not contain HTML such as graphs or pie charts nAvoid returning HTML directly from your servlets whenever possible

11、Penny Shen19 2021-8-16 nIn practice, servlet and JSP are used together via MVC (Model, View, Controller) architecture nServlet handles Controller nJSP handles View Penny Shen20 2021-8-16 An Example HelloWorld Servlet Penny Shen21 2021-8-16 Penny Shen22 2021-8-16 思考:思考: 對(duì)比對(duì)比appletapplet的開(kāi)發(fā)和的開(kāi)發(fā)和servle

12、tservlet開(kāi)發(fā)的相同點(diǎn)和不同點(diǎn)。開(kāi)發(fā)的相同點(diǎn)和不同點(diǎn)。 URLURLweb.xmlweb.xml找到相應(yīng)的找到相應(yīng)的servletservlet運(yùn)行運(yùn)行 Penny Shen24 2021-8-16 Servlet Response Request Browser HTTP WebWeb ServerServer Servlet Container Response Request Penny Shen25 2021-8-16 RequestServlet 1 Servlet 2 Servlet 3Response Web Server Penny Shen26 2021-8-16 nR

13、eceives client request (mostly in the form of HTTP request) nExtract some information from the request nDo content generation or business logic process (possibly by accessing database,invoking EJBs, etc) nCreate and send response to client (mostly in the form of HTTP response) or forward the request

14、 to another servlet or JSP page Penny Shen27 2021-8-16 nWhat is a request? Information that is sent from client to a server nWho made the request nWhat user-entered data is sent nWhich HTTP headers are sent nWhat is a response? Information that is sent to client from a server nText(html, plain) or b

15、inary(image) data nHTTP headers, cookies, etc Penny Shen28 2021-8-16 nHTTP request contains header a method nGet: Input form data is passed as part of URL nPost: Input form data is passed within message body request data Penny Shen29 2021-8-16 nThe most common client requests HTTP GET String driver

16、= getInitParameter(driver); String fURL = getInitParameter(url); try openDBConnection(driver, fURL); catch (SQLException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace(); Penny Shen49 2021-8-16 chart ChartServlet driver COM.cloudscape.core.RmiJdbcDriver url jdbc:cloudscap

17、e:rmi:CloudscapeDB Penny Shen50 2021-8-16 public class CatalogServlet extends HttpServlet private BookDB bookDB; public void init() throws ServletException bookDB = (BookDB)getServletContext(). getAttribute(bookDB); if (bookDB = null) throw new UnavailableException(Couldnt get database.); public voi

18、d destroy() bookDB = null; . Penny Shen51 2021-8-16 nservice() javax.servlet.GenericServlet class Abstract method nservice() in javax.servlet.http.HttpServlet class Concrete method (implementation) Dispatches to doGet(), doPost(), etc Do not override this method! ndoGet(), doPost(), doXxx() in in ja

19、vax.servlet.http.HttpServlet Handles HTTP GET, POST, etc. requests Override these methods in your servlet to provide desired behavior Penny Shen52 2021-8-16 nservice() methods take generic requests and responses: service(ServletRequest request, ServletResponse response) ndoGet() or doPost() take HTT

20、P requests and responses: doGet(HttpServletRequest request, HttpServletResponse response) doPost(HttpServletRequest request, HttpServletResponse response) Penny Shen53 2021-8-16 Request Service( ) Response Server GenericServlet subclass Key: Implemented by subclass Subclass of GenericServlet class P

21、enny Shen54 2021-8-16 Request Service( ) Response ServerHttpServlet subclass Key:Implemented by subclass doGet( ) doPost( ) Penny Shen55 2021-8-16 nExtract client-sent information (HTTP parameter) from HTTP request nSet (Save) and get (read) attributes to/fromScope objects nPerform some business log

22、ic or access database nOptionally forward the request to other Web components (Servlet or JSP) nPopulate HTTP response message and send it to client Penny Shen56 2021-8-16 import javax.servlet.*; import javax.servlet.http.*; import java.io.*; Public class HelloServlet extends HttpServlet public void

23、 doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / Just send back a simple HTTP response response.setContentType(text/html); PrintWriter out = response.getWriter(); out.println(First Servlet); out.println(Hello J2EE Programmers! ); Penny Shen57 20

24、21-8-16 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / Read session-scope attribute “message” HttpSession session = request.getSession(true); ResourceBundle messages = (ResourceBundle)session.getAttribute(messages); / Set headers a

25、nd buffer size before accessing the Writer response.setContentType(text/html); response.setBufferSize(8192); PrintWriter out = response.getWriter(); / Then write the response (Populate the header part of the response) out.println( + + messages.getString(TitleBookDescription) + ); / Get the dispatche

26、r; it gets the banner to the user RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(/banner); if (dispatcher != null) dispatcher.include(request, response); Penny Shen58 2021-8-16 / Get the identifier of the book to display (Get HTTP parameter) String bookId = request.getParame

27、ter(bookId); if (bookId != null) / and the information about the book (Perform business logic) try BookDetails bd = bookDB.getBookDetails(bookId); Currency c = (Currency)session.getAttribute(currency); if (c = null) c = new Currency(); c.setLocale(request.getLocale(); session.setAttribute(currency,

28、c); c.setAmount(bd.getPrice(); / Print out the information obtained out.println(.); catch (BookNotFoundException ex) response.resetBuffer(); throw new ServletException(ex); out.println(); out.close(); Penny Shen59 2021-8-16 nFill Response headers nSet some properties of the response Buffer size nGet

29、 an output stream object from the response nWrite body content to the output stream Penny Shen60 2021-8-16 Public class HelloServlet extends HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / Fill response headers response.s

30、etContentType(text/html); / Set buffer size response.setBufferSize(8192); / Get an output stream object from the response PrintWriter out = response.getWriter(); / Write body content to output stream out.println(First Servlet); out.println(Hello J2EE Programmers! ); Penny Shen61 2021-8-16 案例解析案例解析 P

31、enny Shen63 2021-8-16 nEnables sharing information among collaborating web components via attributes maintained in Scope objects Attributes are name/object pairs nAttributes maintained in the Scope objects are accessed with getAttribute() (session); HttpSessionHttpSession n上下文上下文(context)(context) S

32、ervletContextServletContext Penny Shen66 2021-8-16 ServletServlet請(qǐng)求響應(yīng)模型請(qǐng)求響應(yīng)模型 Servlet Response Request Browser HTTP WebWeb ServerServer Servlet Container Response Request Penny Shen67 2021-8-16 RequestServlet 1 Servlet 2 Servlet 3Response Web Server Penny Shen68 2021-8-16 nWhat is a request?What is

33、a request? Information that is sent from client to a Information that is sent from client to a serverserver nWho made the requestWho made the request nWhat user-entered data is sentWhat user-entered data is sent nWhich HTTP headers are sentWhich HTTP headers are sent nWhat is a response?What is a re

34、sponse? Information that is sent to client from a Information that is sent to client from a serverserver nText(html, plain) or binary(image) dataText(html, plain) or binary(image) data nHTTP headers, cookies, etcHTTP headers, cookies, etc Penny Shen69 2021-8-16 請(qǐng)求和響應(yīng)相關(guān)的方法和接口請(qǐng)求和響應(yīng)相關(guān)的方法和接口 nServletReq

35、uest:ServletRequest:代表了代表了ServletServlet的請(qǐng)求,的請(qǐng)求, HttpServletRequestHttpServletRequest是他的子接口;是他的子接口; nServletResponse:ServletResponse:代表了代表了ServletServlet的響應(yīng),的響應(yīng), HttpServletResponseHttpServletResponse是他的子接口是他的子接口 nServletInputStrean:ServletServletInputStrean:Servlet的輸入流類的輸入流類 nServletOutputStream:Se

36、rvletServletOutputStream:Servlet的輸出流類的輸出流類 nServletRequestWrapper: ServletRequestServletRequestWrapper: ServletRequest的實(shí)現(xiàn)的實(shí)現(xiàn) nServletResponseWrapper:ServletReponseServletResponseWrapper:ServletReponse的實(shí)現(xiàn)的實(shí)現(xiàn) nHttpServletRequest:HttpServletRequest:代表了代表了HttpHttp的請(qǐng)求,繼承了的請(qǐng)求,繼承了 ServletRequestServletRequ

37、est接口接口 nHttpServletResponse:HttpServletResponse:代表了代表了HttpHttp的響應(yīng),繼承了的響應(yīng),繼承了 ServletResponseServletResponse接口接口 nHttpServletRequestWrapper: HttpServletRequestWrapper: HttpServletRequestHttpServletRequest的實(shí)現(xiàn)的實(shí)現(xiàn) nHttpServletResponseWrapper: HttpServletResponseWrapper: HttpServletResponseHttpServletRe

38、sponse的實(shí)現(xiàn)的實(shí)現(xiàn) Penny Shen70 2021-8-16 requestrequest的主要用途的主要用途 n這個(gè)接口中最常用的方法就是獲得請(qǐng)求中的參這個(gè)接口中最常用的方法就是獲得請(qǐng)求中的參 數(shù),這個(gè)請(qǐng)求中的參數(shù)是客戶端表單中的數(shù)據(jù)。數(shù),這個(gè)請(qǐng)求中的參數(shù)是客戶端表單中的數(shù)據(jù)。 n也可以獲取客戶端正在使用的通信協(xié)議,產(chǎn)生也可以獲取客戶端正在使用的通信協(xié)議,產(chǎn)生 請(qǐng)求并且接受請(qǐng)求的遠(yuǎn)端主機(jī)名和其請(qǐng)求并且接受請(qǐng)求的遠(yuǎn)端主機(jī)名和其IPIP地址等地址等 信息;信息; Penny Shen71 2021-8-16 HttpServletRequestHttpServletRequest的常用

39、方法的常用方法 ngetParameterNames()getParameterNames() 獲取客戶提交的表單中所有參數(shù)名的集合獲取客戶提交的表單中所有參數(shù)名的集合 ngetParameter(ParamName)getParameter(ParamName) 獲取參數(shù)名為獲取參數(shù)名為ParamNameParamName的參數(shù)值,如果請(qǐng)求中沒(méi)有的參數(shù)值,如果請(qǐng)求中沒(méi)有 這個(gè)參數(shù),返回這個(gè)參數(shù),返回nullnull。 ngetParameterValues(ParamName)getParameterValues(ParamName) 獲取參數(shù)名為獲取參數(shù)名為ParamNameParamNa

40、me的參數(shù)值的集合,這個(gè)值往的參數(shù)值的集合,這個(gè)值往 往是往是checkboxcheckbox或者或者selectselect控件提交的,獲得的值是控件提交的,獲得的值是 一個(gè)一個(gè)StringString數(shù)組,如果請(qǐng)求中沒(méi)有這個(gè)參數(shù),返回?cái)?shù)組,如果請(qǐng)求中沒(méi)有這個(gè)參數(shù),返回 nullnull。 ngetInputStream()getInputStream() 得到一個(gè)輸入流用來(lái)讀取二進(jìn)制數(shù)據(jù)得到一個(gè)輸入流用來(lái)讀取二進(jìn)制數(shù)據(jù) Penny Shen72 2021-8-16 responseresponse的主要用途的主要用途 nresponseresponse的主要功能是服務(wù)器端用于向客戶端發(fā)的主

41、要功能是服務(wù)器端用于向客戶端發(fā) 送消息,例如送消息,例如HTTPHTTP的頭信息和的頭信息和CookieCookie信息等:信息等: 設(shè)置設(shè)置HTTPHTTP頭信息;頭信息; 重定向重定向: :客戶端跳轉(zhuǎn)客戶端跳轉(zhuǎn)(request(request信息會(huì)丟失信息會(huì)丟失) ); 設(shè)置設(shè)置Cookie;Cookie; 在在JSPJSP中重點(diǎn)闡述中重點(diǎn)闡述 Penny Shen73 2021-8-16 HttpServletResponseHttpServletResponse nsetContentType( text/html;charset=gb2312 ); setContentType( t

42、ext/html;charset=gb2312 ); 設(shè)置文檔輸出類型設(shè)置文檔輸出類型 nServletOutputStream out=response.getOutputStream();ServletOutputStream out=response.getOutputStream(); 獲取字節(jié)形式的輸出流獲取字節(jié)形式的輸出流 nPrintWriter out = response.getWriter();PrintWriter out = response.getWriter(); 獲取字符輸出流獲取字符輸出流 nsendRedirect(StringsendRedirect(Str

43、ing location);location); URLURL重定向,把響應(yīng)發(fā)送到另一頁(yè)面或者重定向,把響應(yīng)發(fā)送到另一頁(yè)面或者ServletServlet進(jìn)行處理;進(jìn)行處理; nencodeURL(String url);encodeURL(String url); 使用使用URLURL和一個(gè)和一個(gè)SessionIdSessionId重寫這個(gè)重寫這個(gè)URL;URL; nsetCharacterEncoding(String charset);setCharacterEncoding(String charset); 設(shè)置響應(yīng)的字符編碼類型。設(shè)置響應(yīng)的字符編碼類型。 Penny Shen74 2

44、021-8-16 表表 單單 處處 理理 login Penny Shen75 2021-8-16 案例解析案例解析 Penny Shen76 2021-8-16 Penny Shen77 2021-8-16 Penny Shen78 2021-8-16 Penny Shen79 2021-8-16 會(huì)話跟蹤會(huì)話跟蹤 Penny Shen81 2021-8-16 會(huì)話跟蹤 請(qǐng)求 1 請(qǐng)求 2 請(qǐng)求 n . 服務(wù)器將唯一的會(huì)話 ID 分 配給客戶端以跟蹤用戶。 ID由服務(wù)器產(chǎn)生,不同的 服務(wù)器的會(huì)話ID完全不同 Http是一種無(wú)狀態(tài)協(xié)議。也就是說(shuō),客是一種無(wú)狀態(tài)協(xié)議。也就是說(shuō),客 戶端在瀏覽器服

45、務(wù)器上的不同頁(yè)面時(shí),每次戶端在瀏覽器服務(wù)器上的不同頁(yè)面時(shí),每次 請(qǐng)求獲得相應(yīng)完成后服務(wù)器和客戶端的請(qǐng)求獲得相應(yīng)完成后服務(wù)器和客戶端的 Socket鏈接會(huì)關(guān)閉。但是在不同頁(yè)面之間,鏈接會(huì)關(guān)閉。但是在不同頁(yè)面之間, 我們往往需要交換信息,這就需要我們對(duì)用我們往往需要交換信息,這就需要我們對(duì)用 戶的會(huì)話進(jìn)行跟蹤戶的會(huì)話進(jìn)行跟蹤 Penny Shen82 2021-8-16 會(huì)話跟蹤技術(shù) 會(huì)話跟蹤技術(shù)會(huì)話跟蹤技術(shù) 用戶授權(quán)隱藏表單域URL 重寫Cookie q 以下是各種會(huì)話跟蹤技術(shù): Penny Shen83 2021-8-16 用戶授權(quán) q 會(huì)話跟蹤的用戶授權(quán)方法在用戶登錄之后跟蹤用戶。用戶 授權(quán)

46、技術(shù)不允許未經(jīng)授權(quán)的用戶訪問(wèn)網(wǎng)站上的某些資源 已授權(quán)用戶 未授權(quán)用戶 請(qǐng)求 請(qǐng)求 Penny Shen84 2021-8-16 URL 重寫 q URL(統(tǒng)一資源定位) 重寫技術(shù):當(dāng)客戶端不接受Cookie 的時(shí)候,可以使用URL重寫機(jī)制將一個(gè)唯一的會(huì)話 ID 添 加到 URL 結(jié)尾,以標(biāo)識(shí)該會(huì)話。Servlet容器解釋URL, 取出SessionID,根據(jù)SessionID將請(qǐng)求與特定的Session 關(guān)聯(lián) 原始 URL:http:/server:post/servlet/Rewritten 用附加信息重寫的 URL: http:/server:post/servlet/Rewritten/

47、10 用添加的參數(shù)重寫的 URL: http:/server:post/servlet/Rewritten?sessionid=10 用自定義修改重寫的 URL: http:/server:post/servlet/Rewritten;$sessionid$10 Penny Shen85 2021-8-16 隱藏表單域 n將會(huì)話跟蹤字段的隱藏表單域添加到 HTML 頁(yè) 面,但是不會(huì)顯示在客戶端瀏覽器中 在 GET 和 POST 方法中指定用于存儲(chǔ)有關(guān)會(huì)話消息的名稱和值。 Penny Shen86 2021-8-16 什么是Cookie n“Cookie”來(lái)自于英文,原意是“小甜餅”。維基百科上

48、的 定義是:“Cookie,有時(shí)也用其復(fù)數(shù)形式Cookies,指某 些網(wǎng)站為了辨別用戶身份而儲(chǔ)存在用戶本地終端上的數(shù) 據(jù)”。簡(jiǎn)單來(lái)說(shuō),Cookie就是服務(wù)器暫存放在瀏覽者電腦 里的資料,好讓服務(wù)器用來(lái)辨認(rèn)其計(jì)算機(jī)。在瀏覽網(wǎng)站的 時(shí)候,Web服務(wù)器會(huì)先送一小小資料放在訪問(wèn)者的計(jì)算機(jī) 上,這段資料就是Cookie。當(dāng)下次用戶再訪問(wèn)同一個(gè)網(wǎng)站, Web服務(wù)器會(huì)先看看有沒(méi)有其上次留下的Cookie資料,有 的話,就會(huì)依據(jù)Cookie里的內(nèi)容來(lái)判斷使用者,送出特定 的網(wǎng)頁(yè)內(nèi)容。 nCookie看上去很復(fù)雜,那么其存在的價(jià)值是什么?為什么 Web服務(wù)器不可以直接識(shí)別用戶的電腦?要理解這些,必 須先理解HT

49、TP協(xié)議。 Penny Shen87 2021-8-16 Cookie的工作原理的工作原理 p客戶的客戶的HTTP 請(qǐng)求到達(dá)服務(wù)器。請(qǐng)求到達(dá)服務(wù)器。 p服務(wù)器創(chuàng)建服務(wù)器創(chuàng)建Cookie,并作為響應(yīng)頭域的一部分返,并作為響應(yīng)頭域的一部分返 回用戶?;赜脩?。 p瀏覽器收到包含瀏覽器收到包含Cookie 的響應(yīng)后,會(huì)把的響應(yīng)后,會(huì)把Cookie 的內(nèi)容用的內(nèi)容用“關(guān)鍵字關(guān)鍵字/值值” 對(duì)的形式寫入到一個(gè)客戶對(duì)的形式寫入到一個(gè)客戶 端專為存放端專為存放Cookie 的的文本文件中文本文件中。 p瀏覽器會(huì)把瀏覽器會(huì)把Cookie 及隨后產(chǎn)生的請(qǐng)求發(fā)給相同的及隨后產(chǎn)生的請(qǐng)求發(fā)給相同的 服務(wù)器,服務(wù)器, p

50、服務(wù)器可以再次讀取服務(wù)器可以再次讀取Cookie 中存放的中存放的Cookie Penny Shen88 2021-8-16 Cookie 也是會(huì)話跟蹤的最常用方法,也是會(huì)話跟蹤的最常用方法,Cookie是是Web 服務(wù)器發(fā)送給服務(wù)器發(fā)送給Web瀏覽器的內(nèi)容較小的純文本信息,瀏覽器的內(nèi)容較小的純文本信息, 以文本文件的形式存放在客戶端的計(jì)算機(jī)中,這些以文本文件的形式存放在客戶端的計(jì)算機(jī)中,這些 Cookie一般用于保存用戶的訪問(wèn)狀態(tài),當(dāng)用戶再次訪一般用于保存用戶的訪問(wèn)狀態(tài),當(dāng)用戶再次訪 問(wèn)同一個(gè)網(wǎng)站的時(shí)候,瀏覽器會(huì)將這些問(wèn)同一個(gè)網(wǎng)站的時(shí)候,瀏覽器會(huì)將這些Cookie信息發(fā)信息發(fā) 送回服務(wù)器,使

51、得服務(wù)器能夠恢復(fù)用戶上一次的訪問(wèn)送回服務(wù)器,使得服務(wù)器能夠恢復(fù)用戶上一次的訪問(wèn) 狀態(tài)。使用狀態(tài)。使用Cookie的根本目的是為了的根本目的是為了在用戶訪問(wèn)期間在用戶訪問(wèn)期間 實(shí)現(xiàn)不同頁(yè)面之間的數(shù)據(jù)傳輸,以解決實(shí)現(xiàn)不同頁(yè)面之間的數(shù)據(jù)傳輸,以解決HTTP無(wú)狀態(tài)的無(wú)狀態(tài)的 問(wèn)題。問(wèn)題。 Servlet API 提供了一個(gè)提供了一個(gè)Cookie 類,封裝了對(duì)類,封裝了對(duì)Cookie 的一些操作。的一些操作。Servlet 可以創(chuàng)建一個(gè)新的可以創(chuàng)建一個(gè)新的Cookie,設(shè),設(shè) 置它的關(guān)鍵字、值及有效期等屬性,然后把置它的關(guān)鍵字、值及有效期等屬性,然后把Cookie 設(shè)設(shè) 置在置在HttpServletR

52、esponse 對(duì)象中發(fā)回瀏覽器。對(duì)象中發(fā)回瀏覽器。 Cookie Penny Shen89 2021-8-16 Cookie q Cookie 用于存儲(chǔ) Web 服務(wù)器發(fā)送給客戶端的信息。服務(wù) 器通過(guò)設(shè)置響應(yīng)標(biāo)題中的 Set-Cookie 方法來(lái)發(fā)送 cookie。 其語(yǔ)法如下: Set-Cookie: Name=VALUE; Comment=COMMENT; Domain=DOMAINNAME; Max-age=SECONDS; Path=PATH; secure; Name 指定 Cookie 的名稱 VALUE 指定 Cookie 名稱的值 Max-age 指定 Cookie 的生命周

53、期(以秒表示) Domain 指定 Cookie 有效的 URL secure HTTP 指定 Cookie 是否可以在 HTTP 上交換 Penny Shen90 2021-8-16 Cookie q javax.servlet.http.cookies 包提供由 Cookie 類實(shí)現(xiàn)的多種 方法 public int getMaxAge(); 返回Cookie 的最大保存時(shí)間 public void setMaxAge(int age); 設(shè)置 cookie 的最大生命周期(以秒表示) Penny Shen91 2021-8-16 Cookie的生成的生成 1。創(chuàng)建Cookie對(duì)象 調(diào)用C

54、ookie的構(gòu)造函數(shù),給出Cookie的名稱及Cookie的值。 二者都為字符串 Cookie c = new Cookie(userinfo, zhang); 2。設(shè)置最大時(shí)效 c.setMaxAge(60*60*24*365); 3。將Cookie發(fā)送給客戶端發(fā)送給客戶端 response.addCookie(c); Penny Shen92 2021-8-16 生成生成Cookie實(shí)例實(shí)例 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,

55、 IOException PrintWriter out = response.getWriter(); Cookie c=new Cookie(repeatUser,yes); c.setMaxAge(60*60*24*7); response.addCookie(c); String title=HI,you have created a Cookie,name=repeaterUser,value=yes; out.print(title); Penny Shen93 2021-8-16 從客戶端讀取從客戶端讀取Cookie時(shí)調(diào)用的是時(shí)調(diào)用的是HttpServletRequest的的 g

56、etCookies方法。該方法返回一個(gè)與方法。該方法返回一個(gè)與HTTP請(qǐng)求頭中的內(nèi)請(qǐng)求頭中的內(nèi) 容對(duì)應(yīng)的容對(duì)應(yīng)的Cookie對(duì)象數(shù)組。得到這個(gè)數(shù)組之后,一般是用對(duì)象數(shù)組。得到這個(gè)數(shù)組之后,一般是用 循環(huán)訪問(wèn)其中的各個(gè)元素,調(diào)用循環(huán)訪問(wèn)其中的各個(gè)元素,調(diào)用getName檢查各個(gè)檢查各個(gè) Cookie的名字,直至找到目標(biāo)的名字,直至找到目標(biāo)Cookie。然后對(duì)這個(gè)目標(biāo)。然后對(duì)這個(gè)目標(biāo) Cookie調(diào)用調(diào)用getValue,根據(jù)獲得的結(jié)果進(jìn)行其他處理。,根據(jù)獲得的結(jié)果進(jìn)行其他處理。 Cookie獲取獲取 Penny Shen94 2021-8-16 Cookie cookies=resquest.g

57、etCookies(); If(cookies!=nullicookies.length;i+) Cookie cookie=cookiesi; String cookieName=cookie.getName(); if(cookieName.equals( “userinfo”) dosomethingwith() Penny Shen95 2021-8-16 Penny Shen96 2021-8-16 Penny Shen97 2021-8-16 Cookie的優(yōu)點(diǎn)和限制 nCookie協(xié)助HTTP協(xié)議,使得客戶端和服務(wù)器之間的通信 有了“狀態(tài)”,給用戶和網(wǎng)站帶來(lái)了很多的好處。舉例來(lái)

58、說(shuō),Cookie可以實(shí)現(xiàn)很多非常實(shí)用的應(yīng)用: Cookie能使站點(diǎn)跟蹤特定訪問(wèn)者的訪問(wèn)次數(shù)、最后訪問(wèn)時(shí)間和訪 問(wèn)者進(jìn)入站點(diǎn)的路徑。 Cookie能告訴在線廣告商廣告被點(diǎn)擊的次數(shù) ,從而可以更精確的 投放廣告。 Cookie有效期限未到時(shí),Cookie能使用戶在不鍵入密碼和用戶名 的情況下進(jìn)入曾經(jīng)瀏覽過(guò)的一些站點(diǎn)。 Cookie能幫助站點(diǎn)統(tǒng)計(jì)用戶個(gè)人資料以實(shí)現(xiàn)各種各樣的個(gè)性化服 務(wù)。 根據(jù)用戶的不同定制網(wǎng)站根據(jù)用戶的不同定制網(wǎng)站 Penny Shen98 2021-8-16 其中,其中,uid=1008public String getId(); 2.2.public boolean isNew

59、();public boolean isNew(); 3.3.Public void invalidate();Public void invalidate(); 4.4.public void setAttribute(String key,Object obj);public void setAttribute(String key,Object obj); 5.5.public object getAttribute(String key);public object getAttribute(String key); 6.6.public long getCreationTime();

60、public long getCreationTime(); 7.7.public long getLastAccessedTime();public long getLastAccessedTime(); 8.8.public Enumeration getAttributeNames();public Enumeration getAttributeNames(); 9.9.public int getMaxInactiveInterval();public int getMaxInactiveInterval(); 10.10.public void setMaxInactiveInte

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論