• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non-absolute anchor href to web app's root  RSS feed

 
Jim Lum
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a simple web app running under Tomcat. I want to add a link to my pages, to take the user back to the root of the web app, but, if possible, I don't want to have to code an anchor ("<A...>") with an href with the web app's URI.

For example, the user gets to the web app initially using:

http://<host>/mywebapp

I tried using "Home", but that is taking me back to Tomcat's root (http://<host>/) instead of (http://<host>/mywebapp).

Is there a way to do this with Tomcat?

Thanks,
Jim
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Lum wrote:I don't want to have to code an anchor ("<A...>") with an href with the web app's URI.

Why not? It's incredibly simple:
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I prefer to use the core taglib to this as I think it is a bit simpler more dynamic:



slightly more code but I think it is easier to read if you have parameters?

Just thought I would throw it in.

Sean
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parameters are a different matter because you have to get into formatting the query string. For that <c:url> is a must. But for just a base reference I fail to see how all those extra and unnecessary lines of code is "simpler" than a single EL expression.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I agree with you and I have edited my post as that parameter is not simpler.

I used it for my application as my "home" location was not the same as the root of the app so I needed an absolute path to the "home" and that worked.

Sean
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, for the more general case, the use of <c:url> cannot be stressed enough!

How many badly-written JSPs have you seen where people try to form URLs "by hand"? (usually getting it wrong)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, as this is nothing that is Tomcat-specific, I've moved it to the JSP forum.
 
Jim Lum
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Jim Lum wrote:I don't want to have to code an anchor ("<A...>") with an href with the web app's URI.

Why not? It's incredibly simple:



Hi,

I tried your suggestion:



but I got:

The requested resource (/xxxxxx/${pageContext.request.contextPath}) is not available

Jim
 
Jim Lum
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry, I just figured out something simpler, that seems to work fine. Basically, I use "./" for the href string.

I forgot to mention that this is a pretty simple app, with all the JSPs in the base/root directory of my app, so that's probably why the above just works?

Jim
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not recommend page-relative addressing in web apps. It's too fragile and easily broken.

You aren't using the EL already in your JSPs? Why not?

Please don't tell me that you are using Java scriptlets in new code!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!