File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Al Koch
Ranch Hand
Posts: 48
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Piyush Mangal
Ranch Hand
Posts: 196
  • 0
  • Mark post as helpful
  • send pies
  • 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: 48
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When forwarding, you do not specify the context path.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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: 48
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you who took the time to reply! I think I've got it now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic