wood burning stoves 2.0*
The moose likes JSP and the fly likes deployed application context not working with absolute url in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "deployed application context not working with absolute url in jsp" Watch "deployed application context not working with absolute url in jsp" New topic
Author

deployed application context not working with absolute url in jsp

Ahsan Javed
Greenhorn

Joined: Aug 29, 2010
Posts: 2
Hi,
Please bear with me if it has been asked earlier. I have an application named as app and deployed at tomcat 5.5.30.
In this app I have a header.jsp file which contain the absolute url link as follow

header.jsp


Now when I try to click this url from browser, I expected it to go to "http://localhost:8080/app/secure/publisher/create.htm".
But it is going to "http://localhost:8080/secure/publisher/create.htm". This jsp file is deployed under a webapp called as app(the name of the application can change however) so I thought that all absolute urls should start with the name of application context "app" but it is taking the tomcat default context "/".
I can't modify the tomcat configuration file because they are not accessible to me so I can't set the root context etc. Also my webapp can be deployed with different names depending upon the production environment or testing environment so i do not want to hard code anything in the url links like
"<a href="{application-name-here}/secure/publisher/create.htm">Add new publisher</a>"

Please help me out.
PS: Earlier in my php/html days, I saw that absolute urls worked starting from application context rather than apache local root folder so I thought it would be same in tomcat as well.

Thanks in Advance.
Ahsan
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179


You can add <%= request.getContextPath(); %> to your url...
Ahsan Javed
Greenhorn

Joined: Aug 29, 2010
Posts: 2
Thanks,
It did the magic for me. However please comment if my understanding is correct about context path.

Ahsan
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10068
    
163

I'll move this out of Tomcat forum, since this isn't specific to Tomcat. Ranchers in the JSP forum will be able to help you more.

[My Blog] [JavaRanch Journal]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Without the context path, how would the container know which application should serve the request?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Ahsan Javed wrote:Earlier in my php/html days, I saw that absolute urls worked starting from application context rather than apache local root folder so I thought it would be same in tomcat as well.


PHP and HTML have no concept of "application context" (unless maybe you are using some PHP framework or something). An absolute URL works the same in PHP and HTML as it does in a JSP.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

If you are using JSTL in your web app, you can use the c:url tag to do just what you're looking for:

The behavior of c:url, when the "value" starts with "/" is to map it to the root of the web app (context). So if you looked at the translated code in your browser, you should see a link similar to "/YourAppNameHere/secure/publisher/create.htm".


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: deployed application context not working with absolute url in jsp