File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How specify path to .JSP file for request.getRequestDispatcher()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How specify path to .JSP file for request.getRequestDispatcher()?" Watch "How specify path to .JSP file for request.getRequestDispatcher()?" New topic
Author

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

Al Koch
Greenhorn

Joined: Aug 24, 2011
Posts: 23

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

Joined: Jan 22, 2007
Posts: 196
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
Greenhorn

Joined: Aug 24, 2011
Posts: 23

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

Joined: Jan 10, 2002
Posts: 61101
    
  66

When forwarding, you do not specify the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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.


SCJP 6 [SCJP - Old is Gold]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61101
    
  66

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

Joined: Jul 11, 2010
Posts: 247

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

Joined: Jan 10, 2002
Posts: 61101
    
  66

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
Greenhorn

Joined: Aug 24, 2011
Posts: 23

Thanks to all of you who took the time to reply! I think I've got it now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How specify path to .JSP file for request.getRequestDispatcher()?