关于ServletConfig与ServletContext的访问
2016-03-19 09:46
453 查看
一般来说,对于整个应用的配置,为了不使用"硬编码",应该使用ServletContext对象。
而如果只有一个特定的Servlet需要设定的参数,其他Servlet不能访问,那么一般要使用ServletConfig();
PS:在使用ServletConfig对象的时候,在init()方法中,一定要用super类初始化ServletConfig对象。
下面来逐个讨论:
一, ServletContext对象
<context-param>元素:设定Context起始参数
在web.xml中,您可以利用<context-param>元素来定义Context起始参数,它包含两个子元素:
n <param-name>:定义Context起始参数名称
n <param-value>:定义Context起始参数值
以下是<context-param>元素的使用范例,在本例中笔者定义了两个Context起始参数:
n driver_type:Web应用程序欲使用的JDBC驱动程序名称
n url:目标数据库位置
有两种方式存取Context起始参数的方式:
表1 在ServletContext接口中用来存取Context起始参数的方法
1.先调用getServletConfig()方法取得ServletConfig对象,再利用ServletConfig接口定义的getServletContext()方法取得ServletContext对象。
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
String driver_type = context.getInitParameter("drvier_type");
String url=context.getInitParameter("url");
2. 直接调用getServletContext()方法取得ServletContext对象。
ServletContext context = getServletContext();
//获得配置的参数
String driver_type = context.getInitParameter("drvier_type");
String url=context.getInitParameter("url");
//获得当前WebApp的路径
String path=context.getRealPath("/");
二, ServletConfig对象
<init-param>元素:设定init起始参数
在web.xml中,您可以利用<init-param>元素来定义Config起始参数,它包含两个子元素:
n <init-name>:定义Config起始参数名称
n <init-value>:定义Config起始参数值
以下是<init-param>元素的使用范例,在本例中笔者定义了两个Config起始参数:
n driver_type:Web应用程序欲使用的JDBC驱动程序名称
n url:目标数据库位置
而如果只有一个特定的Servlet需要设定的参数,其他Servlet不能访问,那么一般要使用ServletConfig();
PS:在使用ServletConfig对象的时候,在init()方法中,一定要用super类初始化ServletConfig对象。
public void init(ServletConfig config) throws ServletException { super.init(config); //TODO }
下面来逐个讨论:
一, ServletContext对象
<context-param>元素:设定Context起始参数
在web.xml中,您可以利用<context-param>元素来定义Context起始参数,它包含两个子元素:
n <param-name>:定义Context起始参数名称
n <param-value>:定义Context起始参数值
以下是<context-param>元素的使用范例,在本例中笔者定义了两个Context起始参数:
n driver_type:Web应用程序欲使用的JDBC驱动程序名称
n url:目标数据库位置
<web-app> <context-param> <param-name>driver_type</param-name> <param-value>oracle.jdbc.driver.OracleDriver</param-value> </context-param> <context-param> <param-name>url</param-name> <param-value>jdbc:oracle:thin:@IP:1521:SID</param-value> </context-param> </web-app>
有两种方式存取Context起始参数的方式:
表1 在ServletContext接口中用来存取Context起始参数的方法
方法名称 | 回传类型 | 用 途 |
getInitParameter() | String | 取得某个Context起始参数值 |
getInitParameterNames() | java.util.Enumeration | 取得所有Context起始参数 |
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
String driver_type = context.getInitParameter("drvier_type");
String url=context.getInitParameter("url");
2. 直接调用getServletContext()方法取得ServletContext对象。
ServletContext context = getServletContext();
//获得配置的参数
String driver_type = context.getInitParameter("drvier_type");
String url=context.getInitParameter("url");
//获得当前WebApp的路径
String path=context.getRealPath("/");
二, ServletConfig对象
<init-param>元素:设定init起始参数
在web.xml中,您可以利用<init-param>元素来定义Config起始参数,它包含两个子元素:
n <init-name>:定义Config起始参数名称
n <init-value>:定义Config起始参数值
以下是<init-param>元素的使用范例,在本例中笔者定义了两个Config起始参数:
n driver_type:Web应用程序欲使用的JDBC驱动程序名称
n url:目标数据库位置
<web-app> <servlet> <servlet-name>testServlet</servlet-name> <servlet-class>com.simon.test.servlet.initparam.testServlet</servlet-class> <init-param> <param-name>driver_type</param-name> <param-value>oracle.jdbc.driver.OracleDriver</param-value> </init-param> <init-param> <param-name>url</param-name> <param-value>jdbc:oracle:thin:@IP:1521:SID</param-value> </init-param> <servlet-mapping> <servlet-name>testServlet</servlet-name> <url-pattern>/testServlet</url-pattern> </servlet-mapping> </web-app>在init()方法中,应该:
public void init(ServletConfig config) throws ServletException { //必须要继承super类的init()方法 super.init(config); String filename=getServletConfig().getInitParameter("config-file"); //TODO }
相关文章推荐
- JabRef 初级阶段学习
- c语言 inline函数的总结
- 22.5-5求有向图的分量图
- Java实验:输出希腊字母
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- Singleton Pattern (单例模式)
- 压缩感知重构算法之SP算法python实现
- 大数据去重,你还在发愁吗?
- 文章标题
- [置顶] JAVA常用代码片段
- Ingress traffic control
- 卫条件
- 应用层杂谈
- 构建之法阅读笔记02
- mysql中insert into select from的使用
- 替换元素、非替换元素、行内元素框模型
- 博客搬家
- SVD与PCA,奇异值分解与主成分分析的比较
- jquery after append appendTo三个函数的区别
- Spring 注释 @Autowired 和@Resource 的区别