You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by ledbettd <le...@yahoo.com> on 2009/06/09 15:49:38 UTC

Tomcat hyperlink interpretation when including jsp file from another subdirectory

Can anyone help explain what is going on with the Tomcat link
creation/interpretation and what we could do to get this working correctly?

Apache Tomcat Version 5.5.26
JRE_HOME:  /usr/java/jdk1.6.0_07
CATALINA_BASE: /usr/local/tomcat5
Running on Virtual Server VMWare 
within a 64bit Windows 2003 Server.

Tomcat Virtual Hosts Current Setup:

/usr/local/tomcat5/conf/server.xml
    <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost" appBase="webapps"
       unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
      </Host>
      <Host appBase="bcosite" name="bcosite.com"
	unpackWARs="true" autoDeploy="true"
	xmlValidation="false" xmlNamespaceAware="false" > 
	<Valve className="org.apache.catalina.valves.AccessLogValve"
        directory="logs/be"  prefix="be_access_log." suffix=".txt"
        pattern="combined" resolveHosts="true"/> 
      </Host>
    </Engine>

/usr/local/tomcat5/bcosite/ROOT/META-INF/context.xml
<Context privileged="true" antiResourceLocking="false" 
	reloadable="true" antiJARLocking="false" debug="1"/>

Directory Structure contents (Only 1 Virtual Host installed so far):
	/usr/local/tomcat5/webapps/ROOT/ - default Tomcat installation files
	/usr/local/tomcat5/bcosite/ROOT/ - default.jsp & prds.jsp
	/usr/local/tomcat5/bcosite/ROOT/includes/ - header.jsp & footer.html
	/usr/local/tomcat5/bcosite/ROOT/pudire/ - pudire.jsp

Inside both the header.jsp and footer.html files the same link is included:
	 /prds.jsp prds 

Both the default.jsp and pudire.jsp files include the footer.html and create
the correct link inside the web page:
	default.jsp = <%@include file="/includes/footer.html"%> = works fine
	pudire.sjp = <%@include file="/includes/footer.html"%> = works fine

This include statement in default.jsp works fine and creates a valid path to
the prds.jsp page:
	<%@include file="/includes/header.jsp"%>
	The link it creates inside the web page is:
	http://bcosite.com/prds.jsp
	Correct link.

This include statement in pudire.jsp creates the wrong path to the prds.jsp
page:
	<%@include file="/includes/header.jsp"%>
	The link it creates for the web page is:
	http://bcosite.com/pudire/prds.jsp
	Wrong link...it added the /pudire directory into the link href??


-- 
View this message in context: http://www.nabble.com/Tomcat-hyperlink-interpretation-when-including-jsp-file-from-another-subdirectory-tp23943241p23943241.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat hyperlink interpretation when including jsp file from another subdirectory

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

To whom it may concern,

On 6/9/2009 5:10 PM, ledbettd wrote:
> Thanks for the help, Chris. The code in footer and header were just a list
> of HTML <A links, not real JSP. I put the above code in for the first
> hyperlink in the header file and it worked from all directories. 

Excellent.

> So, we were hoping to migrate our code over to the new virtual setup without
> much editing. It appears that the context path will play more of a major
> role than it did on the default Tomcat installation where this was fine.

Using the context path in all your links will make re-location *much*
easier: just re-deploy to a different context path (say /foo/bar or even
ROOT) and everything works without any changes. It's definitely worth
migrating as you touch files. Even better: use a tag library like JSTL
(or, if you use Struts, their <s:url> tag) that does everything for you.
That way, your JSP code stays readable yet flexible.

> Actually, a weird think happened on the way to the ...
> I noticed that even though I only changed the first <A link to the JSP
> format, the other links across the header suddenly were correct, even though
> they were still in the HTML <A format. This was not an expected outcome and
> begs the question: Is there an easier way (read: work around with
> configuration files) to have the header HTML <A links inside the JSP file be
> interpreted within the correct context from where ever inside this
> bcosite.com virtual host, without rewriting all the old code to proper JSP? 

The only thing I can think of is to use a <base> tag in your HTML
documents. This is an ugly hack if you ask me, and it might not solve
what I believe your original problem to be: that you were not using
fully-qualified URLs in the first place.

Good luck,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkowFvgACgkQ9CaO5/Lv0PBO0ACgpSuFUBYfToCY+ANYm5PbN/ak
PDIAoKD0gA3zc7YkqGyL3uqLDGn7nfvF
=Coop
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat hyperlink interpretation when including jsp file from another subdirectory

Posted by ledbettd <le...@yahoo.com>.

Christopher Schultz-2 wrote:
> 
> How about the code from footer.jsp (or header.jsp) that actually
> generates the links?
>  <%= response.encodeURL(request.getContextPath()
         +  ">link text 
> 

Thanks for the help, Chris. The code in footer and header were just a list
of HTML <A links, not real JSP. I put the above code in for the first
hyperlink in the header file and it worked from all directories. 

So, we were hoping to migrate our code over to the new virtual setup without
much editing. It appears that the context path will play more of a major
role than it did on the default Tomcat installation where this was fine.

Actually, a weird think happened on the way to the ...
I noticed that even though I only changed the first <A link to the JSP
format, the other links across the header suddenly were correct, even though
they were still in the HTML <A format. This was not an expected outcome and
begs the question: Is there an easier way (read: work around with
configuration files) to have the header HTML <A links inside the JSP file be
interpreted within the correct context from where ever inside this
bcosite.com virtual host, without rewriting all the old code to proper JSP? 

Again, thank you for the encoding of the URL points. Looking for problems
with the virtual hosts configuration and missed the trees. 

-- 
View this message in context: http://www.nabble.com/hyperlink-has-added--dir-on-virtual-host-tp23943241p23951383.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat hyperlink interpretation when including jsp file from another subdirectory

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

To whom it may concern,

On 6/9/2009 9:49 AM, ledbettd wrote:
> Inside both the header.jsp and footer.html files the same link is included:
> 	 /prds.jsp prds 
> 
> Both the default.jsp and pudire.jsp files include the footer.html and create
> the correct link inside the web page:
> 	default.jsp = <%@include file="/includes/footer.html"%> = works fine
> 	pudire.sjp = <%@include file="/includes/footer.html"%> = works fine

How about the code from footer.jsp (or header.jsp) that actually
generates the links?

> This include statement in default.jsp works fine and creates a valid path to
> the prds.jsp page:
> 	<%@include file="/includes/header.jsp"%>
> 	The link it creates inside the web page is:
> 	http://bcosite.com/prds.jsp
> 	Correct link.
> 
> This include statement in pudire.jsp creates the wrong path to the prds.jsp
> page:
> 	<%@include file="/includes/header.jsp"%>
> 	The link it creates for the web page is:
> 	http://bcosite.com/pudire/prds.jsp
> 	Wrong link...it added the /pudire directory into the link href??

You are probably not creating the links correctly. One proper way to
create a link in a JSP (there are others, depending on your environment)
is to do this:

<a href="<%= response.encodeURL(request.getContextPath()
         + "/full/path/to/resource.jsp"))%>">link text</a>

Certain url-generating JSP tag libraries already do proper encoding of
the URL (to encode jsessionid if necessary) as well as prefixing the
full path to the link resource with the context path, which is nice.

The three points to take away from this tip are:

1. Always use fully-specified URLs, otherwise including files from other
directories will cause headaches such as those you are experiencing.
2. Always prepend the webapp's context path to your URLs
3. Always run your URLs through response.encodeURL
  (or response.encodeRedirectURL if you're redirecting)

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkouwCIACgkQ9CaO5/Lv0PBpaACgnoRuxzbGX1SML0cm/xxDvqOv
NwcAn1cbJPZs/RBFT3+Yyr8lmJ6XRp5p
=yVhb
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org