zoukankan      html  css  js  c++  java
  • 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();

                  }

                 

           }

  • 相关阅读:
    Spring IOC(二)beanName 别名管理
    Spring IOC(六)依赖查找
    Spring IOC(五)依赖注入
    Spring IOC(七)类型推断
    Spring 循环引用(二)源码分析
    Spring 循环引用(一)一个循环依赖引发的 BUG
    Spring IOC(四)FactoryBean
    Spring 中的类加载机制
    Spring IOC(三)单例 bean 的注册管理
    Spring Environment(三)生命周期
  • 原文地址:https://www.cnblogs.com/kuillldan/p/5854698.html
Copyright © 2011-2022 走看看