zoukankan      html  css  js  c++  java
  • [Java][Web]ServletContext 方法的应用

    由于一个 Web 应用中的所有 Servlet 共享同一个 ServletContext 对象,所以多个 Servlet 通过 ServletContext 对象实现数据共享。

    ServletContext 对象通常也被称之为 context 域对象。(还有 request session page)

    String data = "aaaa123";
    this.getServletContext().setAttribute("data", data);
    String value = (String) this.getServletContext().getAttribute("data");
    response.getOutputStream().write(("data:" + value).getBytes());

    这两个放在两个 ServletDemo 类中时,在不同浏览器的不同页面都可以访问到 ServletContext 的值。

    // 读取 WebRootWEB-INFweb.xml 中的属性值
    String value = this.getServletContext().getInitParameter("data1");
    response.getOutputStream().write(value.getBytes());
    <servlet>
        <servlet-name>ServletDemo5</servlet-name>
        <servlet-class>cn.itcast.ServletDemo5</servlet-class>
        <init-param>
            <param-name>data1</param-name>
            <param-value>x1x2x3</param-value>
        </init-param>
    </servlet>

    以下代码实现了 Servlet 的转发: ( 1.jsp 位于 WebRoot1.jsp ),但因为 ServletContext 是所有用户共用的域,因此此处使用并不合适。

    String data = "aaa5";
    // 把数据带给 1.jsp 不能通过 ServletContext 域,而要通过 request 域。
    this.getServletContext().setAttribute("data", data);
    
    RequestDispatcher rDispatcher = this.getServletContext()
            .getRequestDispatcher("/1.jsp");
    rDispatcher.forward(request, response);
    <div style="border:1px solid red;">
     <%
         String data = (String) application.getAttribute("data");
         out.write(data);
      %>
      </div>

    以下代码用于读取 src 目录下的 db.properties 配置文件中的键值

    // 资源文件在源代码中放在 src 目录下,但发布后会出现在 WEB-INFclasses 目录下。
    InputStream inputStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
    Properties properties = new Properties();
    properties.load(inputStream);
    
    String urlString = properties.getProperty("url");
    String username = properties.getProperty("username");
    String password = properties.getProperty("password");
    
    response.getOutputStream().write(("url:" + urlString).getBytes());
    response.getOutputStream().write(("
    username:" + username).getBytes());
    response.getOutputStream().write(("
    password" + password).getBytes());

    如果配置文件放在包下,路径会在 /WEB-INF/classes/cn/itcast/ 目录下

    如果配置文件在 WebRoot 目录下,路径会成为  /db.properties 

    如果要使用 FileInputStream 来读取资源文件,可使用以下代码取得文件真实路径:

    String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");

    如果是 java 类等,不是 Servlet 的类来读资源文件,则使用以下代码

    InputStream inputStream = UserDao.class.getClassLoader().getResourceAsStream("db.properties");
    Properties properties = new Properties();
    properties.load(inputStream);

    然后因为只需应用启动时读取一次,所以通常写成以下代码来使用:

    private static Properties dbconfig = new Properties();
    
    static {
        try {
            InputStream inputStream = UserDao.class.getClassLoader()
                    .getResourceAsStream("db.properties");
            dbconfig.load(inputStream);
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    
    // 如果读取资源文件的程序不是 Servlet 的话,就只能通过类装载器来读取
    public void update() throws IOException {
        System.out.println(dbconfig.getProperty("url"));
    }

     这样的代码发布后,修改配置文件需要重启应用,可以使用以下方法改进:

    InputStream inputStream = UserDao.class.getClassLoader().getResourceAsStream("db.properties");
    // 按上面代码加载的配置文件 修改后仍读到原来的值
    String path = UserDao.class.getClassLoader().getResource("db.properties").getPath();
    FileInputStream inputStream = new FileInputStream(path);

    不使用第一句代码,而使用后面的代码即可。

  • 相关阅读:
    Python+fiddler(基于Cookie绕过验证码自动登录)
    Python+selenium(警告框处理)
    Python+selenium(多表单、多窗口切换)
    Python+selenium(定位一组元素)
    Python+selenium登录测试
    【转载】python format遇上花括号{}
    【转载】判断当前使用的编译器及操作系统
    动态库的创建,隐式加载和显式加载
    Google C++单元测试框架GoogleTest---AdvancedGuide(译文)
    三次样条插值 cubic spline interpolation
  • 原文地址:https://www.cnblogs.com/z5337/p/6838954.html
Copyright © 2011-2022 走看看