[Servlet]研究ServletContext对象
作者信息
作者姓名:金云龍
個(gè)人站點(diǎn):http://www.longestory.com
個(gè)人公眾帳號(hào):搜索“longestory”或“龍哥有話(huà)說(shuō)”
ServletContext概述
ServletContext對(duì)象是Servlet三大域?qū)ο笾械囊粋€(gè),每一個(gè)Web應(yīng)用程序都擁有一個(gè)ServletContext對(duì)象,該對(duì)象是Web應(yīng)用程序的全局對(duì)象或者上下文。Tomcatserver在啟動(dòng)時(shí),會(huì)自己主動(dòng)創(chuàng)建一個(gè)ServletContext對(duì)象,在關(guān)閉時(shí),會(huì)自己主動(dòng)銷(xiāo)毀這個(gè)ServletContext對(duì)象。每一個(gè)Web應(yīng)用程序僅僅擁有一個(gè)ServletContext對(duì)象,ServletContext對(duì)象能夠在整個(gè)Web應(yīng)用中共享數(shù)據(jù)資源。
下列是ServletContext提供的方法列表:
| Object | getAttribute(String name) |
| Enumeration | getAttributeNames() |
| String | getInitParameter(String name) |
| Enumeration | getInitParameterNames() |
| String | getMimeType(String file) |
| String | getRealPath(String path) |
| String | getServletContextName() |
| Enumeration | getServletNames() |
| void | log(String msg) |
| void | removeAttribute(String name) |
| void | setAttribute(String name, Object object) |
獲取ServletContext對(duì)象
在自己定義Servlet中有以下幾種方式獲取到ServletContext對(duì)象:
- 通過(guò)ServletConfig對(duì)象的getServletContext()方法獲取。
- 通過(guò)繼承GenericServlet類(lèi)或HttpServlet類(lèi),調(diào)用GenericServlet類(lèi)或HttpServlet類(lèi)的getServletContext()方法獲取。
我們通過(guò)一個(gè)案例來(lái)討論一下。
- 首先,創(chuàng)建一個(gè)自己定義Servlet。用來(lái)獲取ServletContext對(duì)象。
- 在web.xml文件里配置Servlet相關(guān)信息。
- 將Web應(yīng)用程序公布到Tomcatserver,并啟動(dòng)Tomcatserver。
- 打開(kāi)瀏覽器。在地址欄中輸入http://localhost:8080/08_servlet/servlet/AServlet,在控制臺(tái)打印相關(guān)信息。
- 通過(guò)ServletContext對(duì)象的log(Stirng msg)方法,能夠向控制臺(tái)信息打印。
配置全局初始化參數(shù)
在web.xml文件里,使用定義的初始化參數(shù)。僅僅能在當(dāng)前Servlet中使用,而其它Servlet是無(wú)權(quán)限訪(fǎng)問(wèn)當(dāng)前Servlet下配置的初始化參數(shù)的。而能夠使用ServletContext在web.xml文件里配置全局初始化參數(shù),這樣當(dāng)前Web應(yīng)用程序中的全部Servlet都能夠訪(fǎng)問(wèn)。
- 在web.xml文件里使用<context-param>來(lái)定義全局初始化參數(shù)。
xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>weixin</param-name> <param-value>longestory</param-value> </context-param> <servlet> <servlet-name>AServlet</servlet-name> <servlet-class>app.java.context.AServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AServlet</servlet-name> <url-pattern>/servlet/AServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>BServlet</servlet-name> <servlet-class>app.java.context.BServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>BServlet</servlet-name> <url-pattern>/servlet/BServlet</url-pattern> </servlet-mapping> </web-app>- 在兩個(gè)自己定義Servlet中。分別利用ServletContext對(duì)象獲取全局初始化參數(shù)。
- 將Web應(yīng)用程序公布到Tomcatserver。并啟動(dòng)Tomcatserver。
- 打開(kāi)瀏覽器,在地址欄中分別輸入http://localhost:8080/08_servlet/servlet/AServlet和http://localhost:8080/08_servlet/servlet/BServlet,在控制臺(tái)打印相關(guān)信息。
在自己定義Servlet中,能夠通過(guò)ServletContext對(duì)象的getInitParameter(String name)方法獲取相應(yīng)參數(shù)名稱(chēng)的全局初始化參數(shù)值。也能夠通過(guò)ServletContext對(duì)象的getInitParameterNames()方法獲取全部全局初始化參數(shù)的名稱(chēng)。
還能夠通過(guò)ServletContext對(duì)象的getMineType(String file)方法依據(jù)文件擴(kuò)展名獲取文件MIME類(lèi)型。
public class BServlet extends GenericServlet {@Overridepublic void service(ServletRequest req, ServletResponse res)throws ServletException, IOException {ServletContext context = getServletContext();String html = context.getMimeType("1.html");String css = context.getMimeType("2.css");String javascript = context.getMimeType("3.js");System.out.println("HTML的文件類(lèi)型為"+html+", CSS的文件類(lèi)型為"+css+", javascript的文件類(lèi)型為"+javascript);} }公布Web應(yīng)用程序,并啟動(dòng)Tomcatserver,在控制臺(tái)中打印:
HTML的擴(kuò)展名為text/html, CSS的擴(kuò)展名為text/css, javascript的擴(kuò)展名為application/javascriptServletContext對(duì)象的getMineType(String file)方法會(huì)自己主動(dòng)讀取Tomcat安裝文件夾中conf文件夾中的web.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><mime-mapping><extension>html</extension><mime-type>text/html</mime-type> </mime-mapping> <mime-mapping><extension>css</extension><mime-type>text/css</mime-type> </mime-mapping><mime-mapping><extension>js</extension><mime-type>application/javascript</mime-type> </mime-mapping> </web-app>多個(gè)Servlet共享數(shù)據(jù)
在同一個(gè)Web應(yīng)用程序中,多個(gè)Servlet之間能夠共享ServletContext對(duì)象中的數(shù)據(jù)信息。
主要是通過(guò)ServletContext對(duì)象的setAttribute(String name, Object object)方法和getAttribute(String name)方法完畢,以下我們來(lái)實(shí)現(xiàn)統(tǒng)計(jì)站點(diǎn)訪(fǎng)問(wèn)次數(shù)的案例。
- 創(chuàng)建一個(gè)VisitServlet用來(lái)獲取訪(fǎng)問(wèn)次數(shù)。并存儲(chǔ)在ServletContext對(duì)象中。
- 創(chuàng)建一個(gè)ShowTimeServlet用來(lái)顯示訪(fǎng)問(wèn)次數(shù)。
- 配置web.xml文件里有關(guān)Servlet信息。
- 公布Web應(yīng)用程序到Tomcatserver,并啟動(dòng)Tomcatserver。
- 打開(kāi)瀏覽器。在地址欄輸入http://localhost:8080/08_servlet/visit。訪(fǎng)問(wèn)VisitServlet。
- 再新打開(kāi)瀏覽器。在地址欄輸入http://localhost:8080/08_servlet/show。顯示訪(fǎng)問(wèn)次數(shù)。
讀取Webproject中資源文件
讀取project中的資源文件,Java中的IO流事實(shí)上就能夠完畢,以下使用Java中的IO流完畢讀取資源文件。
- 首先在Webproject中,創(chuàng)建四個(gè)資源文件。
- 在Webproject的根文件夾下創(chuàng)建1.txt。
- 在Webproject的WebRoot文件夾下創(chuàng)建2.txt。
- 在Webproject的WebRoot文件夾的WEB-INF文件夾下創(chuàng)建3.txt。
- 在Webproject的src文件夾下創(chuàng)建4.txt。
- 創(chuàng)建一個(gè)Java文件用于讀取上述的四個(gè)資源文件。
- 執(zhí)行該Java文件會(huì)在控制臺(tái)打印響應(yīng)信息。
假設(shè)要想利用Servlet API的內(nèi)容來(lái)讀取Webproject中的資源文件,又要怎樣來(lái)做呢?ServletContext對(duì)象的getRealPath()方法能夠來(lái)完畢此項(xiàng)工作。
- 創(chuàng)建一個(gè)自己定義Servlet,使用ServletContext對(duì)象的getRealPath()方法來(lái)完畢讀取資源文件。
- 公布Web應(yīng)用程序到Tomcatserver。并啟動(dòng)Tomcatserver。
- 打開(kāi)瀏覽器。在地址欄中分別輸入http://localhost:8080/08_servlet/read,在控制臺(tái)打印相關(guān)信息。
除了能夠使用ServletContext對(duì)象的getRealPath()方法之外,還能夠使用ServletContext對(duì)象的getResourceAsStream()方法來(lái)完畢讀取資源文件的工作。
public class ReadFileServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/4.txt");IOUtils.copy(in, out);}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }另一種通用的方法:利用Class類(lèi)的getResource()方法也能夠完畢讀取資源文件的工作。
public class ReadFileServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 利用類(lèi)載入器讀取Webproject的資源文件String filename = ReadFileServlet.class.getResource("/4.txt").getFile();InputStream in = new FileInputStream(new File(filename));IOUtils.copy(in, out);}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }轉(zhuǎn)載說(shuō)明:請(qǐng)注明作者及原文鏈接。謝謝!
轉(zhuǎn)載于:https://www.cnblogs.com/llguanli/p/8400290.html
總結(jié)
以上是生活随笔為你收集整理的[Servlet]研究ServletContext对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《Linux高性能服务器编程》学习总结(
- 下一篇: 函数参数和函数返回值