ServletConfig对象和ServletContext对象
ServletConfig對象是servlet配置對象,(web.xml中的)servlet信息封裝在ServletConfig對象中,因此在一個web應用可存在多個ServletConfig。ServletContext對象是servlet上下文對象,而整個web.xml信息封裝在ServletContext對象中,因此 一個web應用中只有一個servletcontext對象,可能存在多個servletconfig對象。
ServletConfig
創建:在創建完servlet對象之后,在調用init方法之前創建,直接從有參數的init方法中得到。
API:
1 java.lang.String getInitParameter(java.lang.String name) 根據參數名獲取參數值 2 java.util.Enumeration getInitParameterNames() 獲取所有參數 3 ServletContext getServletContext() 得到servlet上下文對象 4 java.lang.String getServletName() 得到servlet的名稱注意:servlet的參數只能由當前的這個sevlet獲取。
如下,我想訪問e盤下的某個文件,想通過web.xml中配置,在提交請求到服務器時訪問。
web.xml
1 <servlet> 2 <servlet-name>ConfigDemo</servlet-name> 3 <servlet-class>gz.itcast.f_config.ConfigDemo</servlet-class> 4 <!-- 初始參數: 這些參數會在加載web應用的時候,封裝到ServletConfig對象中 --> 5 <init-param> 6 <param-name>path</param-name> 7 <param-value>e:/b.txt</param-value> 8 </init-param> 9 </servlet>ConfigDemo.java
public class ConfigDemo extends HttpServlet {/*** 以下兩段代碼GenericServlet已經寫了,我們無需編寫!!*//*private ServletConfig config;*//*** 1)tomcat服務器把這些參數會在加載web應用的時候,封裝到ServletConfig對象中 * 2)tomcat服務器調用init方法傳入ServletConfig對象*//*@Overridepublic void init(ServletConfig config) throws ServletException {this.config = config;}*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*** 讀取servlet的初始參數*/String path = this.getServletConfig().getInitParameter("path");File file = new File(path);//讀取內容BufferedReader br = new BufferedReader(new FileReader(file));String str = null;while( (str=br.readLine())!=null ){System.out.println(str);} }ServletContext對象
核心API
java.lang.String getContextPath() --得到當前web應用的路徑java.lang.String getInitParameter(java.lang.String name) --得到web應用的初始化參數 java.util.Enumeration getInitParameterNames() void setAttribute(java.lang.String name, java.lang.Object object) --域對象有關的方法 java.lang.Object getAttribute(java.lang.String name) void removeAttribute(java.lang.String name) RequestDispatcher getRequestDispatcher(java.lang.String path) --轉發(類似于重定向)java.lang.String getRealPath(java.lang.String path) --得到web應用的資源文件 java.io.InputStream getResourceAsStream(java.lang.String path)在web.xml配置web應用參數,如下:
<context-param><param-name>AAA</param-name><param-value>AAA's value</param-value> </context-param>在web.xml中配置了上述的參數,就相當于給整個web添加了一個新屬性,那么可以通過ServletContext訪問。而上邊的在servlet中添加一個<init-param>,相當于某個web應用的某個特定的servlet添加了一個屬性,所以只能通過該servlet的ServletConfig訪問。下面的例子描述ServletContext如何訪問:
public class ContextDemo2 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//得到SErvletContext對象ServletContext context = this.getServletContext();System.out.println("參數"+context.getInitParameter("AAA"));Enumeration<String> enums = context.getInitParameterNames();while(enums.hasMoreElements()){String paramName = enums.nextElement();String paramValue =context.getInitParameter(paramName);System.out.println(paramName+"="+paramValue);}//嘗試得到ConfigDemo中的servlet參數String path = this.getServletConfig().getInitParameter("path");System.out.println("path="+path);}}?
轉載于:https://www.cnblogs.com/K-artorias/p/7469640.html
總結
以上是生活随笔為你收集整理的ServletConfig对象和ServletContext对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 函数节流与防抖的实现
- 下一篇: tnsname.ora