帶你快速上手Servlet
一、Servlet與Tomcat的關(guān)系
(1)Tomcat是什么?
Tomcat其實是Web服務(wù)器和Servlet容器的結(jié)合體
(2)什么是Web服務(wù)器?
比如,我當前在杭州,你能否用自己的電腦訪問我桌面上的一張圖片?恐怕不行,我們太習(xí)慣通過URL訪問的一個網(wǎng)站、下載一部電影了。一個資源,如果沒有URL映射,那么外界幾乎很難訪問,而Web服務(wù)器的作用說穿了就是:將某個主機上的資源映射為一個URL供外界訪問
二、什么是Servlet
(1)什么是Servlet容器?
Servlet是運行在Web服務(wù)器或應(yīng)用服務(wù)器上的程序。
Servlet容器,顧名思義里面存著Servlet對象,我們?yōu)槭裁茨軌蛲ㄟ^Web服務(wù)器映射的URL訪問資源?肯定需要寫程序處理請求,主要3個過程:接受請求,處理請求,響應(yīng)請求。
三、Servlet的類結(jié)構(gòu)

通過繼承HttpServlet實現(xiàn)Servlet接口
一般在實際項目開發(fā)中,都是使用繼承HttpServlet類的方式去實現(xiàn)Servlet程序
(1)編寫一個類去繼承HttpServlet類
(2)根據(jù)業(yè)務(wù)需要重寫doGet或doPost方法
(3)到web.xml中的配置servlet程序的訪問地址
四、ServletConfig類
ServletConfig代表的是當前Servlet在web.xml中的配置信息
String getServletName(); ---獲取當前Servlet在web.xml中配置的名字
ServletContext getServletContext();---獲取當前Servlet指定名稱的初始化參數(shù)的值
String getInitParameter(String var1);---獲取當前Servlet所有初始化參數(shù)的名字組成的枚舉
Enumeration<String> getInitParameterNames();---獲取代表當前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是一個接口,它表示Servlet上下文對象。
2、一個Web工程,只有一個ServletContext對象實例。
3、ServletContext是一個域?qū)ο蟆?/p>
4、ServletContext是在web工程部署啟動的時候創(chuàng)建,在web工程停止的時候銷毀。
什么是域?qū)ο螅?/p>
域?qū)ο?,是可以像Map一樣存取數(shù)據(jù)的對象,叫域?qū)ο蟆?/p>
這里的域指的是存取數(shù)據(jù)的操作范圍,整個web工程。
存數(shù)據(jù) 取數(shù)據(jù) 刪除數(shù)據(jù)
Map put() get() remove()
域?qū)ο?setAttribute() getAttribute() removeAttribute()
(2) ServletContext類的四個作用
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ù)(它是屬于整個web工程)-->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
2、獲取當前的工程路徑,格式:/工程路徑
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 閫氳繃鍏抽棴绔彛鎺ユ敹鍒版湁鏁堢殑鍏抽棴鍛戒護銆傛鍦ㄥ仠姝㈡湇鍔″櫒瀹炰緥銆�
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初始化方法
第一、二步,是在第一次訪問的時候創(chuàng)建Servlet程序會調(diào)用
(3)執(zhí)行 Service方法
第三步、每次訪問都會調(diào)用
(4)執(zhí)行destroy銷毀方法
第四步:在web工程停止的時候調(diào)用
七、Get、Post
get、post請求都會走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接入deepseek深度求索示例代碼(jdk1.8)
這篇文章主要介紹了SpringBoot接入deepseek深度求索的相關(guān)資料,包括建API?key、封裝詢問Deepseek的工具方法(在配置文件中添加key值)、調(diào)用測試并確保端口一致例如8091,最后運行結(jié)果,需要的朋友可以參考下2025-02-02
springboot + mybatis配置多數(shù)據(jù)源示例
本篇文章主要介紹了springboot + mybatis配置多數(shù)據(jù)源示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
springboot整合shardingjdbc實現(xiàn)分庫分表最簡單demo
我們知道分庫分表是針對某些數(shù)據(jù)量持續(xù)大幅增長的表,比如用戶表、訂單表等,而不是一刀切將全部表都做分片,這篇文章主要介紹了springboot整合shardingjdbc實現(xiàn)分庫分表最簡單demo,需要的朋友可以參考下2021-06-06
Spring Boot 中的自動配置autoconfigure詳解
這篇文章主要介紹了Spring Boot 中的自動配置autoconfigure詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
詳解JAVA后端實現(xiàn)統(tǒng)一掃碼支付:微信篇
本篇文章主要介紹了詳解JAVA后端實現(xiàn)統(tǒng)一掃碼支付:微信篇,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-01-01
tomcat目錄結(jié)構(gòu)簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了tomcat目錄結(jié)構(gòu)簡介_動力節(jié)點Java學(xué)院整理的相關(guān)資料,需要的朋友可以參考下2017-07-07

