How do I access file in WEB-INF in JSP?

后端 未结 2 2110
無奈伤痛
無奈伤痛 2020-12-19 19:59

I am using Tomcat. I would like to put the config file in WEB-INF instead of the default root class path which is WEB-INF/classes. Currently I put

相关标签:
2条回答
  • 2020-12-19 20:29

    You can use getServletConfig() method return an instance of ServletConfig.

    ServletContext sc=getServletConfig().getServletContext();
    

    EDIT:

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
      ServletContext sc=getServletContext();
      ...
    }
    
    0 讨论(0)
  • 2020-12-19 20:34

    The method getRealPath() is not guaranteed to work, e.g. if your webapp is not expanded from a war file there is no 'real path' on the filesystem to a file inside the war file.

    Since you say you are using a ServletContextListener, you can get the ServletContext out of the ServletContextEvent:

    sce.getServletContext().getResourceAsStream("/WEB-INF/config.xml");
    
    0 讨论(0)
提交回复
热议问题