JavaWeb学习-Servlet系列-9-ServletContext获取资源绝对路径

继续来学习ServletContext对象的第三个功能,就是获取当前应用项目下的任何资源文件的绝对路径,这里我们模拟在不同路径下写三个properties文件,分别是abc三个文件,里面key=aaa或者key=bbb或者key=ccc, 我们在servlet中通过这三个文件相对路径或者文件名称,去获取绝对路径,然后解析propertest文件,取出里面key的value。

1.ServletDemo3.java内容

package com.anthony.servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

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


public class ServletDemo3 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		test1();
		test2();
		test3();
	}
	
	// 获取/WEB-INF/a.properties文件路径和key的value
	private void test1() throws IOException, FileNotFoundException {
		String path = this.getServletContext().getRealPath("/WEB-INF/a.properties");
		System.out.println(path);
		Properties pro = new Properties();
		pro.load(new FileInputStream(path));
		System.out.println(pro.getProperty("key"));
	}
	
	// 获取/WEB-INF/a.properties文件路径和key的value
	private void test2() throws IOException, FileNotFoundException {
		String path = this.getServletContext().getRealPath("/WEB-INF/classes/b.properties");
		System.out.println(path);
		Properties pro = new Properties();
		pro.load(new FileInputStream(path));
		System.out.println(pro.getProperty("key"));
	}
	
	// 获取/WEB-INF/a.properties文件路径和key的value
	private void test3() throws IOException, FileNotFoundException {
		String path = this.getServletContext().getRealPath("/WEB-INF/classes/com/anthony/servlet/c.properties");
		System.out.println(path);
		Properties pro = new Properties();
		pro.load(new FileInputStream(path));
		System.out.println(pro.getProperty("key"));
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

2.web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  
  <!-- 添加全局配置信息, 在servlet标签里面是每个servlet对象自己的局部变量信息 -->
  <context-param>
  	<param-name>encoding</param-name>
  	<param-value>GBK</param-value>
  </context-param>
  
  <servlet>
    <servlet-name>servletDemo3</servlet-name>
    <servlet-class>com.anthony.servlet.ServletDemo3</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>servletDemo3</servlet-name>
    <url-pattern>/demo3</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.部署测试效果

D:\apache-tomcat-8.5.40\webapps\Servlet01\WEB-INF\a.properties
aaaaaaaaaaaaa
D:\apache-tomcat-8.5.40\webapps\Servlet01\WEB-INF\classes\b.properties
bbbbbbbbbbb
D:\apache-tomcat-8.5.40\webapps\Servlet01\WEB-INF\classes\com\anthony\servlet\c.properties
cccccccccccc

看输出结果,正确打印了三个文件的绝对路径,并解析出里面key的value。这里绝对路径是tomcat服务器下路径,并不是Eclipse中项目下路径。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值