使用ServletContext对象读取资源文件

时间:2021-10-30 13:47:26

备注:本文以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);
  }

}

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