Servlet编程:ServletConfig
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet编程:ServletConfig相关的知识,希望对你有一定的参考价值。
序号 | 对象 | 作用 |
---|---|---|
1 | HttpServletRequest请求对象 | 获取请求信息 |
2 | HttpServletResponse响应对象 | 设置响应对象 |
3 | ServletConfig对象 | servlet配置对象 |
4 | ServletContext对象 | servlet的上下文对象 |
9、ServletConfig对象
9.1、ServletConfig对象的作用
ServletConfig对象: 主要是用于加载servlet的初始化参数。在一个web应用可以存在多个ServletConfig对象(一个Servlet对应一个ServletConfig对象)
9.2、对象创建和得到
创建时机: 在创建完servlet对象之后,在调用init方法之前创建。
得到对象: getServletConfig()!!!
javax.servlet.GenericServlet类的部分源码:
package javax.servlet; import java.io.IOException; import java.util.Enumeration; public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable { private transient ServletConfig config; /** * Does nothing. All of the servlet initialization is done by one of the <code>init</code> methods. */ public GenericServlet() { } /** * Called by the servlet container to indicate that the servlet is being placed into service. */ public void init(ServletConfig config) throws ServletException { this.config = config; this.init(); } /** * A convenience method which can be overridden so that there‘s no need * to call <code>super.init(config)</code>. * * Instead of overriding init(ServletConfig), simply override * this method and it will be called by <code>GenericServlet.init(ServletConfig config)</code>. * The <code>ServletConfig</code> object can still be retrieved via getServletConfig. */ public void init() throws ServletException { } /** * Returns this servlet‘s ServletConfig object. */ public ServletConfig getServletConfig() { return config; } public String getServletName() { return config.getServletName(); } public String getInitParameter(String name) { return getServletConfig().getInitParameter(name); } public Enumeration getInitParameterNames() { return getServletConfig().getInitParameterNames(); } /** * Returns a reference to the ServletContext in which this servlet is running. */ public ServletContext getServletContext() { return getServletConfig().getServletContext(); } }
1)tomcat服务器把这些参数会在加载web应用的时候,封装到ServletConfig对象中
2)tomcat服务器调用init方法传入ServletConfig对象
9.3、Servlet的初始化参数配置和ServletConfig的API
在servlet的配置信息中,加上<init-param>,可以配置多个!!
servlet的初始化参数配置示例:
<servlet> <servlet-name>ConfigDemo</servlet-name> <servlet-class>com.rk.http.d_config.ConfigDemo</servlet-class> <!-- 初始参数: 这些参数会在加载web应用的时候,封装到ServletConfig对象中 --> <init-param> <param-name>path</param-name> <param-value>D:/rk/MyTest.txt</param-value> </init-param> <init-param> <param-name>website</param-name> <param-value>www.rk.com</param-value> </init-param> <init-param> <param-name>rk</param-name> <param-value>lsieun</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ConfigDemo</servlet-name> <url-pattern>/config</url-pattern> </servlet-mapping>
注意: servlet的参数只能由当前的这个sevlet获取!!!!
序号 | 函数 | 作用 |
---|---|---|
1 | String getServletName() | 得到servlet的名称 |
2 | String getInitParameter(String name) | 根据参数名获取参数值 |
3 | Enumeration getInitParameterNames() | 获取所有参数名 |
4 | ServletContext getServletContext() | 得到servlet上下文对象 |
示例代码:
package com.rk.http.d_config; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; 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 ConfigDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); ServletConfig config = this.getServletConfig();//得到Servlet对象 String servletName = config.getServletName();//得到servlet的名称 out.write("Servlet的名称:" + servletName + "<br/>"); out.write("<strong>Servet的配置信息path路径下的文件内容</strong>:<br/>"); String path = config.getInitParameter("path");//读取servlet的初始参数 File file = new File(path); FileReader reader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(reader); String line = null; while((line=bufferedReader.readLine()) != null) { out.write(line + "<br/>"); } out.write("<strong>Servlet配置信息:</strong><br/>"); Enumeration<String> initParameterNames = config.getInitParameterNames();//查询当前servlet的所有初始化参数 while(initParameterNames.hasMoreElements()) { String paramName = initParameterNames.nextElement(); String paramValue = config.getInitParameter(paramName); out.write(paramName + ":" + paramValue + "<br/>"); } } }
以上是关于Servlet编程:ServletConfig的主要内容,如果未能解决你的问题,请参考以下文章