Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

deployed application context not working with absolute url in jsp

 
Ahsan Javed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can add <%= request.getContextPath(); %> to your url...
 
Ahsan Javed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
It did the magic for me. However please comment if my understanding is correct about context path.

Ahsan
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without the context path, how would the container know which application should serve the request?
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic