秒懂servletContext对象
servletContext對象
ServletContext對象,官方稱為servlet上下文;服務器會為每一個web應用創建一個servletContext對象,它具有全局唯一性,web應用中的所有servlet都共享這個對象,所以也稱之為全局應用程序共享對象;
作用
1.相對路徑轉為絕對路徑;
2.獲取容器的附加信息;
3.讀取配置信息;
4.全局容器;
相對路徑轉為絕對路徑
servletContext.getRealPath(“path”);
該方法可以將相對路徑轉為絕對路徑,在文件的上傳與下載的時候需要用到該方法進行路勁的轉換;
案例:通過服務器下載圖片到本機;
package com.bjsxt.servlet;import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream;/**** 文件下載* */ public class DownFileServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 當前沒有這個方法,但是父類有,調用父類的方法!在GenericServlet類中 // 獲取servletContext對象ServletContext servletContext = this.getServletContext(); // 路徑轉換String realPath = servletContext.getRealPath("image/我妻善逸.jpg");System.out.println(realPath);// 讀文件 // File file = new File("D:\\我妻善逸.jpg");File file = new File(realPath);FileInputStream fis = new FileInputStream(file);byte[] buff = new byte[fis.available()];fis.read(buff); // 在響應中添加附加信息 // 正常情況下 // resp.addHeader("Content-Disposition","attachment;filename="+file.getName()); // 圖片為中文名字的時候(本來應該用gbk的但是本機為日文系統所以用Shift_JIS)resp.addHeader("Content-Disposition","attachment;filename="+new String(file.getName().getBytes("Shift_JIS"),"iso-8859-1"));OutputStream os = resp.getOutputStream();os.write(buff);os.flush();os.close();} }Xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>downFileServlet</servlet-name><servlet-class>com.bjsxt.servlet.DownFileServlet</servlet-class></servlet><servlet-mapping><servlet-name>downFileServlet</servlet-name><url-pattern>/down.do</url-pattern></servlet-mapping> </web-app>獲取容器的附加信息(基本信息)
1.獲取servlet容器的名稱以及版本號
ServletContext.getBaseInfo();(返回string類型)
2.獲取容器所支持servlet的主版本號;
servletContext.getMajoyVersion();(返回int類型)
3.獲取容器所支持servlet的副版本號;
servletContext.getMinorVersion();(返回int類型)
案例:查看當前servlet容器的版本號、名稱,以及其所支持的servlet的版本號;
Xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>getBaseInfoServlet</servlet-name><servlet-class>com.bjsxt.servlet.GetBaseInfoServlet</servlet-class></servlet><servlet-mapping><servlet-name>getBaseInfoServlet</servlet-name><url-pattern>/getBaseInfo.do</url-pattern></servlet-mapping> </web-app>獲取配置信息
<context-param> <param-name>key</param-name> <param-value>value</param-value> </context-param>1.獲取xml文件中的context-param標簽的配置信息;
ServeltContext.getInitParameter(“key”);
2.獲取xml文件中所有param-name的值;
ServletContext.getInitParameterNames();(返回一個枚舉類型);
案例:獲取web.xml中context-param標簽下所有param-name的值以及其配置信息;
Xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><context-param><param-name>key1</param-name><param-value>value1</param-value></context-param><context-param><param-name>key2</param-name><param-value>value2</param-value></context-param><servlet><servlet-name>contextReadInfoServlet</servlet-name><servlet-class>com.bjsxt.servlet.ContextReadInfoServlet</servlet-class></servlet><servlet-mapping><servlet-name>contextReadInfoServlet</servlet-name><url-pattern>/context.do</url-pattern></servlet-mapping> </web-app>全局容器
?可以向ServletContext對象中緩沖一些數據,緩沖數據是,存儲數據結構為key:value;
?向ServletContext對象中緩沖數據后,其對應的web應用中的所有servlet都可以通過ServletContext對象獲取該數據;
1.向ServletContext對象中存儲數據;
ServletContext.setAttribute(“key”,ObjectValue);
2.從全局容器中獲取數據;
servletContext.getAttribute(“key”);
3.根據key從全局容器中刪除數據;
servletContext.removeAttribute(“key”);
案例:向ServletContext對象中存儲數據,并在客戶端瀏覽器上響應;
Xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>globalContainerServlet</servlet-name><servlet-class>com.bjsxt.servlet.GlobalContainerServlet</servlet-class> </servlet> <servlet-mapping><servlet-name>globalContainerServlet</servlet-name><url-pattern>/global.do</url-pattern> </servlet-mapping> </web-app>ServletContext對象的生命周期
Servlet容器啟動,創建ServletContext對象,servlet容器關閉,ServletContext對象的生命周期結束;
這樣看來ServletContext對象的生命周期非常長,所以不建議存放一些(經常改動的數據例如:業務數據);(可以存儲一些幾乎不改動的信息例如:配置信息;)
感謝您的觀看;
總結
以上是生活随笔為你收集整理的秒懂servletContext对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单易懂的多线程(通过实现Runnabl
- 下一篇: servletConfig对象