Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How specify path to .JSP file for request.getRequestDispatcher()?

 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am confused about the meaning of request.getContextPath(). My file layout is as follows:
MyServer/WebContent:
/Resources/MyImage.jpg
/Resources/Scripts/MyScript.js
/WEB-INF/JSP/MyPage.jsp

In the MyPage.jsp I am able to locate the JavaScript and image by using ${pageContext.request.contextPath}/Resources/Scripts/MyScript.js and ${pageContext.request.contextPath}/Resources/MyImage.img. From this I concluded that pageContext.request.contextPath dynamically resolved to the "WebContent" folder and it is my understanding that this will resolve to this folder no matter what it is named. That is working.

However, from all of this I concluded that back in my .java code "request.getContextPath()" would also dynamically resolve to "WebContent". But when I try to forward from the .java code to MyPage.jsp using the string formed from request.getContextPath()+"/WEB-INF/JSP/MyPage.jsp", the JSP cannot be found; this results in a 404 Error - "The Requested Resource (/MyServer/WEB-INF/JSP/MyPage.jsp) is not available". If I call request.getContextPath() with "WEB-INF/JSP/MyPage.jsp" we launch the JSP page. Can someone explain why pre-pending request.getContextPath() causes this to fail and is there something else I should use to ensure that the path to the .JSP is always resolved?

Thank you.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Al Koch wrote: From this I concluded that pageContext.request.contextPath dynamically resolved to the "WebContent" folder and it is my understanding that this will resolve to this folder no matter what it is named.



This is incorrect as WebContent does not end up being part of your war file. IDEs like Eclipse use these folder for web project structure. All the resources under this folder ends up under the root directory of your war file.

However, from all of this I concluded that back in my .java code "request.getContextPath()" would also dynamically resolve to "WebContent". But when I try to forward from the .java code to MyPage.jsp using the string formed from request.getContextPath()+"/WEB-INF/JSP/MyPage.jsp", the JSP cannot be found; this results in a 404 Error - "The Requested Resource (/MyServer/WEB-INF/JSP/MyPage.jsp) is not available". If I call request.getContextPath() with "WEB-INF/JSP/MyPage.jsp" we launch the JSP page. Can someone explain why pre-pending request.getContextPath() causes this to fail and is there something else I should use to ensure that the path to the .JSP is always resolved?



All the resources under WEB-INF are secure and can not be directly accessed by apending context path. Servlets can access resources under WEB-INF relative to WEB-INF only. As Servlets are agnostic of what context path is being used when an application is deployed.
 
Al Koch
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piyush,

Thanks for the reply.

Yes, I understand that the name "WebContent" is generated by Eclipse but I was trying to describe that folder, whatever it's name. But more to my question, if I should not be referencing a context path, what should I use? Am I guaranteed that "/WEB-INF/JSP/MyPage.jsp" will always be resolved no matter how the WAR is deployed? If not, what should I use to ensure portability?

Thanks.
 
Sheriff
Posts: 67618
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
When forwarding, you do not specify the context path.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all transfer all WebContent files to proper "war" file. Dont use "WebContent" as root which ServletContainer will regard as ContextPath...

When you requestDispatch to the path "/WEB-INF/JSP/MyPage.jsp", path will be resolved implicitly under the context path so that dont need to
specify it explicitly. Since RequestDispatching occur in Server side, WEB-INF resources are accessible...

When you do sendRedirect which sends Redirection response status to Client(Browser) to raise new request for the page, it wont work since
the explicit request from client for WEB-INF resources are not allowed.
 
Bear Bibeault
Sheriff
Posts: 67618
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

Ram Narayan.M wrote:First of all transfer all WebContent files to proper "war" file. Dont use "WebContent" as root which ServletContainer will regard as ContextPath...



There is no need to change the project structure. As long as the folder that contains WEB-INF is identified as the context root, it can be named anything.

And yes, as I already said, when forwarding, the context path is not specified.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was meaning only the context root name "WebContent" that it can be changed since it may look odd... Thats all
 
Bear Bibeault
Sheriff
Posts: 67618
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

Ram Narayan.M wrote:I was meaning only the context root name "WebContent" that it can be changed since it may look odd... Thats all



Doesn't seem odd to me. Personally, I wouldn't name it that as I never used mixed case for folder names, but other than that what's odd about it?
 
Al Koch
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you who took the time to reply! I think I've got it now.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic