ServletConfig物件
原出處:https://www.cnblogs.com/HigginCui/p/5961082.html
【ServletConfig物件簡述】
在Servlet的配置檔案中,可以使用一個或多個<init-param>標籤為Servlet配置一些初始化引數。
當Servlet配置了初始化引數後,web容器在建立Servlet例項物件時,會自動將這些初始化引數封裝到ServletConfig物件中,並在呼叫Servlet的init方法的時候,將ServletConfig物件傳遞給Servlet,進而,我們同通過ServletConfig物件就可以得到當前Servlet的初始化引數的資訊。
【ServletConfig的API】
String getInitParameter(String name) 得到引數
Enumeration getInitParameterNames() 得到所有引數
ServletContext getServletContext() 得到上下文物件
String getServletName() 得到Servlet名稱
【簡單案例----從init方法中獲取init-param中的引數值】
[ web.xml ]
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>HelloServlet</display-name> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.Higgin.servlet.FirstServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>Higgin</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> </web-app>
[ FirstServlet ]
package com.Higgin.servlet; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException;import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException { this.config=config; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=config.getInitParameter("name"); //使用ServletConfig物件的getParameter(param-name)來獲取對應的value值 System.out.println("name=="+name); System.out.println("doGet方法執行---"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); System.out.println("doPost方法執行---"); } }
[ 執行結果 ]
【簡單案例2----獲取init-param中的 指定的引數值 和 所有的引數值】
[ web.xml ]
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>HelloServlet</display-name> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.Higgin.servlet.FirstServlet</servlet-class> <init-param> <param-name>name</param-name> <param-value>Higgin</param-value> </init-param> <init-param> <param-name>age</param-name> <param-value>18</param-value> </init-param> <init-param> <param-name>eamil</param-name> <param-value>12345@qq.com</param-value> </init-param> <init-param> <param-name>sex</param-name> <param-value>男</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> </web-app>
[ FirstServlet.java ]
package com.Higgin.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到指定的值 String value =this.getServletConfig().getInitParameter("name"); System.out.println("指定name----------"+value); //得到所有的 Enumeration e=this.getServletConfig().getInitParameterNames(); while(e.hasMoreElements()){ String name=(String) e.nextElement(); String value1=this.getServletConfig().getInitParameter(name); System.out.println("遍歷"+name+"-------"+value1); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); System.out.println("doPost方法執行---"); } }
[ 執行結果 ]
[ 提示 ]
實際開發中,有一些不適合在Servlet程式中寫死的模組,這些資料可以通過配置方式給Servlet,例如Servlet採用那種編碼、Servlet連線哪個庫、Servlet哪個配置檔案。
相關文章
- java基礎學習:JavaWeb之ServletConfig與ServletContextJavaWebServletContext
- Servlet第二篇【Servlet呼叫圖、Servlet細節、ServletConfig、ServletContext】ServletContext
- session物件、cookie物件和appliaction物件Session物件CookieAPP
- Javascript 物件 – 字串物件JavaScript物件字串
- Javascript 物件 – 陣列物件JavaScript物件陣列
- Javascript 物件 – 數學物件JavaScript物件
- 函式物件、物件、原型函式物件原型
- 物件導向,搞定物件物件
- 物件獲取原型物件物件原型
- JavaScript 本地物件、內建物件和宿主物件JavaScript物件
- Golang中多大的物件算是大物件,多小的物件算是小物件?Golang物件
- JavaScript課程——Day07(物件簡介、Math物件、時間物件、字串物件)JavaScript物件字串
- JavaScript 複習之包裝物件,Boolean物件,Number物件JavaScript物件Boolean
- 偽物件、樁物件、模擬物件|單元測試物件
- jsp隱式物件-Servlet物件JS物件Servlet
- Django:F物件和Q物件Django物件
- JS筆記—— 物件 (原型物件)JS筆記物件原型
- Java物件導向——類與物件Java物件
- JavaScript內部物件和Date物件JavaScript物件
- 物件與物件引用的區別物件
- JavaScript物件與建立物件的方式JavaScript物件
- Js物件導向(1): 理解物件JS物件
- 物件導向-物件導向思想物件
- jquery物件如何轉化成DOM物件jQuery物件
- Django(26)HttpResponse物件和JsonResponse物件DjangoHTTP物件JSON
- Python執行緒專題4:Timer物件、Lock物件、Rlock物件Python執行緒物件
- JavaScript 基礎(二) – 建立 function 物件的方法, String物件, Array物件JavaScriptFunction物件
- js jquery 列印物件;json 物件轉字串jQuery物件JSON字串
- JS json字串轉物件、物件轉字串JSON字串物件
- python物件導向思想(類與物件)Python物件
- Python可變物件和不可變物件Python物件
- JS物件導向設計-建立物件JS物件
- JavaScript 複習之Navigator物件,Screen物件JavaScript物件
- JavaScript 複習之 Location物件,URL物件JavaScript物件
- date 物件物件
- Array物件物件
- history物件物件
- Location物件物件