當Servlet 容器啟動的時候 會為每個web應用創建一個ServletContext 對象代表當前的web應用。
在web.xml 文件中不止可以配置Servlet的初始化信息 還可以給整個web應用配置初始化信息。
1、獲取web 程序啟動時初始化參數
web.xml 設置需要初始化的參數
1
2
3
4
5
6
7
8
9
|
<!--1、獲取web應用程序初始化參數--> < 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 > |
寫一個Servlet繼承HttpServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * @Author: crush * @Date: 2021-05-09 16:32 * version 1.0 */ @WebServlet ( "/servlet" ) public class ServletContextTest extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //設置響應頭 resp.setContentType( "text/html;charset=utf-8" ); PrintWriter writer = resp.getWriter(); ServletContext servletContext = this .getServletContext(); // 根據名稱 獲取單個初始化參數 String parameter = servletContext.getInitParameter( "name" ); writer.print(parameter); writer.print( "<br><hr>" ); // 獲取全部初始化參數 Enumeration<String> initParameterNames = servletContext.getInitParameterNames(); while (initParameterNames.hasMoreElements()){ String name = initParameterNames.nextElement(); String value = servletContext.getInitParameter(name); writer.print(name+ ":" +value); writer.print( "<hr>" ); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
@WebServlet("/servlet6") 作用等于
1
2
3
4
5
6
7
8
|
< servlet > < servlet-name >/servlet1</ servlet-name > < servlet-class >com.crush.servlet.ServletContextTest</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >/servlet1</ servlet-name > < url-pattern >/servlet1</ url-pattern > </ servlet-mapping > |
2、實現多個Servlet 對象共享數據
一個web 應用中所有Servlet都共享ServletContext對象。在一定時候,ServletContext 也可以拿來傳遞信息
或者全局都需要的對象或者數據可以放進ServletContext中。
ServletContext接口的方法:這里講解增加、獲取、刪除、設置ServletContext 域屬性四個方法。
方法 | 描述 |
---|---|
Enumeration getAttributeNames(); | 返回一個Enumeration其中包含該ServletContext中所有的屬性名稱 |
Object getAttribute(String name); | 返回具有給定名稱的servlet容器屬性; |
void removeAttribute(String name); | 從此ServletContext中刪除具有給定名稱的屬性。 |
setAttribute(String name,Object obj) |
在此ServletContext中將對象綁定到給定的屬性名稱。 如果指定的名稱已經用于屬性,則此方法將使用新的屬性替換該屬性。 如果在ServletContext上配置了偵聽器,則容器會相應地通知它們。 |
設置值: ServletContextTest1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * @Author: crush * @Date: 2021-05-09 16:59 * version 1.0 */ @WebServlet ( "/servlet1" ) public class ServletContextTest1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 獲取ServletContext對象 ServletContext servletContext = this .getServletContext(); //設置值 ServletContext 域屬性 name 域屬性名 obj是值 // 往ServletContext 中放進 username=crush 這個鍵值對 servletContext.setAttribute( "username" , "crush" ); // 在控制臺給出提示 System.out.println( "值已經設置完成" ); // 重定向 // resp.sendRedirect("/servlet2"); // 轉發 req.getRequestDispatcher( "servlet2" ).forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
取出值 :ServletContextTest2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/** * @Author: crush * @Date: 2021-05-09 16:59 * version 1.0 */ @WebServlet ( "/servlet2" ) public class ServletContextTest2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 獲取ServletContext對象 ServletContext servletContext = this .getServletContext(); // 通過之前的設置的名字 取出username 的值 String username =(String) servletContext.getAttribute( "username" ); PrintWriter writer = resp.getWriter(); writer.print(username); //返回一個Enumeration其中包含該ServletContext中可用的屬性名稱 // Enumeration<String> attributeNames = servletContext.getAttributeNames(); //從此ServletContext中刪除具有給定名稱的屬性。 // servletContext.removeAttribute(""); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
測試:
先訪問 /servlet1 存值進去
再訪問 /servlet2 取值出來
3、讀取web應用下的資源
使用ServletContext 可以讀取web應用下的資源
常用方法
方法 | 描述 |
---|---|
String getRealPath(String path); | 獲取與給定虛擬路徑相對應的真實路徑。 |
InputStream getResourceAsStream(String path); | 返回位于指定路徑處的資源作為InputStream對象。 |
URL getResource(String path) | 返回映射到給定路徑的資源的URL。 |
mysql.properties
1
2
3
4
|
user =root password =123456 url=jdbc:mysql://localhost:3306/mysql drive=com |
讀取資源文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/** * 讀取資源內容 * @Author: crush * @Date: 2021-05-09 16:59 * version 1.0 */ @WebServlet ( "/servlet3" ) public class ServletContextTest3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType( "text/html;charset=utf-8" ); ServletContext servletContext = this .getServletContext(); // 獲取相對路徑中的輸入流對象 是要獲取web應用程序的路徑 InputStream inputStream = servletContext.getResourceAsStream( "\\WEB-INF\\classes\\mysql.properties" ); // 資源類對象 Properties properties = new Properties(); // load 從輸入字節流中讀取屬性列表(鍵和元素對) 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>" ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
對于mysql. properties 為什么是從WEB-INF/classes/ 目錄下取的解釋
resources和java文件夾下的 在web程序下的路徑都是classes的路徑下的 到之后我們會學到classpath的 一個路徑
會更了解一些。
第二種方式
直接將mysql.properties文件放在WEB-INF/目錄下
這個時候取的路徑就產生了變化了,可以直接那么取到
這個時候我們發現 如果文件是放在WEB-INF 下面 的話 編譯完后 是直接就在WEB-INF 下面 而不是在classes目錄下的。這個內容是maven 里的內容,好奇可以去查一查。
4、請求轉發
ServletContextTest5 轉發到 ServletContextTest 去
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/** * 請求轉發 * @Author: crush * @Date: 2021-05-09 16:59 * version 1.0 */ @WebServlet ( "/servlet5" ) public class ServletContextTest5 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType( "text/html;charset=utf-8" ); ServletContext servletContext = this .getServletContext(); // RequestDispatcher對象可用于將請求轉發到資源或將資源包括在響應中。 資源可以是動態的也可以是靜態的。 //路徑名必須以/開頭,并被解釋為相對于當前上下文根。 使用getContext獲取外部上下文中資源的RequestDispatcher servletContext.getRequestDispatcher( "/servlet" ).forward(req,resp); //forward:將請求從servlet轉發到服務器上的另一個資源(servlet,JSP文件或HTML文件) } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
亂碼解決
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/** * 亂碼問題 * @Author: crush * @Date: 2021-05-09 16:59 * version 1.0 */ @WebServlet ( "/servlet6" ) public class ServletContextTest6 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 解決亂碼 只是解決單獨的響應亂碼 如果有請求 還要設置請求的編碼 req.setCharacterEncoding( "utf-8" ); resp.setContentType( "text/html;charset=utf-8" ); PrintWriter writer = resp.getWriter(); writer.print( "你好啊,JavaWeb" ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } |
總結
本篇文章就到這里了,希望能給你帶來幫助,也希望能夠您能夠關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/weixin_45821811/article/details/116571600