摘要:一個(gè)應(yīng)用程序中可以有很多,這些都共享同一個(gè)對象,我們經(jīng)常將對象叫域?qū)ο?。常用的的獲取對象的向?qū)ο蠼壎〝?shù)據(jù)的從對象取出數(shù)據(jù)的獲取當(dāng)前應(yīng)用的初始化參數(shù)的獲取資源文件,返回流的獲取資源文件,返回路徑
第一個(gè)Servlet開發(fā)步驟
01_創(chuàng)建一個(gè)普通Java類,實(shí)現(xiàn)Servlet接口 02_將寫好的Servlet類,還得配置到web.xml文件中去 Demo01.java編寫一個(gè)簡單的Servlet程序輸出英文字符串到瀏覽器【實(shí)現(xiàn)Servlet接口】
public class Demo01 implements Servlet{ @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { OutputStream os = servletResponse.getOutputStream(); os.write("web.xmlHello Servlet
".getBytes()); os.flush(); os.close(); } }
為什么建議創(chuàng)建Servlet直接繼承HttpServlet類呢Demo01 cn.itheima.servlet.Demo01 Demo01 /xxx
創(chuàng)建Servlet有三種方式,前二種創(chuàng)建的Servlet能處理任意協(xié)議的請求, 但我們做Web開發(fā),大多都是處理Http協(xié)議,所有建議直接繼承HttpServlet類 再者,HttpServlet類,也繼承與實(shí)現(xiàn)了前二種方式的類或接口,且有適合于HTTP協(xié)議特點(diǎn)的方法方式一:編寫一個(gè)簡單的Servlet程序輸出英文字符串到瀏覽器【實(shí)現(xiàn)Servlet接口】
編寫一個(gè)簡單的Servlet程序輸出英文字符串到瀏覽器【實(shí)現(xiàn)Servlet接口】
public class Demo01 implements Servlet{ @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { OutputStream os = servletResponse.getOutputStream(); os.write("方式二:編寫一個(gè)簡單的Servlet程序輸出中文字符串到瀏覽器【繼承GenericServlet類】Hello Servlet
".getBytes()); os.flush(); os.close(); } }Demo01 cn.itheima.servlet.Demo01 Demo01 /Demo01
編寫一個(gè)簡單的Servlet程序輸出中文字符串到瀏覽器【繼承GenericServlet類】
public class Demo02 extends GenericServlet{ @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { servletResponse.setContentType("text/html;charset=UTF-8"); PrintWriter pw = servletResponse.getWriter(); pw.write("編寫一個(gè)簡單的Servlet程序輸出當(dāng)前時(shí)間到瀏覽器【繼承HttpServlet類】你好,Servlet,我們今天見面了
"); pw.flush(); pw.close(); } }Demo02 cn.itheima.servlet.Demo02 Demo02 /Demo02
編寫一個(gè)簡單的Servlet程序輸出當(dāng)前時(shí)間到瀏覽器【繼承HttpServlet類】
public class Demo03 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter pw = response.getWriter(); pw.write("什么是Servlet生命周期現(xiàn)在時(shí)間為:" + new Date().toLocaleString()+"
"); pw.flush(); pw.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }Demo03 cn.itheima.servlet.Demo03 如果在瀏覽器地址欄輸入U(xiǎn)RL后回車,這種方式屬于GET請求 Demo03 /Demo03
Servlet是運(yùn)行于Web服務(wù)器中的Servlet引擎/容器中,客戶端是不能直接調(diào)用Servlet的,必須由Servlet引擎/容器才能調(diào)用。因此Servlet在引擎/容器中從創(chuàng)建到銷毀的全過程,稱之為Servlet生命周期Servlet生命周期分為四個(gè)過程,分別是:
01——構(gòu)造器(執(zhí)行一次) 02——初始化(執(zhí)行一次)-----------------------------在默認(rèn)情況下,首次訪問該Servlet時(shí)執(zhí)行 03——doGet/doPost/service(執(zhí)行多次)----------------------非首次訪問該Servlet時(shí)執(zhí)行 04——銷毀(執(zhí)行一次) Servlet是單例的,被多個(gè)客戶端線程共享,建議不要用實(shí)例變量Servlet運(yùn)行過程圖解 ServletConfig對象有什么作用
當(dāng)Servlet配置了初始化參數(shù)后,Servlet引擎/容器在創(chuàng)建Servlet對象時(shí), 會自動將這些初始化參數(shù)封裝到ServletConfig對象中,并在調(diào)用Servlet的 init(ServletContext)方法時(shí),將ServletConfig對象傳遞給Servlet。 從而程序員可以通過ServletConfig對象就可以得到當(dāng)前Servlet的初始化參數(shù)信息常用的API
ServletContext的getInitParameterNames():獲取當(dāng)前Servlet的所有初始化參數(shù)
ServletContext的getInitParameter():獲取當(dāng)前Servlet的一個(gè)初始化參數(shù) Demo04.java
public class Demo04 extends HttpServlet { private ServletConfig config; public Demo04() { } @Override public void init(ServletConfig config) throws ServletException { this.config = config; /* //獲取web.xml文件中的servlet初始化參數(shù) EnumerationServlet創(chuàng)建時(shí)間enums = config.getInitParameterNames(); while(enums.hasMoreElements()){ String key = enums.nextElement(); String value = config.getInitParameter(key); //在控制臺顯示 System.out.println(key+"-"+value); } */ } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter pw = response.getWriter(); Enumeration enums = config.getInitParameterNames(); while(enums.hasMoreElements()){ String key = enums.nextElement(); String value = config.getInitParameter(key); //在瀏覽器中顯示 pw.write(key+"--"+value+"
"); } pw.flush(); pw.close(); } @Override public void destroy() { } }
在默認(rèn)情況下,Servlet是客戶端首次訪問時(shí),Servlet引擎/容器才創(chuàng)建并對其初始化, 有時(shí)我們希望Servlet創(chuàng)建與初始化的時(shí)間能夠提前到Web服務(wù)器啟動時(shí),這樣客戶 端首次訪問時(shí),就不用創(chuàng)建與初始化Servlet了,提高了訪問速度,我們可以用自動 加載Servlet來完成任務(wù) 配置的數(shù)值越小,越先創(chuàng)建與初始化,只限于0,1,2,3等等,負(fù)數(shù)與沒有配置效果一樣 如果配置負(fù)數(shù),必須用戶首次訪問時(shí)才創(chuàng)建與初始化該Servlet
web.xml什么是Servlet虛擬路徑Demo04 cn.itheima.servlet.Demo04 1 Demo04 /Demo04
一個(gè)在web.xml文件中,可供外界能訪問Web服務(wù)器中Servlet的字符串 即什么是缺省Servlet/xxx這個(gè)就是Servlet虛擬路徑 為什么要配置Servlet虛擬路徑 Servlet位于Servlet引擎/容器中,Servlet引擎/容器又位于Web服務(wù)器中, 客戶端是不可能知道Servlet的名字的,但Servlet畢竟要處理客戶端提交的請求, 就必須讓客戶端訪問,那么要訪問Servlet,就必須給客戶端一個(gè)有效的訪問路徑, 又不能暴露Servlet真實(shí)保存在服務(wù)器中的位置,于是就只能配置Servlet虛擬路徑 如何配置Servlet虛擬路徑 Servlet虛擬路徑的配置是在web.xml文件中,一個(gè)Servlet可以配置在一個(gè)或多個(gè)虛擬路徑上, 也可以使用*.xx或者是/xx通配符形式來配置Servlet路徑通配符 不能出現(xiàn)/*.do的情況,只能分成二個(gè)url-pattern來書寫 當(dāng)一個(gè)真正的index.html與一個(gè)虛擬路徑為index.html同時(shí)存在時(shí), 以虛擬路徑為主 web.xmlDemo05 cn.itheima.servlet.Demo05 1 Demo05 /Demo05 /xx /yy /index.html /* *.do
如果某個(gè)Servlet的映射的虛擬路徑為一個(gè)正斜杠(/),那么這個(gè)Servlet就成為 當(dāng)前Web應(yīng)用程序的默認(rèn)Servlet默認(rèn)Servlet有什么作用 ----專用于處理其它正常Servlet不處理的客戶端請求 Servlet讀取硬盤中的圖片
Tomcat中就有默認(rèn)Servlet在Tomcat/conf/web.xml文件中,注冊了一個(gè)名稱為DefaultServlet的Servlet, 并將這個(gè)Servlet設(shè)置為了缺省Servlet,像404,500,等頁面,就是由這個(gè)DefaultServlet
來響應(yīng)給客戶端的,當(dāng)程序員有一個(gè)缺省Servlet時(shí),Tomcat又有一個(gè)缺省Servlet時(shí), 以程序員為主。
項(xiàng)目中,我們以Tomcat中的缺省Servlet為主,即程序員不用寫缺省Servlet
Demo06.java public class Demo06 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //輸入流指向圖片 InputStream is = this.getClass().getClassLoader().getResourceAsStream("../../images/girl1.jpg"); //輸出流指向?yàn)g覽器 OutputStream os = response.getOutputStream(); //讀取圖片并輸出到瀏覽器 byte[] buf = new byte[2048]; int len = 0; while((len=is.read(buf))>0){ os.write(buf,0,len); } //關(guān)閉流 os.close(); is.close(); } }ServletContext對象 ServletContext對象有什么作用Demo06 cn.itheima.servlet.Demo06 關(guān)于路徑問題: /webapps /day11 /images/girl_1.jpg /WEB-INF/classes/Demo06.class /lib /web.xml Demo06 /
Tomcat啟動時(shí),會為每個(gè)部署在Tomcat中的Web應(yīng)用程序都創(chuàng)建一個(gè)對應(yīng)的ServletContext對象,
它代表當(dāng)前Web應(yīng)用。即一個(gè)Web應(yīng)用程序?qū)?yīng)著一個(gè)ServletContext對象。
一個(gè)Web應(yīng)用程序中可以有很多Servlet,這些Servlet都共享同一個(gè)ServletContext對象,我們經(jīng)常
將ServletContext對象叫域?qū)ο蟆?/p>
常用的API
ServletConfig的getServletContext():獲取ServletContext對象 ServletContext的setAttribute():向ServletContext對象綁定數(shù)據(jù) ServletContext的getAttribute():從ServletContext對象取出數(shù)據(jù) ServletContext的getInitParameter():獲取當(dāng)前Web應(yīng)用的初始化參數(shù) ServletContext的getResourceAsStream():獲取資源文件,返回流 ServletContext的getRealPath():獲取資源文件,返回路徑 Demo07.java public class Demo07 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("Demo07::doGet()"); //InputStream is = this.getServletContext().getResourceAsStream("/images/girl_2.png"); String filePath = this.getServletContext().getRealPath("/images/girl_3.png"); System.out.println(filePath); InputStream is = new FileInputStream(filePath); OutputStream os = response.getOutputStream(); byte[] buf = new byte[1024*2]; int len = 0; while((len=is.read(buf))>0){ os.write(buf,0,len); } os.close(); is.close(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/70007.html
摘要:,容器的作用是負(fù)責(zé)處理客戶請求,當(dāng)客戶請求來到時(shí),容器獲取請求,然后調(diào)用某個(gè),并把的執(zhí)行結(jié)果返回給客戶。將請求發(fā)送給服務(wù)器端是的容器。將該請求轉(zhuǎn)換成一個(gè)消息,并將其放入一個(gè)隊(duì)列。繼承自類,其在中扮演的角色是中心控制器。 容器就是你的程序運(yùn)行時(shí)需要的環(huán)境 1,Tomcat是Servlet的運(yùn)行環(huán)境,即一個(gè)Servlet容器。 2,Servlet容器的作用是負(fù)責(zé)處理客戶請求,當(dāng)客戶請求來到...
閱讀 1738·2021-11-15 11:37
閱讀 3484·2021-09-28 09:44
閱讀 1738·2021-09-07 10:15
閱讀 2858·2021-09-03 10:39
閱讀 2753·2019-08-29 13:20
閱讀 1358·2019-08-29 12:51
閱讀 2268·2019-08-26 13:44
閱讀 2186·2019-08-23 18:02