码迷,mamicode.com
首页 > 其他好文 > 详细

使用ServletContext对象读取资源文件

时间:2017-05-07 10:06:46      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:print   request   http   except   this   imp   使用   细节   bsp   

备注:本文以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对象读取资源文件

标签:print   request   http   except   this   imp   使用   细节   bsp   

原文地址:http://www.cnblogs.com/summertime-wu/p/6819423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!