File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes JSP/HTML files in WEB-INF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
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: 3830
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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: 3830
Thanks a lot Vishnu.

It was great explanation and that too with code.
sunaina agarwal
Ranch Hand

Joined: Oct 10, 2013
Posts: 49


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.


Hello Vishnu i have another doubt regarding this if my jsp is in Web-inf/webPages and my servlet is in JavaResources/src/com package. Then how to get to that servlet
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

The path to the servlet has nothing to do with any JSP. The path to a servlet is determined by how it is mapped in the deployment descriptor.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Consider Paul's rocket mass heater.
 
subject: JSP/HTML files in WEB-INF
 
Similar Threads
creating website basic question
Unable to locate the .jsp file error
Make Tomcat go
Where in Tomcat3.1 should i put .jsp files, .java files(Beans files)
WEB_INF