帶你快速上手Servlet
一、Servlet與Tomcat的關(guān)系
(1)Tomcat是什么?
Tomcat其實(shí)是Web服務(wù)器和Servlet容器的結(jié)合體
(2)什么是Web服務(wù)器?
比如,我當(dāng)前在杭州,你能否用自己的電腦訪問我桌面上的一張圖片?恐怕不行,我們太習(xí)慣通過URL訪問的一個(gè)網(wǎng)站、下載一部電影了。一個(gè)資源,如果沒有URL映射,那么外界幾乎很難訪問,而Web服務(wù)器的作用說穿了就是:將某個(gè)主機(jī)上的資源映射為一個(gè)URL供外界訪問
二、什么是Servlet
(1)什么是Servlet容器?
Servlet是運(yùn)行在Web服務(wù)器或應(yīng)用服務(wù)器上的程序。
Servlet容器,顧名思義里面存著Servlet對象,我們?yōu)槭裁茨軌蛲ㄟ^Web服務(wù)器映射的URL訪問資源?肯定需要寫程序處理請求,主要3個(gè)過程:接受請求,處理請求,響應(yīng)請求。
三、Servlet的類結(jié)構(gòu)
通過繼承HttpServlet實(shí)現(xiàn)Servlet接口
一般在實(shí)際項(xiàng)目開發(fā)中,都是使用繼承HttpServlet類的方式去實(shí)現(xiàn)Servlet程序
(1)編寫一個(gè)類去繼承HttpServlet類
(2)根據(jù)業(yè)務(wù)需要重寫doGet或doPost方法
(3)到web.xml中的配置servlet程序的訪問地址
四、ServletConfig類
ServletConfig代表的是當(dāng)前Servlet在web.xml中的配置信息
String getServletName(); ---獲取當(dāng)前Servlet在web.xml中配置的名字 ServletContext getServletContext();---獲取當(dāng)前Servlet指定名稱的初始化參數(shù)的值 String getInitParameter(String var1);---獲取當(dāng)前Servlet所有初始化參數(shù)的名字組成的枚舉 Enumeration<String> getInitParameterNames();---獲取代表當(dāng)前web應(yīng)用的ServletContext對象
(1)作用:
1、可以獲取Servlet程序的別名Servlet-name的值
2、獲取初始化參數(shù)init-param
3、獲取ServletContext對象
@Override public void init(ServletConfig servletConfig) throws ServletException { // 1、可以獲取Servlet程序的別名Servlet-name的值 System.out.println(servletConfig.getServletName()); // 2、獲取初始化參數(shù)init-param System.out.println(servletConfig.getInitParameter("username")); // 3、獲取ServletContext對象 System.out.println(servletConfig.getServletContext()); System.out.println("2、執(zhí)行初始化方法"); }
五、ServletContext類
(1)什么是ServletContext?
1、ServletContext是一個(gè)接口,它表示Servlet上下文對象。
2、一個(gè)Web工程,只有一個(gè)ServletContext對象實(shí)例。
3、ServletContext是一個(gè)域?qū)ο蟆?/p>
4、ServletContext是在web工程部署啟動(dòng)的時(shí)候創(chuàng)建,在web工程停止的時(shí)候銷毀。
什么是域?qū)ο螅?/p>
域?qū)ο?,是可以像Map一樣存取數(shù)據(jù)的對象,叫域?qū)ο蟆?/p>
這里的域指的是存取數(shù)據(jù)的操作范圍,整個(gè)web工程。
存數(shù)據(jù) 取數(shù)據(jù) 刪除數(shù)據(jù)
Map put() get() remove()
域?qū)ο?setAttribute() getAttribute() removeAttribute()
(2) ServletContext類的四個(gè)作用
1、獲取web.xml中配置的上下文參數(shù)context-param
public class ContextServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、 獲取web.xml中配置上下文參數(shù)context-param ServletContext servletContext = getServletConfig().getServletContext(); String username = servletContext.getInitParameter("username"); System.out.println("context-param參數(shù)的username"+username); } }
在web.xml中
<!-- context-param 是上下文參數(shù)(它是屬于整個(gè)web工程)--> <context-param> <param-name>username</param-name> <param-value>context</param-value> </context-param>
2、獲取當(dāng)前的工程路徑,格式:/工程路徑
3、獲取工程部署后在服務(wù)器硬盤上的絕對路徑
(3)ServletContext像map一樣存取數(shù)據(jù)
public class ContextServlet1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取ServletContext對象 ServletContext context = getServletContext(); System.out.println("保存之前:Context1 獲取key1的值是:"+context.getAttribute("key1")); context.setAttribute("key1","value1"); System.out.println("Context1中獲取域數(shù)據(jù)key1的值是:"+context.getAttribute("key1")); } }
保存之前:Context1 獲取key1的值是:null Context1中獲取域數(shù)據(jù)key1的值是:value1 Context2中獲取域數(shù)據(jù)key1的值是:value1
六、Servlet的生命周期
public class HelloServlet implements Servlet { public HelloServlet() { System.out.println("1、執(zhí)行構(gòu)造器方法"); } @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("2、執(zhí)行初始化方法"); } @Override public ServletConfig getServletConfig() { return null; } //service方法是專門用來處理請求和響應(yīng)的 @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("3、hello servlet 被訪問了"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { System.out.println(" 4、執(zhí)行銷毀方法"); } }
執(zhí)行的結(jié)果
1、執(zhí)行構(gòu)造器方法
2、執(zhí)行初始化方法
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
3、hello servlet 被訪問了
G:\softWareInstall\apache-tomcat-9.0.45\bin\catalina.bat stop
Using CATALINA_BASE: "C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_Servlet"
Using CATALINA_HOME: "G:\softWareInstall\apache-tomcat-9.0.45"
Using CATALINA_TMPDIR: "G:\softWareInstall\apache-tomcat-9.0.45\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_60"
Using CLASSPATH: "G:\softWareInstall\apache-tomcat-9.0.45\bin\bootstrap.jar;G:\softWareInstall\apache-tomcat-9.0.45\bin\tomcat-juli.jar"
Using CATALINA_OPTS: ""
03-May-2021 14:33:11.909 淇℃伅 [main] org.apache.catalina.core.StandardServer.await 閫氳繃鍏抽棴绔彛鎺ユ敹鍒版湁鏁堢殑鍏抽棴鍛戒護(hù)銆傛鍦ㄥ仠姝㈡湇鍔″櫒瀹炰緥銆�
03-May-2021 14:33:11.909 淇℃伅 [main] org.apache.coyote.AbstractProtocol.pause 鏆傚仠ProtocolHandler["http-nio-8080"]
03-May-2021 14:33:12.289 淇℃伅 [main] org.apache.catalina.core.StandardService.stopInternal 姝e湪鍋滄鏈嶅姟[Catalina]
4、執(zhí)行銷毀方法
(1)執(zhí)行Servlet構(gòu)造器方法
(2) 執(zhí)行init初始化方法
第一、二步,是在第一次訪問的時(shí)候創(chuàng)建Servlet程序會(huì)調(diào)用
(3)執(zhí)行 Service方法
第三步、每次訪問都會(huì)調(diào)用
(4)執(zhí)行destroy銷毀方法
第四步:在web工程停止的時(shí)候調(diào)用
七、Get、Post
get、post請求都會(huì)走Service方法,那么怎么區(qū)分get、post請求
//service方法是專門用來處理請求和響應(yīng)的 @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("3、hello servlet 被訪問了"); HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest; String method = httpServletRequest.getMethod(); if ("Get".equals(method)){ } if ("POST".equals(method)){ } }
到此這篇關(guān)于帶你快速上手Servlet的文章就介紹到這了,更多相關(guān)Servlet詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于springboot實(shí)現(xiàn)redis分布式鎖的方法
這篇文章主要介紹了基于springboot實(shí)現(xiàn)redis分布式鎖的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()
這篇文章主要介紹了JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()的相關(guān)資料,需要的朋友可以參考下2016-05-05Java 自動(dòng)安裝校驗(yàn)TLS/SSL證書
這篇文章主要介紹了Java 自動(dòng)安裝校驗(yàn)TLS/SSL證書的示例,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-10-10SpringBoot小程序推送信息的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot小程序推送信息的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04