• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JSP/HTML files in WEB-INF  RSS feed

 
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.
 
Java Cowboy
Sheriff
Posts: 16084
88
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.
 
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.
 
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.
 
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
 
Author and ninkuma
Marshal
Posts: 66806
168
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!