Access resources (CSS, HTML, images, JS) through the servlet

I would like to know if anyone has a solution for accessing website resources only through a servlet. I have all my resources under WEB-INF. In other words, I do not want users to have direct access to any of my resources.

+3
source share
2 answers

You can use ServletContext#getResource()for this.

URL resource = getServletContext().getResource("/WEB-INF/file.ext");
File file = new File(resource.getPath());
// ...

You can even use ServletContext#getResourceAsStream()to get InputStream:

InputStream input = getServletContext().getResourceAsStream("/WEB-INF/file.ext");
// ...

As you can see in the examples, it ServletContextis available in inherited servlets GenericServlet#getServletContext().


, , - . . ? URL-. " "? ? , Filter .

JSP . , - ( MVC), JSP RequestDispatcher#forward(), ServletRequest#getRequestDispatcher().

request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
+5

Source: https://habr.com/ru/post/1744651/


All Articles