![課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第1頁](http://file4.renrendoc.com/view/6b2823716ea36c55940c6dbd59763e6c/6b2823716ea36c55940c6dbd59763e6c1.gif)
![課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第2頁](http://file4.renrendoc.com/view/6b2823716ea36c55940c6dbd59763e6c/6b2823716ea36c55940c6dbd59763e6c2.gif)
![課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第3頁](http://file4.renrendoc.com/view/6b2823716ea36c55940c6dbd59763e6c/6b2823716ea36c55940c6dbd59763e6c3.gif)
![課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第4頁](http://file4.renrendoc.com/view/6b2823716ea36c55940c6dbd59763e6c/6b2823716ea36c55940c6dbd59763e6c4.gif)
![課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第5頁](http://file4.renrendoc.com/view/6b2823716ea36c55940c6dbd59763e6c/6b2823716ea36c55940c6dbd59763e6c5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Servlet&今日內(nèi)容學(xué)習(xí)目能夠理解Servlet 技術(shù)概能夠獨(dú)立寫出 Servlet 的入門程能夠說出 servlet 生命周期方法執(zhí)行流能夠應(yīng)用能夠使用 servletcontext 域?qū)δ軌驅(qū)懗鼋y(tǒng)計網(wǎng)站訪問次數(shù)的代第1Servlet什么 Servlet的作用來處理從客戶端發(fā)送過來的請求,Servlet 的任務(wù)有Servlet準(zhǔn)備工Servlet 規(guī)范要求:Servlet 程序需要編寫實(shí)現(xiàn)類,并在 web.xml 進(jìn)行配置javax.servlet.http.HttpServletdoGetdoPostdoGet()getdoPost()post請求。web.xml 進(jìn)行配置。編寫步Ht
2、tpServletdoGetdoPostpackage import import import import import publicclassHelloServletextends HttpServlet publicvoiddoGet(HttpServletRequestreq,HttpServletResponsethrowsServletException,IOException System.out.println(get 請求將執(zhí)行publicvoiddoPost(HttpServletRequestreq,HttpServletResponsethrowsServletExc
3、eption,IOException System.out.println(post 請求將執(zhí)行!-demo01servlet hello * servletxmlservlet-classservlet*servletservletservlet-/servlet-!-demo01servlethello end-Servlet 的執(zhí)行的流程詳解RequestRequest 接收請求參數(shù)的StringgetParameter(Stringnull,如果只有Request 接收請求參數(shù)入編寫表單,提供表單字段:username、password、hobby,以 post 方式提formacti
4、on=./demo01ParamServlet 用戶名:inputtype=textname=usernamevalue=jack密碼:inputtype=textname=passwordvalue=1234愛好:inputtype=checkboxname=hobbyvalue=checked=checkedinputtype=checkboxname=hobbyvalue=checked=checked燙inputtype=submitvalue=postServletPOSTpublicpublicclassDemo01ParamServletextends HttpServlet p
5、rivateprivatestaticfinallongserialVersionUID=publicvoiddoGet(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOException /0/1/1.1Stringusername= /1.2Stringpassword= + : + /2Stringhobbies= /3MapallData=for(Map.Entryentry:allData.entrySet()System.out.print(-publicvoiddoPost(HttpSer
6、vletRequestrequest,HttpServletResponsethrowsServletException,IOException /*,doPostdoGet*doPost*doGetdoGet(request,編寫 Servlet,配置文servlet-/servlet-Servlet完成一個用戶登錄的案例案例需求在網(wǎng)站的首頁上在網(wǎng)站的首頁上,登錄的鏈接,點(diǎn)擊登錄的鏈接,可以跳轉(zhuǎn)到登錄的頁面.案例分代碼實(shí)htmlformaction=./userServlet 用戶名:inputtype=textname=usernamevalue=jack密碼:inputtype=text
7、name=passwordvalue=123456inputtype=submitvalue=xml servlet-/servlet-Servlet代public classUserServlet extendsHttpServlet privatestaticfinallongserialVersionUID=ServletException,IOExceptiontry 1.Stringusername= Stringpassword= 2.User user= new 3.UserServiceuserService =new UserexistUser = 4.if(existUse
8、r=response.getWriter().println(Loginresponse.getWriter().println(Logincatch (Exceptione) protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponsethrows ServletException,IOExceptiondoGet(request,service代public classUserService *param*throwspublicUser login(Useruser)throwsSQLException User re
9、sult =DAOUserDaouserDao =new ListuserList= if(userList.size() != 0) result = returnDAOpublic classUserDao JdbcTemplatejdbcTemplate= new *DAO*param*throwspublicListlogin(Useruser) throwsSQLException Stringsql =select * from userwhere username=? and password = ListexistUser= returnServlet的生命周期生命周期:就是一
10、個對象從創(chuàng)建到銷毀的過程Servlet生命周期:Servlet從創(chuàng)建到銷毀的過程何時創(chuàng)建:ServletServlet (單實(shí)例何時銷毀:當(dāng)項(xiàng)目從服務(wù)器中移除的時候,或者關(guān)閉服務(wù)器的時候Servletservletjavax.servlet.Servlet每一次調(diào)用,都將執(zhí)行 service(ServletRequest,ServletResponse)方JavaEEServletGenericServletservletHttpServletHttpservicehttp getdoGet()如果是post,將調(diào)用servletServletServlet servletServlet 的i
11、nit 然后每一次請求服務(wù)器都會創(chuàng)建一個新的線程訪問 Servlet 中的 service 的方法service方法內(nèi)部根據(jù)請求的方式的不同調(diào)用 doXxx 的方法(get 請求調(diào)用 doGet,post 請求調(diào)用 Servlet 從服務(wù)器中被移除,或者關(guān)閉服務(wù)器,Servlet Servlet的相關(guān)的配置【啟動時創(chuàng)建 Servlet的配置的配置在 *2 - 傳入正整數(shù),整數(shù)越小,被創(chuàng)建的優(yōu)先級就越高【url-pattern 的配置完全路徑匹:以 /開例如/ServletDemo4/aaa/ServletDemo5目錄匹:以 /開始 需要以 *結(jié)束例如/* (所有),/aaa/*(aaa目錄下
12、的所有)擴(kuò)展名匹:不能以/開始以*開始的.例如:*.do,*.action、*.jsp、*錯誤的寫法 : 缺省路html頁面時,首先從當(dāng)前web項(xiàng)目的web.xml文件尋找匹配路徑,如果如果沒有找到,tomcatweb.xmlservlettomcat獲得匹配路徑時,優(yōu)先級順序:123有如下的配置servlet-/servlet-如果訪問地址* servlet-/servlet-servlet-/servlet-開發(fā)中的路徑的編寫相對路徑相對路徑:都是需要找位置相對關(guān)系.開始的當(dāng)前路徑 ./使用相對路徑訪問絕對路徑:不需要找位置相對關(guān)系以 開始的絕對路徑中分為客戶端路徑和服務(wù)器端路徑客戶端路徑
13、一定要加工程名服務(wù)器端路徑不需要加工程名第2ServletContext服務(wù)器啟動的時候,WEB ServletContext 對象,我們可以使用這個對象存取數(shù)據(jù),WEB 應(yīng)用中獲得。可以使用如下方法存取數(shù)據(jù)setAttribute(Stringname,ObjectServletContextgetAttribute(StringServletContextServletContext案例:記錄網(wǎng)站的登錄成功的人數(shù)案例需求登錄成功后登錄成功后,5秒后跳轉(zhuǎn)到某個頁面,在頁面中顯示您是第x位登錄成功的用戶案例分析代碼實(shí)現(xiàn)formaction=./userCountServlet用戶名:input
14、type=textname=usernamevalue=jack密碼:inputtype=textname=passwordvalue=123456inputtype=submitvalue=xml servlet-servlet-/servlet-Servlet代*publicclassUserCountServletextends HttpServlet privatestaticfinallongserialVersionUID=publicvoidinit() throwsServletException countint count = ServletContextthis.getS
15、ervletContext().setAttribute(count,ServletException,IOExceptiontry 1.Stringusername= Stringpassword= 2.User user= new 3.UserServiceuserService =new UserexistUser= 4.if(existUser=intcount=(int) this.getServletContext().setAttribute(count,response.setHeader(Refresh,catch (Exceptione) protectedvoiddoPo
16、st(HttpServletRequestrequest,HttpServletResponsethrowsServletException,IOExceptiondoGet(request,Servlet代publicclassCountServletextends HttpServlet privatestaticfinallongserialVersionUID=ServletException,IOExceptionCountintcount =(int) protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponse
17、throwsServletException,IOExceptiondoGet(request,ServletContext:WEBServletContext對象,tomcat為每一個web項(xiàng)目單獨(dú)創(chuàng)建的一個上下文(知上知下貫穿全文)對servlet之間共享數(shù)據(jù)WEB項(xiàng)目中的指定資源(文件webweb.xmlweb全局初始化參數(shù)(整個項(xiàng)目)全局初始化參數(shù)(整個項(xiàng)目)方法說功能描SetgetResourcePaths(StringSet path必須以正斜線(/)開始,StringgetRealPath(String返回資源文件在服務(wù)器文件系統(tǒng)上的真實(shí)路徑(文件的絕對路徑)path 代表資源文
18、件的虛擬路徑,它應(yīng)該以正斜線開始(/)開始,“/”表示當(dāng)前 方法說功能描SetgetResourcePaths(StringSet path必須以正斜線(/)開始,StringgetRealPath(String返回資源文件在服務(wù)器文件系統(tǒng)上的真實(shí)路徑(文件的絕對路徑)path 代表資源文件的虛擬路徑,它應(yīng)該以正斜線開始(/)開始,“/”表示當(dāng)前 URL getResource(String URL getResourceAsStream(Stringpath)返回映射到某個資源文件的 InputStream pathgetResource()方法完全一ServletContextWeb資源路
19、徑的方法后,接下來通過一個案例,分步驟演示ServletContext 對象讀取資源文件代碼實(shí)publicclassReadFileServletextends HttpServlet publicvoiddoGet(HttpServletRequestHttpServletResponseresponse)throwsServletException,ServletContextcontext =PrintWriterout= InputStream inPropertiespros =new out.println(Company=+pros.getProperty(Company)+out.println(Address=+
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 教育培訓(xùn)機(jī)構(gòu)評估居間合同
- 紡織品交易居間合同協(xié)議書
- 2025年度辦公室日常保潔與節(jié)能照明設(shè)備供應(yīng)服務(wù)合同
- 廣告投放數(shù)據(jù)分析合同
- 產(chǎn)品包裝設(shè)計技術(shù)指南
- 安全生產(chǎn)托管協(xié)議合同
- 工礦企業(yè)產(chǎn)品購銷合同
- 廚房承包協(xié)議集錦
- 農(nóng)業(yè)質(zhì)量標(biāo)準(zhǔn)制定指南
- 能源行業(yè)能源供應(yīng)鏈優(yōu)化與智能倉儲管理
- 保潔班長演講稿
- 勞務(wù)雇傭協(xié)議書范本
- JGJ52-2006 普通混凝土用砂、石質(zhì)量及檢驗(yàn)方法標(biāo)準(zhǔn)
- 環(huán)境監(jiān)測的基本知識
- 電動車棚施工方案
- 《中國十大書法家》課件
- 超實(shí)用可編輯版中國地圖全圖及分省地圖
- 尿路結(jié)石腔內(nèi)碎石患者圍手術(shù)期并發(fā)尿膿毒癥護(hù)理專家共識
- 交換機(jī)工作原理詳解(附原理圖)
- 小學(xué)總復(fù)習(xí)非連續(xù)性文本教學(xué)課件
- 2023年考研考博考博英語福建師范大學(xué)考試高頻考點(diǎn)參考題庫帶答案
評論
0/150
提交評論