Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP/HTML files in WEB-INF

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Vishnu.

It was great explanation and that too with code.
 
sunaina agarwal
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 65114
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic