Two Laptop Bag
The moose likes Servlets and the fly likes JSP/HTML files in WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "JSP/HTML files in WEB-INF" Watch "JSP/HTML files in WEB-INF" New topic
Author

JSP/HTML files in WEB-INF

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3829
If I don't want to let client access the JSP/HTML files directory, then I can put them in WEB-INF directory. But then how my application will access them?

Will container automatically figure out, where JSP files are???

Please comments.

Thanks.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

No, the container will not automatically find out where they are. The purpose of putting your JSP/HTML or other files in the WEB-INF directory (or a subdirectory of the WEB-INF directory) is to make sure that a client cannot access them directly by typing in an URL in his/her browser.

You can access them only from a servlet or JSP outside the WEB-INF directory, by forwarding from the servlet or JSP to one of the pages inside the WEB-INF directory.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Hemant Agarwal
Ranch Hand

Joined: Nov 21, 2005
Posts: 138
You can also map your jsp files in web.xml

<servlet>
<servlet-name>someName</sevlet-name>
<jsp-file>someFile</jsp-file>
</servlet>

Then in servlet mappings section just map your servlet-name to the url-pattern you want to use for that jsp file. In this way you can access your jsp file directly from WEB-INF folder.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Consider you have a web-application where one jsp(MyJsp.jsp) is at the context root available to everyone and another jsp(SecuredJsp.jsp) inside WEB-INF. With the following code you can communicate between resources



But you cannot use response.sendRedirect(String resource) to communicate between resources.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

You can access them only from a servlet or JSP outside the WEB-INF directory,


Not necessarily.

Consider the same example which I had explained in my previous post. Now lets add one more jsp page lets call it SecuredJspTwo.jsp inside WEB-INF.

After accessing MyJsp.jsp control is dispatched to SecuredJsp.jsp. Now include the following code inside SecuredJsp.jsp



Since request.getRequestDispatcher() can take a relative path now request will be dispatched to SecuredJspTwo.jsp which is also inside WEB-INF.

Consider my next scenario

Create a servlet inside WEB-INF/classes/com/example/TestServlet.class

add the following in the service method



Access this servlet directly from your browser and you will end up in SecuredJspTwo.jsp page.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3829
Thanks a lot Vishnu.

It was great explanation and that too with code.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: JSP/HTML files in WEB-INF
 
Similar Threads
creating website basic question
Unable to locate the .jsp file error
Make Tomcat go
WEB_INF
Where in Tomcat3.1 should i put .jsp files, .java files(Beans files)