How to manage path address of our file in J2EE?
Hi there,
I just wondering what is the best way to link a file (JSP, servlet service, dll) in J2EE?
For example I have
www.myserver.com/web/home.jsp (Home JSP file)
www.myserver.com/web/service/home (for example Home servlet)
and if the Home JSP post data to servlet it shoud link to "./service/home"
and servlet will pass back the data to the JSP through dispatching not redirecting... so that the link in the web browser will like "www.myserver.com/web/service/home"
the problem is... it will be not working if I post data to servlet again with same link ".service/home" because it will go to "www.myserver.com/web/service/service/home"
so anyone can help me? is there any solution to manage linking a page or service in J2EE? Thanks!
[792 byte] By [
-wizz-a] at [2007-11-26 15:41:54]

# 2
Hi evnafets,
Big me pardon, can you explain more clearly to me a bit? Because I looked at Google about <base> tag explanation, it does almost like I want... But still I have to manage every page to define the base tag and "assume" the location of page... So is there any elegant way to solve this problem? Thanks for your reply!
# 3
The standard approache is to assume the "base" url for a page will be the same as the actualy URL of the jsp page being invoked - ie what the address would be if you navigated directly to the page, as opposed to getting forwarded to it.
Yes you can hardcode it on every jsp page, but it will need to be edited if you shift the page around.
The generic way is to have an expression derive what this value is from the request.
Something like the following expression in a jsp:
<base href="<%= request.getScheme() +"://" + request.getServerName() + (request.getServerPort() != 80 ? ":" + request.getServerPort() : "") + request.getRequestURI() %> "/>
Thats a little messy to put on a JSP. A more elegant way to solve it is with the use of a custom tag.
The Struts html:base tag does exactly this.
<html:base/>
Post back if you need more help on this.
Cheers,
evnafets
# 4
I see... So if we want elegant way (hide coding scriptlet from presentation) by using custom tag? And there is someone who did this which Struts <html:base/> ? WOW cool...
But I searched to google about that... and I saw there is getServer and setServer ... Can you explain how to use this one? but i think i will prefer to stick with servlet one... LOL! Thanks man! you really help me!