码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet中读取参数的几种方式

时间:2016-09-08 23:11:12      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

为每一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();

              }

             

       }

Servlet中读取参数的几种方式

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5854698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!