继续来学习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中项目下路径。