日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

[Java拾遗三]JavaWeb基础之Servlet

發(fā)布時(shí)間:2025/3/20 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Java拾遗三]JavaWeb基础之Servlet 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Servlet
???
1,servlet介紹
??????? servlet是一項(xiàng)動(dòng)態(tài)web資源開(kāi)發(fā)技術(shù).
??????? 運(yùn)行在服務(wù)器端.
??????? 作用:處理業(yè)務(wù)邏輯,生成動(dòng)態(tài)的內(nèi)容,返回給瀏覽器.
??????? 本質(zhì)就是一個(gè)類(lèi)
???? servlet的入門(mén)
??????? 1.編寫(xiě)servlet(類(lèi))--- 繼承HttpServlet
??????? 2.編寫(xiě)關(guān)系--- web.xml(在WEB-INF下)
??????? 3.訪問(wèn):
??????????? 路徑:http://localhost:80/serveltDemo/helloWorld
??? servlet的體系結(jié)構(gòu)及其常見(jiàn)api
??????? Servlet-- 接口
??????????? |
??????????? |
??????? GenericServlet---(抽象類(lèi))
??????????? |
??????????? |
??????? HttpServlet--(抽象類(lèi))
???????
??????? 常用的方法:
??????????? servlet的常用方法:
??????????????? void init(ServletConfig):初始化方法
??????????????? void service(ServletRequest,ServletResponse):服務(wù)--處理業(yè)務(wù)邏輯
??????????????? void destroy():銷(xiāo)毀方法
???????????????
??????????????? ServletConfig getServletConfig():返回一個(gè)servlet的配置對(duì)象
??????????????? String getServletInfo() :返回是servlet一些信息,比如作者 版本
???????????
??????????? GenericServlet的常用方法:
??????????????? 實(shí)現(xiàn)了除了service方法之外的所有方法
??????????????? init():自己重寫(xiě)init方法
??????????? HttpServlet的常用方法:
??????????????? service(HttpServletRequest,HttpServletResponse):服務(wù)--處理業(yè)務(wù)邏輯
??????????????? doGet(HttpServletRequest,HttpServletResponse):處理get請(qǐng)求的
??????????????? doPost(HttpServletRequest,HttpServletResponse):處理post請(qǐng)求的
? ? 代碼示例:

1 public class HelloWorldServlet extends HttpServlet { 2 @Override 3 public void doGet(HttpServletRequest req, HttpServletResponse resp) 4 throws ServletException, IOException { 5 System.out.println("hello world servlet"); 6 } 7 } HelloServlet.java <servlet-mapping><servlet-name>HelloWroldServlet</servlet-name><url-pattern>/helloWorld</url-pattern> </servlet-mapping><servlet><servlet-name>LifeServlet</servlet-name><servlet-class>cn.augmentum.b_life.LifeServlet</servlet-class> </servlet>? ? ?

? ? 2,servlet生命周期:
?????? 生命周期:什么時(shí)候來(lái)的,什么時(shí)候走的.
??????????? void init(ServletConfig):初始化方法
???????????????? * 執(zhí)行時(shí)間:默認(rèn)情況來(lái)說(shuō),第一次的請(qǐng)求的時(shí)候執(zhí)行
???????????????? * 執(zhí)行次數(shù):一次
???????????????? * 執(zhí)行者:tomcat
??????????? void service(ServletRequest,ServletResponse):服務(wù)--處理業(yè)務(wù)邏輯
??????????????? * 執(zhí)行時(shí)間:有請(qǐng)求就執(zhí)行
??????????????? * 執(zhí)行次數(shù):請(qǐng)求一次,執(zhí)行一次
??????????????? * 執(zhí)行者:tomcat
??????????? void destroy():銷(xiāo)毀方法???
??????????????? * 執(zhí)行時(shí)間:服務(wù)器正常關(guān)閉的時(shí)候執(zhí)行
??????????????? * 執(zhí)行次數(shù):一次
??????????????? * 執(zhí)行者:tomcat
??????? servlet是單實(shí)例多線程的
??????????? 默認(rèn)的情況下是第一次訪問(wèn)的時(shí)候創(chuàng)建(有服務(wù)器創(chuàng)建),每出現(xiàn)一次請(qǐng)求,創(chuàng)建一個(gè)線程,用來(lái)處理請(qǐng)求,
??????????? 直到服務(wù)器正常關(guān)閉或者項(xiàng)目移除的時(shí)候servlet銷(xiāo)毀
?? url-pattern編寫(xiě)
??????? 1.完全匹配,----(servlet最常用的)以"/"開(kāi)頭
??????????? 例如:/a/b/c/hello
??????? 2.目錄匹配 以"/"開(kāi)頭,以"*"結(jié)尾(filter常用的)
??????????? 例如:/a/*
??????? 3.后綴名匹配 以"*"開(kāi)頭?
??????????? 例如:? *.do?? *.jsp
???????
??????? 優(yōu)先級(jí):
??????????? 完全匹配>目錄匹配>后綴名匹配
???????
??????? 有如下的一些映射關(guān)系:
??????????? Servlet1 映射到 /abc/*
??????????? Servlet2 映射到 /*
??????????? Servlet3 映射到 /abc
??????????? Servlet4 映射到 *.do
??????? 問(wèn)題:
??????????? 當(dāng)請(qǐng)求URL為“/abc/a.html”,“/abc/*”和“/*”都匹配,哪個(gè)servlet響應(yīng)
??????????????? Servlet引擎將調(diào)用Servlet1。
??????????? 當(dāng)請(qǐng)求URL為“/abc”時(shí),“/abc/*”和“/abc”都匹配,哪個(gè)servlet響應(yīng)
??????????????? Servlet引擎將調(diào)用Servlet3。
??????????? 當(dāng)請(qǐng)求URL為“/abc/a.do”時(shí),“/abc/*”和“*.do”都匹配,哪個(gè)servlet響應(yīng)
??????????????? Servlet引擎將調(diào)用Servlet1。
??????????? 當(dāng)請(qǐng)求URL為“/a.do”時(shí),“/*”和“*.do”都匹配,哪個(gè)servlet響應(yīng)
??????????????? Servlet引擎將調(diào)用Servlet2.
??????????? 當(dāng)請(qǐng)求URL為“/xxx/yyy/a.do”時(shí),“/*”和“*.do”都匹配,哪個(gè)servlet響應(yīng)
??????????????? Servlet引擎將調(diào)用Servlet2。
???????? tomcat的defalutservlet處理別的servlet處理不了的請(qǐng)求.
?? load-on-startup
??????? 在servlet標(biāo)簽下
??????? 改變servlet初始化時(shí)機(jī).
??????? 若值>=0的時(shí)候,servlet會(huì)隨著服務(wù)器的啟動(dòng)而創(chuàng)建.
??????? 值越小,優(yōu)先級(jí)越高
??? 瀏覽器的路徑編寫(xiě):
??????? 1.瀏覽器直接輸入
??????? 2.a標(biāo)簽
??????? 3.location.href
??????? 4.表單提交
???????
??????? 路徑的寫(xiě)法:
??????????? 1.絕對(duì)路徑
??????????????? 帶協(xié)議的絕對(duì)路徑--一般訪問(wèn)站外資源的時(shí)候用
??????????????????? http://localhost:80/servletDemo/helloWorld
??????????????? 不帶協(xié)議的絕對(duì)路徑--站內(nèi)資源
??????????????????? /servletDemo/helloWorld
??????????? 2.相對(duì)路徑
??????????????? ./(路徑) 和 ../
??????????????? 八字方針
??????????????????? 當(dāng)前路徑? http://localhost/servletDemo/??? index.html
??????????????????? 訪問(wèn)路徑? http://localhost/servletDemo/??? demoa
???????????????????
??????????????????? 當(dāng)前路徑? http://localhost/servletDemo/?? a/b/c??? ../../demoa
??????????????????? 訪問(wèn)路徑? http://localhost/servletDemo/?? demoa
??????? 以后使用的是絕對(duì)路徑(一般使用的是不帶協(xié)議的絕對(duì)路徑)
???????
3,ServletConfig
??? 他是當(dāng)前servlet的配置對(duì)象.
??? 獲取方式:
??????? ServletConfig config=this.getServletConfig();
??? 作用:
??????? 1.可以獲取到當(dāng)前servlet初始化參數(shù)
??????? 2.可以獲得全局管理者
??? 常用方法:
??????? String getServletName():獲取當(dāng)前servlet的名稱(chēng) --指的是web.xml中servlet名字
??????? String getInitParameter(name):獲取指定的參數(shù)
??????? Enumeration getInitParameterNames():獲取全部初始化參數(shù)名稱(chēng)
???????
??????? ServletContext getServletContext():獲取全局管理者
??? 當(dāng)servlet初始化的時(shí)候,執(zhí)行了init(ServletConfig config),就把創(chuàng)建好的servletConfig傳給了servlet
??????? 由tomcat創(chuàng)建

示例代碼:

1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 ServletConfig conf=this.getServletConfig(); 3 //獲取當(dāng)前servlet名字 4 String name=conf.getServletName(); 5 System.out.println(name); 6 7 //獲取名字為 "姓名"的參數(shù)值 8 String value = conf.getInitParameter("姓名"); 9 System.out.println(value); 10 11 //獲取全部參數(shù)名稱(chēng) 12 Enumeration<String> names=conf.getInitParameterNames(); 13 while(names.hasMoreElements()){ 14 String name_=names.nextElement(); 15 System.out.println(name_+":"+conf.getInitParameter(name_)); 16 } 17 } View Code

?


? ? ?
4,ServletContext
??? 全局管理者.
??? 每個(gè)應(yīng)用在tomcat啟動(dòng)的時(shí)候,就會(huì)創(chuàng)建一個(gè)servletcontext對(duì)象,這個(gè)對(duì)象可以獲取到當(dāng)前應(yīng)用的所有信息,實(shí)現(xiàn)資源共享.
??? 本質(zhì)servletcontext就是對(duì)當(dāng)前應(yīng)用的一個(gè)引用.
??? 作用:
??????? 1.實(shí)現(xiàn)資源共享
??????? 2.獲取全局初始化參數(shù).
??????? 3.獲取資源.
??? 怎么獲取:
??????? this.getServletConfig().getServletContext():
??????? getServletContext()也可以獲取
??? 常見(jiàn)的方法:
??????? String getInitparameter(name);獲取指定的參數(shù)
??????? Enumeration getInitParameterNames():獲取全部的參數(shù)名稱(chēng)
??????????? 參數(shù)???
??????????????? <context-param>
??????????????????? <param-name>
??????????????????? <param-value>
???????????????????
??????? String getRealPath(path):返回相應(yīng)文件在tomcat的實(shí)際路徑
??????????? 例如:D:\JavaTools\apache-tomcat-7.0.53\webapps\servletDemo\
??????? InputStream getResourceAsStream(String path) :以流的形式返回對(duì)應(yīng)的文件.
???????
??????? String getMimeType(String file)?? 可以獲取一個(gè)文件的mimeType類(lèi)型. 例如 text/html
??????? URL getResource(String path) 它返回的是一個(gè)資源的URL??????? 例如:? localhost/day09/a.html
??? 域?qū)ο?
??????? 把他看成一個(gè)map,
??????? xxxAttribute()
??????????? setAttribute(String,Object):添加
??????????? Object getAttribute(string):獲取
??????????? removeAttribute(string):移除
示例代碼:

1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 //首先獲取ServletContext對(duì)象 3 ServletContext context=this.getServletContext(); 4 5 //獲取url 6 String url=context.getInitParameter("url"); 7 //System.out.println(url); 8 9 //獲取全部的全局初始化參數(shù) 10 Enumeration<String> paramNames=context.getInitParameterNames(); 11 while(paramNames.hasMoreElements()){ 12 String name=paramNames.nextElement(); 13 ///System.out.println(name+":"+context.getInitParameter(name)); 14 } 15 16 //返回 "/"的路徑 17 //String path=context.getRealPath("/"); 18 //String path = context.getRealPath("/5.txt"); // 錯(cuò)誤的 19 //System.out.println(path); 20 /*InputStream in = context.getResourceAsStream("/WEB-INF/classes/cn/itcast/e_servletcontext/5.txt"); 21 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 22 String readLine = reader.readLine(); 23 System.out.println(readLine);*/ 24 25 //獲取index.htmlmime類(lèi)型 26 27 String type = context.getMimeType("/a.html"); 28 //System.out.println(type); 29 URL url_ = context.getResource("/a.html"); 30 System.out.println(url_.getPath()); 31 } View Code

? ? ?

5,classpath
??? 獲取字節(jié)碼文件的路徑.
??? 通過(guò)字節(jié)碼文件獲取
??????? 當(dāng)前類(lèi).class.getResource("/").getPath():返回classes的目錄
??????????? D:/JavaTools/apache-tomcat-7.0.53/webapps/day09/WEB-INF/classes/
??????? 當(dāng)前類(lèi).class.getResource("").getPath():返回當(dāng)前類(lèi)的字節(jié)碼文件所在的目錄
??????????? D:/JavaTools/apache-tomcat-7.0.53/webapps/day09/WEB-INF/classes/cn/itcast/h_classpath/
???????????
??? 通過(guò)類(lèi)加載器獲取文件路徑
??????? 當(dāng)前類(lèi).class.getClassLoader().getResource("/").getPath():返回classes的目錄
??????????? D:/JavaTools/apache-tomcat-7.0.53/webapps/day09/WEB-INF/classes/
??????? 當(dāng)前類(lèi).class.getClassLoader().getResource().getPath():返回classes的目錄
??????????? D:/JavaTools/apache-tomcat-7.0.53/webapps/day09/WEB-INF/classes/
示例代碼:

1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 //String path1 = PathServlet.class.getResource("/").getPath(); 3 // D:/JavaTools/apache-tomcat-7.0.53/webapps/day09/WEB-INF/classes/ 4 //String path2 = PathServlet.class.getResource("").getPath(); 5 // D:/JavaTools/apache-tomcat-7.0.53/webapps/day09/WEB-INF/classes/cn/augmentum/h_classpath/ 6 // System.out.println(path1); 7 // System.out.println(path2); 8 9 //String path3 = PathServlet.class.getClassLoader().getResource("/").getPath(); 10 //String path4 = PathServlet.class.getClassLoader().getResource("").getPath(); 11 12 //System.out.println(path3); 13 //System.out.println(path4); 14 // myeclipse tomcat路徑 獲取 15 //1.txt src /WEB-INF/classes/1.txt lei.class.getResource("/").getpath() 16 //2.txt webroot /2.txt context.getRealPath 17 //3.txt web-inf /WEB-INF/3.txt context.getRealPath 18 //4.txt cn.augmentum.h_classpath /WEB-INF/classes/cn/augmentum/h_classpath/4.txt lei.class.getResource("").getpath() 19 20 ServletContext context=this.getServletContext(); 21 String path1 = PathServlet.class.getResource("/1.txt").getPath(); 22 String path2 = context.getRealPath("/2.txt"); 23 String path3 = context.getRealPath("/WEB-INF/3.txt"); 24 String path4 = PathServlet.class.getResource("4.txt").getPath(); 25 26 /*System.out.println(path1); 27 System.out.println(path2); 28 System.out.println(path3); 29 System.out.println(path4);*/ 30 readFile(path1); 31 readFile(path2); 32 readFile(path3); 33 readFile(path4); 34 } View Code


小demo: 當(dāng)訪問(wèn)index.html中的 鏈接則通過(guò)CountServlet計(jì)數(shù), 每訪問(wèn)一次則count加1, 然后通過(guò)ShowServlet展示到控制臺(tái):

1 public class CountServlet extends HttpServlet { 2 3 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 4 //訪問(wèn)計(jì)數(shù) 5 //首先獲取context對(duì)象 6 ServletContext context=this.getServletContext(); 7 //請(qǐng)求一次,context取出訪問(wèn)次數(shù) 8 Integer count=(Integer) context.getAttribute("count"); 9 //次數(shù)+1,放入context中 10 if (count==null) { 11 context.setAttribute("count", 1); 12 }else{ 13 context.setAttribute("count", ++count); 14 } 15 } 16 17 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 doGet(request, response); 19 } 20 21 } CountServlet.java 1 public class ShowServlet extends HttpServlet { 2 3 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 4 //首先獲取context 5 ServletContext context=this.getServletContext(); 6 //取次數(shù) 7 Integer count=(Integer) context.getAttribute("count"); 8 System.out.println("訪問(wèn)的次數(shù)為:"+(count==null?0:count)); 9 } 10 11 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 doGet(request, response); 13 } 14 15 } ShowServlet.java 1 <body> 2 <a href="/servletDemo/count">顯示次數(shù)</a><br/> 3 <a href="/servletDemo/show">展示結(jié)果</a><br/> 4 </body> index.html 1 <servlet> 2 <servlet-name>CountServlet</servlet-name> 3 <servlet-class>cn.augmentum.showcount.CountServlet</servlet-class> 4 </servlet> 5 <servlet> 6 <servlet-name>ShowServlet</servlet-name> 7 <servlet-class>cn.augmentum.showcount.ShowServlet</servlet-class> 8 </servlet> 9 <servlet-mapping> 10 <servlet-name>CountServlet</servlet-name> 11 <url-pattern>/count</url-pattern> 12 </servlet-mapping> 13 <servlet-mapping> 14 <servlet-name>ShowServlet</servlet-name> 15 <url-pattern>/show</url-pattern> 16 </servlet-mapping> web.xml

轉(zhuǎn)載于:https://www.cnblogs.com/wang-meng/p/5389214.html

總結(jié)

以上是生活随笔為你收集整理的[Java拾遗三]JavaWeb基础之Servlet的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。