Servlet中读取参数的几种方式

为每一Servlet设置初始化参数

可以为每一个Servlet在对应的web.xml中的Servlet节点下编写初始化参数,格式如下:

<init-param>

                <param-name>userName</param-name>

                <param-value>admin</param-value>

</init-param>

然后在servlet中用如下代码获取相应的参数:

ServletConfig config = this.getServletConfig();

this.username = config.getInitParameter("userName");

 

为所有的Servlet设置公用的初始化参数

可以为所有的Servlet设置公用初始化参数,该参数和上面的参数有所不同,上面的要放在对应的Servlet节点下,而公用参数不用也不能放在Servlet节点下。

<context-param>

<param-name>userName</param-name>

            <param-value>admin</param-value>

</context-param>

同样在Servlet中可以通过如下代码获取我们设置的全局配置信息对象:

ServletContext context = this.getServletContext();

String userNameInGlobal = context.getInitParameter("userName");

 

在代码中设置公用属性

可以在代码中为ServletContext设置属性,然后就可以在任意地方获取了。该属性是全局属性,一旦设置成功后,在整个容器中均可以使用。

ServletContext context = this.getServletContext();

context.setAttribute("maxNumber", 999);

int maxNumebrInContext = (int)context.getAttribute("maxNumber");

 

 

读取外部文件中的配置信息

通过ServletContext对象读取外部文件中的配置信息主要有三种形式:

  • getResource
    • 返回一个URL对象,然后调用openStream()方法获取InputStream
    • getResourceAsStream
      • 直接返回InputStream对象
      • getRealPath
        • 返回资源文件的绝对路径,然后通过绝对路径用FileInputStream类读取数据

在classes目录下有一个Person.properties文件,内容如下:

 

getResource获取外部文件

@Override

public void init() throws ServletException

{

       ServletContext ctx = this.getServletContext();

       String resourcePath = "/WEB-INF/classes/Person.properties";

       try

       {

              URL url = ctx.getResource(resourcePath);

              InputStream is = url.openStream();

              String name = getPropertiesByKey("name", is);

              System.out.println(name);

       } catch (MalformedURLException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       } catch (IOException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       }

}

 

 

//从Stream中读取properties信息

public static String getPropertiesByKey(String key, InputStream is)

{

       Properties properties = new Properties();

       try

       {

              properties.load(is);

       } catch (IOException e)

       {

              // TODO Auto-generated catch block

              e.printStackTrace();

       }

      

       String value = (String) properties.get(key);

       return value;

}

 

 

getResourceAsStream获取外部文件

       public void init() throws ServletException

       {

              ServletContext ctx = this.getServletContext();

              String resourcePath = "/WEB-INF/classes/Person.properties";

              InputStream is = ctx.getResourceAsStream(resourcePath);

              String age = getPropertiesByKey("age", is);

              System.out.println("年龄是: "+ age);

       }

 

getRealPath获取外部文件

       public void init() throws ServletException

       {

              ServletContext ctx = this.getServletContext();

              String resourcePath = "/WEB-INF/classes/Person.properties";

              String resourceRealPath = ctx.getRealPath(resourcePath);

              try

              {

                     InputStream is = new FileInputStream(new File(resourceRealPath));

                     String address = getPropertiesByKey("address", is);

                     System.out.println("地址是:" + address);

              } catch (FileNotFoundException e)

              {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

             

       }

转载于:https://www.cnblogs.com/kuillldan/p/5854698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值