使用ServletContext获取资源路径

package cn.ttt.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

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

import org.omg.CORBA.Context;

/**
 * 使用ServletContext获取资源路径
 */
@WebServlet("/DServlet")
public class DServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		 * 它得到的是有盘符的路径:F:/xxx/xxx/xx/
		 * F:\MyEclipseGZML\.metadata\.me_tcat85\webapps\day09_02\index.jsp
		 */
		String path = this.getServletContext().getRealPath("/index.jsp");
		System.out.println(path);
		
		//获取资源路径后,再创建输出流对象!
		InputStream input = this.getServletContext().getResourceAsStream("/index.jsp");
		
		//获取指定目录下所有的路径
		//[/WEB-INF/lib/, /WEB-INF/classes/, /WEB-INF/web.xml]
		Set paths = this.getServletContext().getResourcePaths("/WEB-INF");
		System.out.println(paths);
	}

}

你可能感兴趣的:(使用ServletContext获取资源路径)