使用ServletContext对象读取资源文件

Posted 夏日浅笑丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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);
  }

}

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

 






























































以上是关于使用ServletContext对象读取资源文件的主要内容,如果未能解决你的问题,请参考以下文章

servlet开发之ServletContext

J2EE之ServletContext读取资源文件

小谈——读取web资源文件的方式和路径问题

ServletContext对象的使用

在Action 中访问web资源

ServletContext对象