入職培訓(xùn)-JAVAWEB編程中級(jí)課程_第1頁(yè)
入職培訓(xùn)-JAVAWEB編程中級(jí)課程_第2頁(yè)
入職培訓(xùn)-JAVAWEB編程中級(jí)課程_第3頁(yè)
入職培訓(xùn)-JAVAWEB編程中級(jí)課程_第4頁(yè)
入職培訓(xùn)-JAVAWEB編程中級(jí)課程_第5頁(yè)
已閱讀5頁(yè),還剩49頁(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)介

入職培訓(xùn)-JAVAWEB編程中級(jí)課程內(nèi)容概述JavaWeb工程目錄結(jié)構(gòu)Servlet生命周期Filter、Listener自定義Tag標(biāo)簽MVC框架介紹第二頁(yè),共54頁(yè)。JavaWeb工程目錄結(jié)構(gòu)主要說(shuō)明:src:編譯至WebRoot/WEB-INF/classeslib:運(yùn)行庫(kù),WEB應(yīng)用使用的第三方運(yùn)行jar存放目錄web.xml:web配置文件第三頁(yè),共54頁(yè)。JavaWeb工程目錄結(jié)構(gòu)查看Tomcat部署目錄,結(jié)構(gòu)與WebRoot一致第四頁(yè),共54頁(yè)。JavaWeb工程目錄結(jié)構(gòu)導(dǎo)出WAR包,結(jié)構(gòu)也與WebRoot一致第五頁(yè),共54頁(yè)。web.xml配置web.xml配置第六頁(yè),共54頁(yè)。web.xml配置web.xml配置context-param獲取參數(shù)方法javax.servlet.ServletContext.getInitParameter(“…”);<context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/perties</param-value></context-param>第七頁(yè),共54頁(yè)。web.xml配置web.xml配置filter<filter><filter-name>UseTimeFilter</filter-name><filter-class>…</filter-class></filter><filter-mapping><filter-name>UseTimeFilter</filter-name><url-pattern>/download/*</url-pattern></filter-mapping>第八頁(yè),共54頁(yè)。web.xml配置web.xml配置Listener<listener><listener-class>…</listener-class></listener>第九頁(yè),共54頁(yè)。web.xml配置web.xml配置Servlet<servlet><servlet-name>DownloadServlet</servlet-name><servlet-class>…</servlet-class></servlet><servlet-mapping><servlet-name>DownloadServlet</servlet-name><url-pattern>/download.do</url-pattern></servlet-mapping>第十頁(yè),共54頁(yè)。web.xml配置web.xml配置Session超時(shí)單位:分鐘-1,代表會(huì)話不超時(shí)其他數(shù)字代表,多少分鐘后會(huì)話超時(shí)<session-config><session-timeout>-1</session-timeout></session-config>第十一頁(yè),共54頁(yè)。web.xml配置web.xml配置ErrorPage配置error-code錯(cuò)誤http代碼location跳轉(zhuǎn)路徑<error><error-code>404</error-code><location>/error/404.jsp</location></error>第十二頁(yè),共54頁(yè)。Servlet生命周期initdestoryservice第一次使用初始化多次服務(wù)服務(wù)器關(guān)閉銷毀第十三頁(yè),共54頁(yè)。Servlet生命周期時(shí)序圖第十四頁(yè),共54頁(yè)。Servlet生命周期演示代碼privatestaticLoglog=LogFactory.getLog(LifeCycleServlet.class);publicvoiddestroy(){super.destroy();("LifeCycleServletdestroy...");}protectedvoidservice(HttpServletRequestarg0,HttpServletResponsearg1)throwsServletException,IOException{("LifeCycleServletservice...");super.service(arg0,arg1);}publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{("LifeCycleServletdoGet...");…}publicvoidinit()throwsServletException{("LifeCycleServletinit...");}第十五頁(yè),共54頁(yè)。Servlet生命周期演示代碼日志2012-12-1118:56:16,296INFOhttp-8080-1LifeCycleServlet.java97-LifeCycleServletinit...2012-12-1118:56:16,296INFOhttp-8080-1LifeCycleServlet.java29-LifeCycleServletservice...2012-12-1118:56:16,312INFOhttp-8080-1LifeCycleServlet.java45-LifeCycleServletdoGet...2012-12-1118:56:17,093INFOhttp-8080-1LifeCycleServlet.java29-LifeCycleServletservice...2012-12-1118:56:17,093INFOhttp-8080-1LifeCycleServlet.java45-LifeCycleServletdoGet...2012-12-1118:56:17,734INFOhttp-8080-1LifeCycleServlet.java29-LifeCycleServletservice...2012-12-1118:56:17,734INFOhttp-8080-1LifeCycleServlet.java45-LifeCycleServletdoGet...2012-12-1118:56:21,546INFOmainLifeCycleServlet.java23-LifeCycleServletdestroy...第十六頁(yè),共54頁(yè)。JSP與ServletJSP是Servlet技術(shù)的擴(kuò)展JSPJSP引擎.class區(qū)別:JSP有內(nèi)置對(duì)象,Servlet沒(méi)有JSP可以直接寫html標(biāo)簽,Servlet需要通過(guò)獲取客戶端輸出流寫html標(biāo)簽第十七頁(yè),共54頁(yè)。JSP編譯class實(shí)現(xiàn)接口javax.servlet.jsp.HttpJspPage.classjavax.servlet.jsp.JspPagejavax.servlet.Servlet第十八頁(yè),共54頁(yè)。JSP編譯class查看第十九頁(yè),共54頁(yè)。FilterFilter:過(guò)濾器作用訪問(wèn)資源攔截,權(quán)限控制日志記錄動(dòng)態(tài)轉(zhuǎn)換第二十頁(yè),共54頁(yè)。FilterFilterChain:過(guò)濾器鏈,由過(guò)濾器組成一條鏈第二十一頁(yè),共54頁(yè)。Filter使用

1.實(shí)現(xiàn)Filter接口init()初始化方法doFilter()過(guò)濾器處理方法

destroy()

銷毀方法

2.doFilter()調(diào)用下一過(guò)濾器FilterChain.doFilter(request,response);

3.在web.xml注冊(cè)這個(gè)Filter,以與它將過(guò)濾的頁(yè)面。第二十二頁(yè),共54頁(yè)。FilterHelloWorld1.實(shí)現(xiàn)Filter接口publicclassLogFilterimplementsFilter{ publicvoiddestroy(){ System.out.println("LogFilterdestroy"); } publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse, FilterChainchain)throwsIOException,ServletException{ HttpServletRequesthttpRequest=(HttpServletRequest)request; System.out.println("LogFilter:"+httpRequest.getRemoteAddr()+"visit"+httpRequest.getRequestURI()); chain.doFilter(request,response); } publicvoidinit(FilterConfigconfig)throwsServletException{ System.out.println("LogFilterinit"); }}第二十三頁(yè),共54頁(yè)。FilterHelloWorld2.web.xml配置Filter<filter><filter-name>LogFilter</filter-name><filter-class>filters.LogFilter</filter-class></filter><filter-mapping><filter-name>LogFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>第二十四頁(yè),共54頁(yè)。Filter練習(xí)編寫Filter實(shí)現(xiàn)以下功能記錄客戶端詳細(xì)訪問(wèn)記錄訪問(wèn)資源訪問(wèn)ip訪問(wèn)時(shí)間耗時(shí)某些資源允許某些ip訪問(wèn)保護(hù)資源允許配置ip名單允許配置拒絕訪問(wèn)跳轉(zhuǎn)頁(yè)面配置第二十五頁(yè),共54頁(yè)。ListenerListener:監(jiān)聽(tīng)器作用對(duì)會(huì)話、Servlet上下文進(jìn)行監(jiān)聽(tīng)常用監(jiān)聽(tīng)器:ServletContextListenerServletContextAttributeListenerHttpSessionListenerHttpSessionAttributeListener第二十六頁(yè),共54頁(yè)。ServletContextListener對(duì)整個(gè)Servlet上下文監(jiān)聽(tīng)(啟動(dòng)、銷毀)方法說(shuō)明voidcontextInitialized(ServletContextEventevent)當(dāng)ServletContext創(chuàng)建的時(shí)候,將會(huì)調(diào)用這個(gè)方法voidcontextDestroyed(ServletContextEventevent)當(dāng)ServletContext銷毀的時(shí)候(例如關(guān)閉應(yīng)用服務(wù)器或者重新加載應(yīng)用),將會(huì)調(diào)用這個(gè)方法。第二十七頁(yè),共54頁(yè)。ServletContextAttributeListener對(duì)Servlet上下文屬性的監(jiān)聽(tīng)方法說(shuō)明voidattributeAdded(ServletContextAttributeEventevent)ServletContext屬性添加事件voidattributeRemoved(ServletContextAttributeEventevent)ServletContext屬性刪除事件voidattributeReplaced(ServletContextAttributeEventevent)ServletContext屬性修改事件第二十八頁(yè),共54頁(yè)。HttpSessionListener對(duì)整個(gè)Session監(jiān)聽(tīng)(創(chuàng)建、銷毀)方法說(shuō)明voidsessionCreated(HttpSessionEventevent)當(dāng)會(huì)話創(chuàng)建之后,將會(huì)調(diào)用這個(gè)方法voidsessionDestroyed(HttpSessionEventevent)當(dāng)會(huì)話過(guò)期之后,將會(huì)調(diào)用這個(gè)方法。第二十九頁(yè),共54頁(yè)。HttpSessionAttributeListener對(duì)Session屬性的監(jiān)聽(tīng)方法說(shuō)明voidattributeAdded(HttpSessionBindingEventevent)Session屬性添加事件voidattributeRemoved(HttpSessionBindingEventevent)Session屬性刪除事件voidattributeReplaced(HttpSessionBindingEventevent)Session屬性修改事件第三十頁(yè),共54頁(yè)。ListenerHelloworld1.新建ServletContextListener實(shí)現(xiàn)packagelisteners;publicclassContextListenerimplementsServletContextListener{publicvoidcontextDestroyed(ServletContextEventevent){System.out.println("contextDestroyed()");}publicvoidcontextInitialized(ServletContextEventevent){System.out.println("contextInitialized()");}}第三十一頁(yè),共54頁(yè)。ListenerHelloworld2.新建HttpSessionListener接口實(shí)現(xiàn)packagelisteners;publicclassSessionListenerimplementsHttpSessionListener{

publicvoidsessionCreated(HttpSessionEventevent){System.out.println("sessionCreated('"+event.getSession().getId()+"')");

}

publicvoidsessionDestroyed(HttpSessionEventevent){System.out.println("sessionDestroyed('"+event.getSession().getId()+"')");

}}第三十二頁(yè),共54頁(yè)。ListenerHelloworld3.配置web.xml<listener><listener-class>listeners.ContextListener</listener-class></listener><listener><listener-class>listeners.SessionListener</listener-class></listener>第三十三頁(yè),共54頁(yè)。Listener練習(xí)編寫<listener><listener-class>listeners.ContextListener</listener-class></listener><listener><listener-class>listeners.SessionListener</listener-class></listener>第三十四頁(yè),共54頁(yè)。Listener練習(xí)編寫相應(yīng)Listener實(shí)現(xiàn)以下功能查看當(dāng)前在線用戶數(shù)服務(wù)器啟動(dòng)時(shí)間最大在線用戶限制第三十五頁(yè),共54頁(yè)。自定義標(biāo)簽標(biāo)簽(Tag)標(biāo)簽是一種XML元素,通過(guò)標(biāo)簽可以使JSP網(wǎng)頁(yè)變得簡(jiǎn)潔并且易于維護(hù)。由于標(biāo)簽是XML元素,所以它的名稱和屬性都是大小寫敏感的標(biāo)簽庫(kù)描述文件(TagLibraryDescriptor)標(biāo)簽庫(kù)描述文件是一個(gè)XML文件,這個(gè)文件提供了標(biāo)簽庫(kù)中類和JSP中對(duì)標(biāo)簽引用的映射關(guān)系。它是一個(gè)配置文件,和web.xml是類似的標(biāo)簽處理類(TagHandleClass)標(biāo)簽處理類是一個(gè)Java類,這個(gè)類繼承了TagSupport或者擴(kuò)展了SimpleTag接口,通過(guò)這個(gè)類可以實(shí)現(xiàn)自定義JSP標(biāo)簽的具體功能第三十六頁(yè),共54頁(yè)。自定義標(biāo)簽標(biāo)簽庫(kù)描述文件存放路徑WEB-INF目錄下jar包的META-INF目錄下第三十七頁(yè),共54頁(yè)。TagSupport類TagSupport類pageContext屬性,保存頁(yè)面對(duì)象parent屬性,父標(biāo)簽常用方法說(shuō)明doStartTag()JSP容器遇到自定義標(biāo)簽的起始標(biāo)志,就會(huì)調(diào)用doStartTag()方法。返回值如下:SKIP_BODYEVAL_BODY_INCLUDEdoAfterBody()doStartTag返回EVAL_BODY_INCLUDE,就會(huì)調(diào)用doAfterBody()方法,返回值如下:EVAL_BODY_AGAINSKIP_BODYdoEndTag()JSP容器遇到自定義標(biāo)簽的結(jié)束標(biāo)志,就會(huì)調(diào)用doEndTag()方法,返回值如下:EVAL_PAGESKIP_PAGE第三十八頁(yè),共54頁(yè)。TagSupport處理流程doStartTag()doEndTag()doAfterBody()處理標(biāo)簽之間內(nèi)容EVAL_BODY_INCLUDESKIP_BODYSKIP_BODYEVAL_BODY_AGIN結(jié)束執(zhí)行JSP繼續(xù)執(zhí)行JSPSKIP_PAGEEVAL_PAGE第三十九頁(yè),共54頁(yè)。TLDshort-name標(biāo)簽:描述標(biāo)簽庫(kù)名稱uri標(biāo)簽:描述引入tld的uri第四十頁(yè),共54頁(yè)。TLDtag標(biāo)簽name:設(shè)定Tag的名字;tagclass:設(shè)定Tag的處理類;bodycontent:設(shè)定標(biāo)簽的主體(body)內(nèi)容。empty:表示標(biāo)簽中沒(méi)有bodyJSP:表示標(biāo)簽的body中可以加入JSP程序代碼tagdependent:表示標(biāo)簽中的內(nèi)容由標(biāo)簽自己去處理。第四十一頁(yè),共54頁(yè)。TLDtag標(biāo)簽屬性name:屬性名稱required:屬性是否必需的,默認(rèn)為falsertexprvalue屬性值是否可以為request-time表達(dá)式,也就是類似于<%=…%>的表達(dá)式。第四十二頁(yè),共54頁(yè)。自定義標(biāo)簽HelloWorld1.新建標(biāo)簽處理類tags.HelloTag

publicclassHelloTagextendsTagSupport{privateStringmessage="Hello";publicintdoEndTag()throwsJspException{JspWriterout=pageContext.getOut();try{out.print(message);out.print(newSimpleDateFormat("yyyy-MM-ddHH:mm:ss").format(newDate()));out.print("<br/>");}catch(IOExceptione){e.printStackTrace();}returnEVAL_PAGE;}publicStringgetMessage(){returnmessage;}publicvoidsetMessage(Stringmessage){this.message=message;}}第四十三頁(yè),共54頁(yè)。自定義標(biāo)簽HelloWorld2.WEB-INF目錄新建hello.tld描述標(biāo)簽<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEtaglibPUBLIC"-//SunMicrosystems,Inc.//DTDJSPTagLibrary1.2//EN"""><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><short-name>hello</short-name><uri></uri><tag><name>hello</name><tag-class>tags.HelloTag</tag-class><body-content>JSP</body-content><attribute><name>message</name><required>false</required></attribute></tag></taglib>第四十四頁(yè),共54頁(yè)。自定義標(biāo)簽HelloWorld3.新建JSP頁(yè)面引入標(biāo)簽庫(kù)<%@pagelanguage="java"pageEncoding="UTF-8"%><%@taglibprefix="mylib"uri=""%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>自定義標(biāo)簽測(cè)試</title></head><body><mylib:hello></mylib:hello><mylib:hellomessage="xxx"></mylib:hello></body></html>第四十五頁(yè),共54頁(yè)。自定義標(biāo)簽庫(kù)練習(xí)編寫自定義標(biāo)簽實(shí)現(xiàn)以下功能:顯示當(dāng)前登錄用戶名稱編寫登錄成功jsp,使用制作的標(biāo)簽顯示當(dāng)前登錄用戶名第四十六頁(yè),共54頁(yè)。MVC介紹MVC英文即Model-View-Controller,它是目前非常流行的一種軟件設(shè)計(jì)模式。MVC的設(shè)計(jì)思想是將應(yīng)用的輸入、處理和

溫馨提示

  • 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)論