Why put . jsp file in WEB-INF directory?

Asked

Viewed 1,128 times

6

Taking Maven as an example, when starting a new web project, the structure is equal/similar to this:

Meu Projeto
    |- src
        |- main
            |- java
            |- resources
            |- webapp
                 |- WEB-INF

Where, in the directory "webapp" are placed the files used by the web application: html, css, Javascript, Jsps, etc.

My question is regarding the WEB-INF directory, there are several projects that use this directory to put the files with extension . jsp, instead of putting them directly into "webapp". An example is Mammoth from Caelum, where they created a directory called "jsps" for this.

From what I read about it, the files contained in WEB-INF cannot be accessed directly by the client because they are not public, but can be accessed by Servlets:

A special directory exists Within the application Hierarchy named WEB-INF. This directory contains all Things Related to the application that aren’t in the Document root of the application. The WEB-INF Node is not part of the public Document Tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the Contents of the WEB-INF directory are Visible to Servlet code using the getResource and getResourceAsStream method calls on the ServletContext, and may be Exposed using the RequestDispatcher calls.

So it would only be for security? The only way to get the files would be to be processed through a Servlet.

1 answer

2

9.10 Hiding our pages - https://www.caelum.com.br/apostila-java-web/mvc-model-view-controller/

If the pages are in /webapp, the user will have direct access to them. If they are only static pages, I see no problem. But some pages need a logic before they are displayed, in case the user accesses jsp directly the page will not display correctly, it will not go through the controller.

"Therefore, we should not allow the user to directly access our page. To prevent this direct access, we will place our pages inside the WEB-INF directory/"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.