zoukankan      html  css  js  c++  java
  • 使用ServletContext对象读取资源文件

    备注:本文以properties文件为例

    一、通过ServletContext读取文件

    1.通过ServletContext读取放置在src下的properties文件

    package com;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet("/Test_ServletContext")
    public class Test_ServletContext extends HttpServlet {

      protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

        InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/test.properties"); //项目的目录结构和部署在服务器上的目录结构并不一    致,src对应的是/WEB-INF/classes


        Properties props = new Properties();
        props.load(in);

        String key= props.getProperty("key"); //获取key所对应的字符串
        System.out.println(key);

      }


      protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        doGet(request, response);
      }

    }

    2.通过ServletContext读取放置在WebRoot下的properties文件

    package com;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet("/Test_ServletContext")
    public class Test_ServletContext extends HttpServlet {

      protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {

        InputStream in = this.getServletContext().getResourceAsStream("/test.properties"); 

        Properties props = new Properties();
        props.load(in);

        String key= props.getProperty("key"); //获取key所对应的字符串
        System.out.println(key);

      }


      protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        doGet(request, response);
      }

    }

    备注:其实1和2的方式一样,但是因为项目的目录结构和部署在服务器上的目录结构不一致这个细节,所以就还是写出来了

    二、用FileInputStream读取资源文件

    package com;

    import java.io.FileInputStream;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet("/Test_ServletContext")
    public class Test_ServletContext extends HttpServlet {

      protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        

        String path = this.getServletContext().getRealPath("/WEB-INF/classes/test.properties");//通过ServletContext对象获取该资源的绝对路径
        FileInputStream in = new FileInputStream(path);

        Properties props = new Properties();
        props.load(in);

        String key= props.getProperty("key"); //获取key所对应的字符串
        System.out.println(key);

      }


      protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        doGet(request, response);
      }

    }

    备注:这种方式虽然麻烦一点,但是可以得到当前读取资源文件的名称

     

  • 相关阅读:
    【MongoDB】windows下搭建Mongo主(Master)/从(slave)数据库同步
    视图与暂时表
    Highcharts构建分组分类坐标轴
    iOS可持续化集成: Jenkins + bundler + cocoapods + shenzhen + fastlane + pgyer
    Internationalization composition diagram
    ASCII表 基本记忆 -- C
    Java内部类
    Idea过期继续激活~
    Spark SQL 用户自定义函数UDF、用户自定义聚合函数UDAF 教程(Java踩坑教学版)
    Spark源码分析之Spark-submit和Spark-class
  • 原文地址:https://www.cnblogs.com/summertime-wu/p/6819423.html
Copyright © 2011-2022 走看看