JavaWeb 入门篇(3)ServletContext 详解 具体应用
當(dāng)Servlet 容器啟動(dòng)的時(shí)候 會(huì)為每個(gè)web應(yīng)用創(chuàng)建一個(gè)ServletContext 對(duì)象代表當(dāng)前的web應(yīng)用。
在web.xml 文件中不止可以配置Servlet的初始化信息 還可以給整個(gè)web應(yīng)用配置初始化信息。
1、獲取web 程序啟動(dòng)時(shí)初始化參數(shù)
web.xml 設(shè)置需要初始化的參數(shù)
<!--1、獲取web應(yīng)用程序初始化參數(shù)--> <context-param><param-name>name</param-name><param-value>crush</param-value> </context-param> <context-param><param-name>school</param-name><param-value>hngy</param-value> </context-param>寫一個(gè)Servlet繼承HttpServlet
/*** @Author: crush* @Date: 2021-05-09 16:32* version 1.0*/ @WebServlet("/servlet") public class ServletContextTest extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//設(shè)置響應(yīng)頭resp.setContentType("text/html;charset=utf-8");PrintWriter writer = resp.getWriter();ServletContext servletContext = this.getServletContext();// 根據(jù)名稱 獲取單個(gè)初始化參數(shù)String parameter = servletContext.getInitParameter("name");writer.print(parameter);writer.print("<br><hr>");// 獲取全部初始化參數(shù)Enumeration<String> initParameterNames = servletContext.getInitParameterNames();while (initParameterNames.hasMoreElements()){String name = initParameterNames.nextElement();String value = servletContext.getInitParameter(name);writer.print(name+":"+value);writer.print("<hr>");}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }@WebServlet("/servlet6")
作用等于
2、實(shí)現(xiàn)多個(gè)Servlet 對(duì)象共享數(shù)據(jù)
一個(gè)web 應(yīng)用中所有Servlet都共享ServletContext對(duì)象。在一定時(shí)候,ServletContext 也可以拿來(lái)傳遞信息
或者全局都需要的對(duì)象或者數(shù)據(jù)可以放進(jìn)ServletContext中。
ServletContext接口的方法:這里講解增加、獲取、刪除、設(shè)置ServletContext 域?qū)傩运膫€(gè)方法。
| Enumeration getAttributeNames(); | 返回一個(gè)Enumeration其中包含該ServletContext中所有的屬性名稱 |
| Object getAttribute(String name); | 返回具有給定名稱的servlet容器屬性; |
| void removeAttribute(String name); | 從此ServletContext中刪除具有給定名稱的屬性。 |
| setAttribute(String name,Object obj) | 在此ServletContext中將對(duì)象綁定到給定的屬性名稱。 如果指定的名稱已經(jīng)用于屬性,則此方法將使用新的屬性替換該屬性。 如果在ServletContext上配置了偵聽器,則容器會(huì)相應(yīng)地通知它們。 |
設(shè)置值: ServletContextTest1
/*** @Author: crush* @Date: 2021-05-09 16:59* version 1.0*/ @WebServlet("/servlet1") public class ServletContextTest1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 獲取ServletContext對(duì)象ServletContext servletContext = this.getServletContext();//設(shè)置值 ServletContext 域?qū)傩?name 域?qū)傩悦?obj是值// 往ServletContext 中放進(jìn) username=crush 這個(gè)鍵值對(duì)servletContext.setAttribute("username","crush");// 在控制臺(tái)給出提示System.out.println("值已經(jīng)設(shè)置完成");// 重定向 // resp.sendRedirect("/servlet2");// 轉(zhuǎn)發(fā)req.getRequestDispatcher("servlet2").forward(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }取出值 :ServletContextTest2
/*** @Author: crush* @Date: 2021-05-09 16:59* version 1.0*/ @WebServlet("/servlet2") public class ServletContextTest2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 獲取ServletContext對(duì)象ServletContext servletContext = this.getServletContext();// 通過(guò)之前的設(shè)置的名字 取出username 的值String username =(String) servletContext.getAttribute("username");PrintWriter writer = resp.getWriter();writer.print(username);//返回一個(gè)Enumeration其中包含該ServletContext中可用的屬性名稱// Enumeration<String> attributeNames = servletContext.getAttributeNames();//從此ServletContext中刪除具有給定名稱的屬性。 // servletContext.removeAttribute("");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }測(cè)試:
先訪問(wèn) /servlet1 存值進(jìn)去
再訪問(wèn) /servlet2 取值出來(lái)
3、讀取web應(yīng)用下的資源
使用ServletContext 可以讀取web應(yīng)用下的資源
常用方法
| String getRealPath(String path); | 獲取與給定虛擬路徑相對(duì)應(yīng)的真實(shí)路徑。 |
| InputStream getResourceAsStream(String path); | 返回位于指定路徑處的資源作為InputStream對(duì)象。 |
| URL getResource(String path) | 返回映射到給定路徑的資源的URL。 |
mysql.properties
user=root password=123456 url=jdbc:mysql://localhost:3306/mysql drive=com讀取資源文件
/*** 讀取資源內(nèi)容* @Author: crush* @Date: 2021-05-09 16:59* version 1.0*/ @WebServlet("/servlet3") public class ServletContextTest3 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charset=utf-8");ServletContext servletContext = this.getServletContext();// 獲取相對(duì)路徑中的輸入流對(duì)象 是要獲取web應(yīng)用程序的路徑InputStream inputStream = servletContext.getResourceAsStream("\\WEB-INF\\classes\\mysql.properties");// 資源類對(duì)象Properties properties = new Properties();// load 從輸入字節(jié)流中讀取屬性列表(鍵和元素對(duì))properties.load(inputStream);PrintWriter writer = resp.getWriter();writer.print("user"+properties.getProperty("user")+"<br>");writer.print("password"+properties.getProperty("password")+"<br>");writer.print("url"+properties.getProperty("url")+"<br>");writer.print("drive"+properties.getProperty("drive")+"<br>");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }對(duì)于mysql. properties 為什么是從WEB-INF/classes/ 目錄下取的解釋
resources和java文件夾下的 在web程序下的路徑都是classes的路徑下的 到之后我們會(huì)學(xué)到classpath的 一個(gè)路徑
會(huì)更了解一些。
第二種方式
直接將mysql.properties文件放在WEB-INF/目錄下
這個(gè)時(shí)候取的路徑就產(chǎn)生了變化了,可以直接那么取到
這個(gè)時(shí)候我們發(fā)現(xiàn) 如果文件是放在WEB-INF 下面 的話 編譯完后 是直接就在WEB-INF 下面 而不是在classes目錄下的。這個(gè)內(nèi)容是maven 里的內(nèi)容,好奇可以去查一查。
4、請(qǐng)求轉(zhuǎn)發(fā)
ServletContextTest5 轉(zhuǎn)發(fā)到 ServletContextTest 去
/*** 請(qǐng)求轉(zhuǎn)發(fā)* @Author: crush* @Date: 2021-05-09 16:59* version 1.0*/ @WebServlet("/servlet5") public class ServletContextTest5 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charset=utf-8");ServletContext servletContext = this.getServletContext();// RequestDispatcher對(duì)象可用于將請(qǐng)求轉(zhuǎn)發(fā)到資源或?qū)①Y源包括在響應(yīng)中。 資源可以是動(dòng)態(tài)的也可以是靜態(tài)的。//路徑名必須以/開頭,并被解釋為相對(duì)于當(dāng)前上下文根。 使用getContext獲取外部上下文中資源的RequestDispatcherservletContext.getRequestDispatcher("/servlet").forward(req,resp);//forward:將請(qǐng)求從servlet轉(zhuǎn)發(fā)到服務(wù)器上的另一個(gè)資源(servlet,JSP文件或HTML文件)}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }亂碼解決
/*** 亂碼問(wèn)題* @Author: crush* @Date: 2021-05-09 16:59* version 1.0*/ @WebServlet("/servlet6") public class ServletContextTest6 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 解決亂碼 只是解決單獨(dú)的響應(yīng)亂碼 如果有請(qǐng)求 還要設(shè)置請(qǐng)求的編碼req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");PrintWriter writer = resp.getWriter();writer.print("你好啊,JavaWeb");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }自言自語(yǔ)
好像除了努力好像沒有別的捷徑了。
總結(jié)
以上是生活随笔為你收集整理的JavaWeb 入门篇(3)ServletContext 详解 具体应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: org.apache.ibatis.bi
- 下一篇: 新闻系统粗略说明文档