Pink
Pink

Reputation: 451

JSP hypertext link to index page

can You explain me how to make hypertext links between .jsp files? I found two possible options, the firs is: <a href="index.jsp">index</a> but there is a problem that my url now contains ".jsp" string at the end (and we dont want that). Second possibility is this:

<a href="<%=request.getContextPath() %>">index</a>

this is working great, but question is this is a good practise because there can be a lot of links in my web application and to have scriptlet in every anchor is not very nice to see. How do you solve links between jsp files? thanks

EDIT: http://tinypic.com/view.php?pic=6ynkpl&s=8#.VC8NjBaimS4 -here is my problem, as you can see in the picture, index.jsp and register.jsp are not in the same folder level. (other jsp files are just parts of both index and register), and I have many links just in navigation.jsp, so from there, I cannot determine to what level should I call my .jsp file in tag. Clear now?

example: clicking link on navigation from index requires <a href="index.jsp">index</a> while clicking that link when I'm in register jsp requires <a href="../../index.jsp">index</a> //if I'm not wrong, I didn't try it, it's just for imagination.

Upvotes: 0

Views: 4045

Answers (2)

hooknc
hooknc

Reputation: 4981

I would recommend using what is called the core jstl libraries to do what you're looking for.

Especially look at the url tag.

Example:

<a href="<c:url value="/" />">Index</a>
<a href="<c:url value="/user/register.html" />">Register</a>

Upvotes: 1

developerwjk
developerwjk

Reputation: 8659

Same level:

<a href="./index.jsp">index</a>

One level back:

<a href="../index.jsp">index</a>

Two levels back:

<a href="../../index.jsp">index</a>

If you want to get rid of the .jsp you can either map your jsp to a different URL in web.xml (kind of a pain), or install Tuckey URL Rewrite filter (more versatile, since it uses regex). I'd suggest the Tuckey option since it will let you turn URL path info into parameters.

Like for instance http://localhost/app/index/1/2/3/ could be mapped to run on the back-end as http://localhost/app/index.jsp?var1=1&var2=2&var3=3

Upvotes: 0

Related Questions