ServletConfig+ServletContext获取资源路径

文章目录

  • ServletConfig
    • 获取初始化信息的方法
  • ServletContext
  • 主要方法
  • 实现数据共享
  • getRealPath获取web资源
    • 获取web应用目录下资源
    • 获取WEB-INF目录下资源
    • 获取src目录下资源
  • 生命周期

ServletConfig

代表当前Servlet初始化信息的对象

获取初始化信息的方法

@WebServlet(
        value = "/ServletConfigDemo2",
        initParams = {
                @WebInitParam(name = "username",value = "summerday"), @WebInitParam(name = "age",value = "18"),
                @WebInitParam(name = "name",value = "summerday")
        },
        loadOnStartup = 1,
        description = "使用WebServlet注解"
)
public class ServletConfigDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletConfig对象
        ServletConfig config = this.getServletConfig();
        //获取所有初始化参数名
        Enumeration<String> names = config.getInitParameterNames();
        while(names.hasMoreElements()){
            String s = names.nextElement();
            //根据参数名获取参数值
            String param = config.getInitParameter(s);
            System.out.println(s+">>>"+param);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

ServletContext

代表当前Web应用的对象,作为域对象,实现当前web应用范围内资源共享。

一想到域对象,就会想到对象map,进而想到Attribute相关的方法。

主要方法

  • void setAttribute(String var1, Object var2);
  • Object getAttribute(String var1);
  • void removeAttribute(String var1);
  • Enumeration getAttributeNames();

实现数据共享

//ServletContextDemo1    
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext sc = this.getServletContext();
    	//向域中设置msg = "hello"
        sc.setAttribute("msg","hello");
    }
//ServletContextDemo2
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //获取域中名叫msg的值
        Object msg = servletContext.getAttribute("msg");
        System.out.println(msg);
    }

getRealPath获取web资源

//直接写上相对路径,将会去加载的服务器启动目录下寻找指定文件。 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "file.txt";
        File file = new File(path);
        System.out.println(file.getAbsolutePath());
        //D:\apache-tomcat-8.5.31\bin\file.txt
    }
//如果写上绝对路径,将回去项目启动的磁盘根目录下寻找指定文件。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "/file.txt";
        File file = new File(path);
        System.out.println(file.getAbsolutePath());
        //D:\file.txt
    }

以上两种方式,有时候很难完成我们的需求,我们可以利用ServletContext对象的getRealPath(path)方法,获取文件的服务器路径。

获取web应用目录下资源

//将会去当前web应用目录下寻找    
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext c = this.getServletContext();
        String path = "/file.txt";
        String realPath = c.getRealPath(path);
        File file = new File(realPath);
        System.out.println(file.getAbsolutePath());
        //E:\java_learning\WebDemo\out\artifacts\servletDemo_war_exploded\file.txt
    }

ServletConfig+ServletContext获取资源路径_第1张图片

通过以上可以发现,如果String path = "/WEB-INF/file.txt";,将会去WEB-INF目录下寻找资源。IDEA默认设置下,是没有WEB-INF的(其实是有的),可以自己创建一个,里面塞一个file.txt。

获取WEB-INF目录下资源

//将会去当前web应用目录下的WEB-INF目录下寻找    
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext c = this.getServletContext();
        String realPath = c.getRealPath("/WEB-INF/file.txt");
        File file = new File(realPath);
        System.out.println(file.getAbsolutePath());
        //E:\java_learning\WebDemo\out\artifacts\servletDemo_war_exploded\WEB-INF\file.txt
    }

获取src目录下资源

如果file.txt文件在src目录下,IDEA里默认是看不到classes的,但其实src目录下的文件在web应用发布后,都被扔到了WEB-INF/classes中,这时候可以String path = "/WEB-INF/classes/file.txt";

ServletConfig+ServletContext获取资源路径_第2张图片
ServletConfig+ServletContext获取资源路径_第3张图片

//将会去当前web应用目录下的WEB-INF/classes目录下寻找(src目录发布之后会被扔到classes中)  
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext c = this.getServletContext();
        String realPath = c.getRealPath("/WEB-INF/classes/file.txt");
        File file = new File(realPath);
        System.out.println(file.getAbsolutePath());
                //E:\java_learning\WebDemo\out\artifacts\servletDemo_war_exploded\WEB-INF\classes\file.txt
    }

//通过类加载器,也可以找到src目录下的资源
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = ServletContextDemo3.class.getClassLoader().getResource("file.txt").getPath();
        File file = new File(path);
        System.out.println(file.getAbsolutePath());
    }

生命周期

Web应用加载时时创建,web应用销毁时销毁。

作用在当前web应用范围内。

你可能感兴趣的:(JavaWeb)