• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP/HTML files in WEB-INF

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Vishnu.

It was great explanation and that too with code.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic