版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第三章 Servlet 編程1本章主要內(nèi)容Servlet 基礎(chǔ)Servlet 編程基本技能Servlet 編程高級(jí)技能Servlet Filter 編程Servlet Listener 編程2Servlet 基礎(chǔ)Servlet 編程基本技能Servlet 編程高級(jí)技能Servlet Filter 編程Servlet Listener 編程3Servlet 基礎(chǔ)Servlet定義:一種獨(dú)立于操作系統(tǒng)平臺(tái)和網(wǎng)絡(luò)傳輸協(xié)議的服務(wù)器端的Java應(yīng)用程序,它用來擴(kuò)展服務(wù)器的功能,可以生成動(dòng)態(tài)的Web頁面 注意:Servlet不是從命令行啟動(dòng)的,而是由包含Java虛擬機(jī)的Web服務(wù)器進(jìn)行加載 4Servle
2、t 基礎(chǔ)Servlet 與 Applet 相比較 :相似之處:(1)它們不是獨(dú)立的應(yīng)用程序,沒有main()方法。(2)它們不是由用戶調(diào)用,而是由另外一個(gè)應(yīng)用程序(容器)調(diào)用。(3)它們都有一個(gè)生存周期,包含init()和destroy()方法。不同之處:(1)Applet運(yùn)行在客戶端,具有豐富的圖形界面。(2)Servlet運(yùn)行在服務(wù)器端,沒有圖形界面。5Servlet 最大的用途是通過動(dòng)態(tài)響應(yīng)客戶機(jī)請(qǐng)求來擴(kuò)展服務(wù)器功能!Servlet 基礎(chǔ)6Servlet 基礎(chǔ)Servlet工作原理 :Servlet運(yùn)行在Web服務(wù)器上的Web容器里。Web容器負(fù)責(zé)管理Servlet。它裝入并初始化Ser
3、vlet,管理Servlet的多個(gè)實(shí)例,并充當(dāng)請(qǐng)求調(diào)度器,將客戶端的請(qǐng)求傳遞到Servlet,并將Servlet的響應(yīng)返回給客戶端。Web容器在 Servlet 的使用期限結(jié)束時(shí)終結(jié)該 Servlet。服務(wù)器關(guān)閉時(shí),Web容器會(huì)從內(nèi)存中卸載和除去 Servlet 7Servlet 基礎(chǔ)Servlet基本工作流程 (1)客戶機(jī)將請(qǐng)求發(fā)送到服務(wù)器。 (2)服務(wù)器上的Web容器實(shí)例化(裝入)Servlet(3)Web容器將請(qǐng)求信息發(fā)送到 Servlet。 (4)Servlet 創(chuàng)建一個(gè)響應(yīng),并將其返回到Web容器。(5)Web容器將響應(yīng)發(fā)回客戶機(jī)。(6)服務(wù)器關(guān)閉或Servlet空閑時(shí)間超過一定限度
4、時(shí),調(diào)用destory()方法退出。8Servlet 基礎(chǔ)注意:Servlet也可以配置為Web應(yīng)用程序啟動(dòng)時(shí)自動(dòng)裝載。結(jié)論:客戶機(jī)與Servlet間沒有直接的交互。無論客戶機(jī)對(duì)Servlet的請(qǐng)求還是Servlet對(duì)客戶端的響應(yīng),都是通過Web服務(wù)器來實(shí)現(xiàn)的,這就大大提高了Servlet組件的可移植性 9Servlet 基礎(chǔ)多個(gè)請(qǐng)求的處理 :為每個(gè)請(qǐng)求創(chuàng)建一個(gè)新的線程來處理客戶端的請(qǐng)求 多個(gè)線程可能嘗試訪問同一數(shù)據(jù)塊,需要注意同步??梢詫?shí)現(xiàn)SingleThreadModel,避免多線程的并發(fā)訪問。10Servlet 編程接口 Servlet 基礎(chǔ)11Servlet 基礎(chǔ)Servlet 編程基
5、本技能Servlet 編程高級(jí)技能Servlet Filter 編程Servlet Listener 編程12編寫響應(yīng)HTTP請(qǐng)求的Servlet的步驟: (1)創(chuàng)建一個(gè)擴(kuò)展了 javax.servlet.http.HttpServlet接口的Servlet類。 (2)重寫doGet()或doPost()方法實(shí)現(xiàn)對(duì)HTTP請(qǐng)求信息的動(dòng)態(tài)響應(yīng)。 Servlet 編程基本技能13程序31:FirstServlet.javapackage com.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.se
6、rvlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class FirstServlet extends HttpServlet protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,
7、 IOException /TODO Method stub generated by Lomboz response.setContentType(text/html); PrintWriter out = response.getWriter(); out.println( + + + + First Servlet + + + + + +Hello World! + + + + );14利用Eclipse快速修正錯(cuò)誤 1516程序32:Web.xml !- - !- MyServlet - !- examples.MyServlet - !- - !- myparam - !- 1234
8、5 - !- - !- - !- - !- MyServlet - !- /mine/* - !- - First com.servlet.FirstServlet First /first welcome- 404 /error.jsp 17處理客戶端輸入 Servlet 編程基本技能在Web程序設(shè)計(jì)中,以表單方式向服務(wù)器提交數(shù)據(jù)是最常見的方法。表單數(shù)據(jù)的提交方法有兩種:Post方法和Get方法,當(dāng)使用Post方法時(shí),數(shù)據(jù)由標(biāo)準(zhǔn)的輸入設(shè)備讀入。當(dāng)使用Get方法時(shí),數(shù)據(jù)即附加在請(qǐng)求地址的后面,在瀏覽器的地址欄中可以看到。用戶只要簡單的調(diào)用HttpServletRequest的getParame
9、ter()方法,給出變量名稱即可取得該變量的值。需要注意的是,變量的名稱是大小寫敏感的。當(dāng)請(qǐng)求的變量不存在時(shí),將會(huì)返回一個(gè)空字符串 Eg: GetPostData.java (url/loing.html url/GetPostData)MultiChoiceServlet.java (url/multichoice url/multiChoice.html)18發(fā)送非HTML文檔 Servlet 編程基本技能隨著Internet應(yīng)用的不斷擴(kuò)展,為了支持多媒體數(shù)據(jù)類型,HTTP協(xié)議中就使用了附加在文檔之前的MIME數(shù)據(jù)類型信息來標(biāo)識(shí)數(shù)據(jù)類型MIME:Multipurpose Internet
10、Mail Extensions,多用途因特網(wǎng)郵件擴(kuò)展 Servlets 可以使用 MIME 類型來向?yàn)g覽器傳送非 HTML 文件,比如 Adobe PDF 和 Micorsoft Word。使用正確的 MIME 類型能夠保證這些非 HTML 文件被正確的插件或外部程序處理顯示。解決方案:需要將 response 對(duì)象中header 的 content 類型設(shè)置成 相應(yīng)的MIME標(biāo)志19發(fā)送非HTML文檔 Servlet 編程基本技能例如:發(fā)送PDF文檔res.setContentType(“application/pdf”)Eg:PDFServlet.java (url/pdfshow) 發(fā)送
11、word文檔:res.setContentType(“application/msword”)發(fā)送Excel文檔 res.setContentType(“application/vnd.ms-excel”)調(diào)用response的setHeader( ),設(shè)置應(yīng)答頭,可以避免在瀏覽器中直接打開文檔,而要求用戶下載保存文檔。res.setHeader(“Content-disposition”,”attachment;”);20response.setContentType() ;* = application/octet-stream .001 = application/x-001 .301
12、= application/x-301 .323 = text/h323 .906 = application/x-906 .907 = drawing/907 .a11 = application/x-a11 .acp = audio/x-mei-aac .ai = application/postscript .aif = audio/aiff .aifc = audio/aiff .aiff = audio/aiff .anv = application/x-anv .asa = text/asa .asf = video/x-ms-asf .asp = text/asp .asx =
13、video/x-ms-asf .au = audio/basic .avi = video/avi .awf = application/vnd.adobe.workflow .biz = text/xml .bmp = application/x-bmp .bot = application/x-bot .c4t = application/x-c4t .c90 = application/x-c90 .cal = application/x-cals .cat = application/vnd.ms-pki.seccat .cdf = application/x-netcdf .cdr
14、= application/x-cdr .cel = application/x-cel .cer = application/x-x509-ca-cert .cg4 = application/x-g4 .cgm = application/x-cgm .cit = application/x-cit .class = java/* .cml = text/xml :客戶端瀏覽器按XML格式進(jìn)行解析文檔 .cmp = application/x-cmp .cmx = application/x-cmx .cot = application/x-cot .crl = application/p
15、kix-crl .crt = application/x-x509-ca-cert .csi = application/x-csi .css = text/css :客戶端瀏覽器按CSS格式進(jìn)行解析文檔 .cut = application/x-cut .dbf = application/x-dbf .dbm = application/x-dbm .dbx = application/x-dbx .dcd = text/xml :客戶端瀏覽器按XML格式進(jìn)行解析文檔 .dcx = application/x-dcx .der = application/x-x509-ca-cert .d
16、gn = application/x-dgn .dib = application/x-dib .dll = application/x-msdownload .doc = application/msword .dot = application/msword .drw = application/x-drw .dtd = text/xml :客戶端瀏覽器按XML格式進(jìn)行解析文檔 .dwf = Model/vnd.dwf .dwf = application/x-dwf .dwg = application/x-dwg .dxb = application/x-dxb .dxf = appl
17、ication/x-dxf .edn = application/vnd.adobe.edn .emf = application/x-emf .eml = message/rfc822 .ent = text/xml :客戶端瀏覽器按XML格式進(jìn)行解析文檔 .epi = application/x-epi .eps = application/x-ps .eps = application/postscript .etd = application/x-ebx .exe = application/x-msdownload .fax = image/fax .fdf = applicatio
18、n/vnd.fdf .fif = application/fractals .fo = text/xml :客戶端瀏覽器按XML格式進(jìn)行解析文檔 .frm = application/x-frm .g4 = application/x-g4 .gbr = application/x-gbr .gcd = application/x-gcd .gif = image/gif .gl2 = application/x-gl2 .gp4 = application/x-gp4 .hgl = application/x-hgl .hmr = application/x-hmr .hpg = appli
19、cation/x-hpgl .hpl = application/x-hpl .hqx = application/mac-binhex40 .hrf = application/x-hrf .hta = application/hta .htc = text/x-component .htm = text/html :客戶端瀏覽器按超文本格式進(jìn)行解析文檔 .html = text/html :客戶端瀏覽器按超文本格式進(jìn)行解析文檔 .htt = text/webviewhtml .htx = text/html :客戶端瀏覽器按超文本格式進(jìn)行解析文檔 .icb = application/x-
20、icb .ico = image/x-icon .ico = application/x-ico .iff = application/x-iff .ig4 = application/x-g4 .igs = application/x-igs .iii = application/x-iphone .img = application/x-img .ins = application/x-internet-signup .isp = application/x-internet-signup .IVF = video/x-ivf .java = java/* .jfif = image/jp
21、eg .jpe = image/jpeg .jpe = application/x-jpe .jpeg = image/jpeg .jpg = image/jpeg .jpg = application/x-jpg .js = application/x-javascript .jsp = text/html :客戶端瀏覽器按超文本格式進(jìn)行解析文檔 .la1 = audio/x-liquid-file .lar = application/x-laplayer-reg .latex = application/x-latex .lavs = audio/x-liquid-secure .lbm
22、 = application/x-lbm .lmsff = audio/x-la-lms .ls = application/x-javascript .ltr = application/x-ltr .m1v = video/x-mpeg .m2v = video/x-mpeg .m3u = audio/mpegurl .m4e = video/mpeg4 .mac = application/x-mac .man = application/x-troff-man .math = text/xml .mdb = application/msaccess .mdb = application
23、/x-mdb .mfp = application/x-shockwave-flash .mht = message/rfc822 .mhtml = message/rfc822 .mi = application/x-mi .mid = audio/mid .midi = audio/mid .mil = application/x-mil .mml = text/xml .mnd = audio/x-musicnet-download .mns = audio/x-musicnet-stream .mocha = application/x-javascript .movie = vide
24、o/x-sgi-movie .mp1 = audio/mp1 .mp2 = audio/mp2 .mp2v = video/mpeg .mp3 = audio/mp3 .mp4 = video/mpeg4 .mpa = video/x-mpg .mpd = application/vnd.ms-project .mpe = video/x-mpeg .mpeg = video/mpg .mpg = video/mpg .mpga = audio/rn-mpeg .mpp = application/vnd.ms-project .mps = video/x-mpeg .mpt = applic
25、ation/vnd.ms-project .mpv = video/mpg .mpv2 = video/mpeg .mpw = application/vnd.ms-project .mpx = application/vnd.ms-project .mtx = text/xml .mxp = application/x-mmxp .net = image/pnetvue .nrf = application/x-nrf .nws = message/rfc822 .odc = text/x-ms-odc .out = application/x-out .p10 = application/
26、pkcs10 .p12 = application/x-pkcs12 .p7b = application/x-pkcs7-certificates .p7c = application/pkcs7-mime .p7m = application/pkcs7-mime .p7r = application/x-pkcs7-certreqresp .p7s = application/pkcs7-signature .pc5 = application/x-pc5 .pci = application/x-pci .pcl = application/x-pcl .pcx = applicati
27、on/x-pcx .pdf = application/pdf :客戶端瀏覽器按PDF格式進(jìn)行解析文檔 .pdf = application/pdf .pdx = application/vnd.adobe.pdx .pfx = application/x-pkcs12 .pgl = application/x-pgl .pic = application/x-pic .pko = application/vnd.ms-pki.pko .pl = application/x-perl .plg = text/html .pls = audio/scpls .plt = application/
28、x-plt .png = image/png .png = application/x-png .pot = application/vnd.ms-powerpoint .ppa = application/vnd.ms-powerpoint .ppm = application/x-ppm .pps = application/vnd.ms-powerpoint .ppt = application/vnd.ms-powerpoint .ppt = application/x-ppt .pr = application/x-pr .prf = application/pics-rules .
29、prn = application/x-prn .prt = application/x-prt .ps = application/x-ps .ps = application/postscript .ptn = application/x-ptn .pwz = application/vnd.ms-powerpoint .r3t = text/vnd.rn-realtext3d .ra = audio/vnd.rn-realaudio .ram = audio/x-pn-realaudio .ras = application/x-ras .rat = application/rat-fi
30、le .rdf = text/xml .rec = application/vnd.rn-recording .red = application/x-red .rgb = application/x-rgb .rjs = application/vnd.rn-realsystem-rjs .rjt = application/vnd.rn-realsystem-rjt .rlc = application/x-rlc .rle = application/x-rle .rm = application/vnd.rn-realmedia .rmf = application/vnd.adobe
31、.rmf .rmi = audio/mid .rmj = application/vnd.rn-realsystem-rmj .rmm = audio/x-pn-realaudio .rmp = application/vnd.rn-rn_music_package .rms = application/vnd.rn-realmedia-secure .rmvb = application/vnd.rn-realmedia-vbr .rmx = application/vnd.rn-realsystem-rmx .rnx = application/vnd.rn-realplayer .rp
32、= image/vnd.rn-realpix .rpm = audio/x-pn-realaudio-plugin .rsml = application/vnd.rn-rsml .rt = text/vnd.rn-realtext .rtf = application/msword .rtf = application/x-rtf .rv = video/vnd.rn-realvideo .sam = application/x-sam .sat = application/x-sat .sdp = application/sdp .sdw = application/x-sdw .sit
33、= application/x-stuffit .slb = application/x-slb .sld = application/x-sld .slk = drawing/x-slk .smi = application/smil .smil = application/smil .smk = application/x-smk .snd = audio/basic .sol = text/plain .sor = text/plain .spc = application/x-pkcs7-certificates .spl = application/futuresplash .spp
34、 = text/xml .ssm = application/streamingmedia .sst = application/vnd.ms-pki.certstore .stl = application/vnd.ms-pki.stl .stm = text/html .sty = application/x-sty .svg = text/xml .swf = application/x-shockwave-flash :客戶端瀏覽器按 Flash 格式進(jìn)行解析文檔 .tdf = application/x-tdf .tg4 = application/x-tg4 .tga = appl
35、ication/x-tga .tif = image/tiff .tif = application/x-tif .tiff = image/tiff .tld = text/xml .top = drawing/x-top .torrent = application/x-bittorrent .tsd = text/xml .txt = text/plain :客戶端瀏覽器按 純文本 格式進(jìn)行解析文檔 .uin = application/x-icq .uls = text/iuls .vcf = text/x-vcard .vda = application/x-vda .vdx = a
36、pplication/vnd.visio .vml = text/xml .vpg = application/x-vpeg005 .vsd = application/vnd.visio .vsd = application/x-vsd .vss = application/vnd.visio .vst = application/vnd.visio .vst = application/x-vst .vsw = application/vnd.visio .vsx = application/vnd.visio .vtx = application/vnd.visio .vxml = te
37、xt/xml .wav = audio/wav .wax = audio/x-ms-wax .wb1 = application/x-wb1 .wb2 = application/x-wb2 .wb3 = application/x-wb3 .wbmp = image/vnd. .wiz = application/msword .wk3 = application/x-wk3 .wk4 = application/x-wk4 .wkq = application/x-wkq .wks = application/x-wks .wm = video/x-ms-wm .wma = audio/x
38、-ms-wma .wmd = application/x-ms-wmd .wmf = application/x-wmf .wml = text/vnd. .wmv = video/x-ms-wmv .wmx = video/x-ms-wmx .wmz = application/x-ms-wmz .wp6 = application/x-wp6 .wpd = application/x-wpd .wpg = application/x-wpg .wpl = application/vnd.ms-wpl .wq1 = application/x-wq1 .wr1 = application/x
39、-wr1 .wri = application/x-wri .wrk = application/x-wrk .ws = application/x-ws .ws2 = application/x-ws .wsc = text/scriptlet .wsdl = text/xml .wvx = video/x-ms-wvx .xdp = application/vnd.adobe.xdp .xdr = text/xml .xfd = application/vnd.adobe.xfd .xfdf = application/vnd.adobe.xfdf .xhtml = text/html .
40、xls = application/vnd.ms-excel .xls = application/x-xls .xlw = application/x-xlw .xml = text/xml .xpl = audio/scpls .xq = text/xml .xql = text/xml .xquery = text/xml .xsd = text/xml .xsl = text/xml .xslt = text/xml .xwd = application/x-xwd .x_b = application/x-x_b .x_t = application/x-x_t21Servlet 編
41、程基本技能Servlet配置參數(shù)由ServletConfig對(duì)象表示。在Servlet被實(shí)例化后,ServletConfig對(duì)象對(duì)任何客戶端在任何時(shí)候訪問都有效,但一個(gè)Servlet的ServletConfig對(duì)象不能被其他Servlet訪問。使用ServletConfig的getInitParameter(String)方法,獲得初始化參數(shù)在Servlet中要得到ServletConfig對(duì)象有兩種方法: 在init()方法中獲?。ㄍㄟ^init的重載方法傳遞); 通過getServletConfig()方法直接獲取。 Eg:InitParamServlet (url/InitParamSer
42、vlet)Eg:ConfigParamServlet 用途:通過配置信息來初始化Servlet可以有效避免硬編碼信息,提高Servlet的可移植性。22Servlet 基礎(chǔ)Servlet 編程基本技能Servlet 編程高級(jí)技能Servlet Filter 編程Servlet Listener 編程23Servlet 編程高級(jí)技能HTTP特征:HTTP協(xié)議是一種無狀態(tài)的協(xié)議,客戶端每次打開一個(gè)Web 頁面,它就會(huì)與服務(wù)器建立一個(gè)新的連接,發(fā)送一個(gè)新的請(qǐng)求到服務(wù)器,服務(wù)器處理客戶端的請(qǐng)求,返回響應(yīng)到客戶端,并關(guān)閉與客戶端建立的連接。當(dāng)客戶端發(fā)起新的請(qǐng)求,那么它重新與服務(wù)器建立連接,因此服務(wù)器并不
43、記錄關(guān)于客戶的任何信息。 24Servlet 編程高級(jí)技能常見會(huì)話跟蹤技術(shù)有:Cookie、URL重寫隱藏表單域25Servlet 編程高級(jí)技能Cookie是一小塊可以嵌入到Http請(qǐng)求和響應(yīng)中的數(shù)據(jù)。典型情況下,Web服務(wù)器將Cookie值嵌入到響應(yīng)的首部,而瀏覽器則在其后的請(qǐng)求中都將攜帶同樣的Cookie。Cookie的信息中可以有一部分用來存儲(chǔ)會(huì)話ID,該會(huì)話ID被服務(wù)器用來將某些HTTP請(qǐng)求綁定在會(huì)話中。Cookie由瀏覽器保存窄客戶端,通常為一個(gè)文本文件。使用Cookie進(jìn)行會(huì)話跟蹤 26Servlet 編程高級(jí)技能request.getCookies( );獲得當(dāng)前請(qǐng)求中的Cook
44、ie對(duì)象的一個(gè)數(shù)組通過Cookie對(duì)象的getName( )方法獲得Cookie的標(biāo)識(shí)通過Cookie對(duì)象的getValue( )獲得Cookie相關(guān)信息response.addCookie( )能將Cookie加入到response對(duì)象并返回客戶端,以后客戶端的對(duì)服務(wù)端的請(qǐng)求都將攜帶此CookieEg:CookieServlet (url/Cookie)使用Cookie進(jìn)行會(huì)話跟蹤 27Servlet 編程高級(jí)技能在Cookie中添加與會(huì)話相關(guān)的信息,實(shí)現(xiàn)會(huì)話跟蹤Eg:CookieTraceServlet使用Cookie進(jìn)行會(huì)話跟蹤 28Servlet 編程高級(jí)技能URL重寫通過窄URL地址
45、后面增加一個(gè)包含會(huì)話信息的字符串來記錄會(huì)話信息。URl地址與會(huì)話信息串之間用”?”分隔,如果有多個(gè)參數(shù),用“&”分隔。Eg:URLRewrite1 (url/url1)URLRewrite2 (url/url2)使用URL重寫跟蹤會(huì)話29Servlet 編程高級(jí)技能HttpSession 接口允許 Servlet查看和管理關(guān)于會(huì)話的信息,確保信息持續(xù)跨越多個(gè)用戶連接等。HttpSession高級(jí)接口實(shí)際上是建立在 Cookie 和 URL重寫這兩種會(huì)話跟蹤技術(shù)之上的,只不過由Servlet自動(dòng)實(shí)現(xiàn)了關(guān)于會(huì)話跟蹤的一切,不再需要程序員了解具體細(xì)節(jié)。30Servlet 編程高級(jí)技能使用 HttpS
46、ession 接口進(jìn)行程序開發(fā)的基本步驟是: (1)獲取會(huì)話對(duì)象HttpSession (2)對(duì)會(huì)話對(duì)象進(jìn)行讀或?qū)?(3)手工終止會(huì)話,或者什么也不做,讓它自動(dòng)終止。每個(gè)會(huì)話對(duì)象都有一定的生存周期,超過這個(gè)周期,容器將自動(dòng)將會(huì)話對(duì)象中止。31Servlet 編程高級(jí)技能HttpSession接口常用方法isNew( ):檢測(cè)當(dāng)前客戶會(huì)話是否為新的會(huì)話getId( ):返回分配給session的唯一標(biāo)識(shí)字符串setAttribute( ):將數(shù)據(jù)以某個(gè)名稱綁定到sessioingetAttribute( ):從session中取得數(shù)據(jù)setMaxInactiveInterval( ):sessi
47、on的有效期,秒為單位removeAttribute( ):從session中刪除數(shù)據(jù)invalidate( ):終止當(dāng)前會(huì)話Eg:HitCounter (url/HitCounter)Eg:CatalogServlet (url/catalog) CartServlet (url/cart)使用HttpSession對(duì)象跟蹤會(huì)話32Servlet 編程高級(jí)技能上下文:服務(wù)器上的每個(gè)Web應(yīng)用都會(huì)有一個(gè)背景環(huán)境對(duì)象。上下文對(duì)象提供了一個(gè)同一Web應(yīng)用內(nèi)的不同資源間共享信息的場(chǎng)所。Javax.Servlet.ServletContext接口提供正在運(yùn)行的Servlet所處的Web應(yīng)用程序的上下文
48、對(duì)象的視圖,可以通過getServletContext()方法得到該Servlet運(yùn)行的上下文對(duì)象。 ServletContext33Servlet 編程高級(jí)技能通過Servlet 上下文可以實(shí)現(xiàn)以下功能:(1)訪問Web應(yīng)用程序資源(2)在Servlet上下文屬性中保存Web應(yīng)用程序信息(3)獲取初始化參數(shù)信息(4)提供日志支持。Eg:AdminTemperatureServlet url/adminTemperatureShowTempteratureServlet url/showTemperature34Web容器接收到客戶端的請(qǐng)求,負(fù)責(zé)創(chuàng)建request對(duì)象和response對(duì)象,然
49、后將二者以參數(shù)形式傳遞給與請(qǐng)求URL相關(guān)聯(lián)的Servlet的service( )方法進(jìn)行出來。請(qǐng)求指派:對(duì)于負(fù)責(zé)的請(qǐng)求,在一個(gè)Servlet處理完畢后,將客戶端的請(qǐng)求傳遞到另外一個(gè)Servlet來處理。javax.servlet.Requestdispatcher接口 forward( )方法 將請(qǐng)求傳遞到其他資源include( )方法 將其他資源對(duì)同一請(qǐng)求的響應(yīng)包含進(jìn)來Eg:Main.java LoginSuccess url/LoginSuccess LoginFail url/LoginFailServlet 編程高級(jí)技能35Servlet 基礎(chǔ)Servlet 編程基本技能Servle
50、t 編程高級(jí)技能Servlet Filter 編程Servlet Listener 編程36Java Servlet 2.3 規(guī)范新特性:Servlet FilterServlet Filter 編程注意:Filter不是Servlet,它只是Servlet接收請(qǐng)求前的預(yù)處理器,不能產(chǎn)生response,但是能夠在requet到達(dá)servlet之前預(yù)處理request,對(duì)servlet執(zhí)行結(jié)束后得到的response做處理。 3738三個(gè)核心接口,javax.servle包中:Filter,FilterChain,FilterConfig一個(gè)Filter 必須實(shí)現(xiàn)javax.Servlet.Filter 接口,即實(shí)現(xiàn)下面的三個(gè)方法:(1)doFilter(ServletRequest, ServletResponse, FilterChain)。用來實(shí)現(xiàn)過濾行為的方法。引入的 FilterChain 對(duì)象提供了后續(xù)Filter所要調(diào)用的信息。 (
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度觸控技術(shù)培訓(xùn)與認(rèn)證合同4篇
- 2025年收藏品市場(chǎng)交易規(guī)則制定與轉(zhuǎn)讓合同3篇
- 二零二五年度石油化工儲(chǔ)油罐采購合同樣本4篇
- 二零二四三方詢價(jià)采購合同-新能源汽車充電樁安裝與維護(hù)3篇
- A與B雙方合作經(jīng)營合同書2024版版B版
- 二零二四年專業(yè)講師團(tuán)隊(duì)簽約合作合同范本3篇
- 個(gè)人與企業(yè)2024年度庫房租賃承包合同3篇
- 2025年新型車庫設(shè)施銷售與技術(shù)支持合同4篇
- 2025年度車輛租賃合同終止協(xié)議范本(含車輛違章處理責(zé)任)4篇
- 2025年度室內(nèi)外裝飾設(shè)計(jì)與施工總承包合同模板4篇
- 部編版六年級(jí)下冊(cè)道德與法治全冊(cè)教案教學(xué)設(shè)計(jì)
- 【高空拋物侵權(quán)責(zé)任規(guī)定存在的問題及優(yōu)化建議7100字(論文)】
- 二年級(jí)數(shù)學(xué)上冊(cè)100道口算題大全 (每日一套共26套)
- 物流無人機(jī)垂直起降場(chǎng)選址與建設(shè)規(guī)范
- 肺炎臨床路徑
- 外科手術(shù)鋪巾順序
- 創(chuàng)新者的窘境讀書課件
- 聚焦任務(wù)的學(xué)習(xí)設(shè)計(jì)作業(yè)改革新視角
- 移動(dòng)商務(wù)內(nèi)容運(yùn)營(吳洪貴)任務(wù)三 APP的品牌建立與價(jià)值提供
- 電子競技范文10篇
- 食堂服務(wù)質(zhì)量控制方案與保障措施
評(píng)論
0/150
提交評(píng)論