課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第1頁(yè)
課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第2頁(yè)
課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第3頁(yè)
課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第4頁(yè)
課件7-13協(xié)議和tomcat服務(wù)器day14servlet_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、Servlet&今日內(nèi)容學(xué)習(xí)目能夠理解Servlet 技術(shù)概能夠獨(dú)立寫出 Servlet 的入門程能夠說出 servlet 生命周期方法執(zhí)行流能夠應(yīng)用能夠使用 servletcontext 域?qū)δ軌驅(qū)懗鼋y(tǒng)計(jì)網(wǎng)站訪問次數(shù)的代第1Servlet什么 Servlet的作用來處理從客戶端發(fā)送過來的請(qǐng)求,Servlet 的任務(wù)有Servlet準(zhǔn)備工Servlet 規(guī)范要求:Servlet 程序需要編寫實(shí)現(xiàn)類,并在 web.xml 進(jìn)行配置javax.servlet.http.HttpServletdoGetdoPostdoGet()getdoPost()post請(qǐng)求。web.xml 進(jìn)行配置。編寫步Ht

2、tpServletdoGetdoPostpackage import import import import import publicclassHelloServletextends HttpServlet publicvoiddoGet(HttpServletRequestreq,HttpServletResponsethrowsServletException,IOException System.out.println(get 請(qǐng)求將執(zhí)行publicvoiddoPost(HttpServletRequestreq,HttpServletResponsethrowsServletExc

3、eption,IOException System.out.println(post 請(qǐng)求將執(zhí)行!-demo01servlet hello * servletxmlservlet-classservlet*servletservletservlet-/servlet-!-demo01servlethello end-Servlet 的執(zhí)行的流程詳解RequestRequest 接收請(qǐng)求參數(shù)的StringgetParameter(Stringnull,如果只有Request 接收請(qǐng)求參數(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完成一個(gè)用戶登錄的案例案例需求在網(wǎng)站的首頁(yè)上在網(wǎng)站的首頁(yè)上,登錄的鏈接,點(diǎn)擊登錄的鏈接,可以跳轉(zhuǎn)到登錄的頁(yè)面.案例分代碼實(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、個(gè)對(duì)象從創(chuàng)建到銷毀的過程Servlet生命周期:Servlet從創(chuàng)建到銷毀的過程何時(shí)創(chuàng)建:ServletServlet (單實(shí)例何時(shí)銷毀:當(dāng)項(xiàng)目從服務(wù)器中移除的時(shí)候,或者關(guān)閉服務(wù)器的時(shí)候Servletservletjavax.servlet.Servlet每一次調(diào)用,都將執(zhí)行 service(ServletRequest,ServletResponse)方JavaEEServletGenericServletservletHttpServletHttpservicehttp getdoGet()如果是post,將調(diào)用servletServletServlet servletServlet 的i

11、nit 然后每一次請(qǐng)求服務(wù)器都會(huì)創(chuàng)建一個(gè)新的線程訪問 Servlet 中的 service 的方法service方法內(nèi)部根據(jù)請(qǐng)求的方式的不同調(diào)用 doXxx 的方法(get 請(qǐng)求調(diào)用 doGet,post 請(qǐng)求調(diào)用 Servlet 從服務(wù)器中被移除,或者關(guān)閉服務(wù)器,Servlet Servlet的相關(guān)的配置【啟動(dòng)時(shí)創(chuàng)建 Servlet的配置的配置在 *2 - 傳入正整數(shù),整數(shù)越小,被創(chuàng)建的優(yōu)先級(jí)就越高【url-pattern 的配置完全路徑匹:以 /開例如/ServletDemo4/aaa/ServletDemo5目錄匹:以 /開始 需要以 *結(jié)束例如/* (所有),/aaa/*(aaa目錄下

12、的所有)擴(kuò)展名匹:不能以/開始以*開始的.例如:*.do,*.action、*.jsp、*錯(cuò)誤的寫法 : 缺省路html頁(yè)面時(shí),首先從當(dāng)前web項(xiàng)目的web.xml文件尋找匹配路徑,如果如果沒有找到,tomcatweb.xmlservlettomcat獲得匹配路徑時(shí),優(yōu)先級(jí)順序:123有如下的配置servlet-/servlet-如果訪問地址* servlet-/servlet-servlet-/servlet-開發(fā)中的路徑的編寫相對(duì)路徑相對(duì)路徑:都是需要找位置相對(duì)關(guān)系.開始的當(dāng)前路徑 ./使用相對(duì)路徑訪問絕對(duì)路徑:不需要找位置相對(duì)關(guān)系以 開始的絕對(duì)路徑中分為客戶端路徑和服務(wù)器端路徑客戶端路徑

13、一定要加工程名服務(wù)器端路徑不需要加工程名第2ServletContext服務(wù)器啟動(dòng)的時(shí)候,WEB ServletContext 對(duì)象,我們可以使用這個(gè)對(duì)象存取數(shù)據(jù),WEB 應(yīng)用中獲得??梢允褂萌缦路椒ù嫒?shù)據(jù)setAttribute(Stringname,ObjectServletContextgetAttribute(StringServletContextServletContext案例:記錄網(wǎng)站的登錄成功的人數(shù)案例需求登錄成功后登錄成功后,5秒后跳轉(zhuǎn)到某個(gè)頁(yè)面,在頁(yè)面中顯示您是第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對(duì)象,tomcat為每一個(gè)web項(xiàng)目單獨(dú)創(chuàng)建的一個(gè)上下文(知上知下貫穿全文)對(duì)servlet之間共享數(shù)據(jù)WEB項(xiàng)目中的指定資源(文件webweb.xmlweb全局初始化參數(shù)(整個(gè)項(xiàng)目)全局初始化參數(shù)(整個(gè)項(xiàng)目)方法說功能描SetgetResourcePaths(StringSet path必須以正斜線(/)開始,StringgetRealPath(String返回資源文件在服務(wù)器文件系統(tǒng)上的真實(shí)路徑(文件的絕對(duì)路徑)path 代表資源文

18、件的虛擬路徑,它應(yīng)該以正斜線開始(/)開始,“/”表示當(dāng)前 方法說功能描SetgetResourcePaths(StringSet path必須以正斜線(/)開始,StringgetRealPath(String返回資源文件在服務(wù)器文件系統(tǒng)上的真實(shí)路徑(文件的絕對(duì)路徑)path 代表資源文件的虛擬路徑,它應(yīng)該以正斜線開始(/)開始,“/”表示當(dāng)前 URL getResource(String URL getResourceAsStream(Stringpath)返回映射到某個(gè)資源文件的 InputStream pathgetResource()方法完全一ServletContextWeb資源路

19、徑的方法后,接下來通過一個(gè)案例,分步驟演示ServletContext 對(duì)象讀取資源文件代碼實(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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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)論