当前位置: 移动技术网 > IT编程>开发语言>Java > 有关ServletConfig与ServletContext的访问

有关ServletConfig与ServletContext的访问

2019年07月22日  | 移动技术网IT编程  | 我要评论

一般来说,对于整个应用的配置,为了不使用"硬编码",应该使用servletcontext对象。

而如果只有一个特定的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起始参数

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:目标数据库位置  

<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
}

以上这篇有关servletconfig与servletcontext的访问就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网