You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Juergen Weber <we...@yahoo.com> on 2004/02/22 01:20:25 UTC

Relative links do not work with controller servlet.

I want to run all requests through a controller
servlet.

The requests should be like
/controller/dir/fileXXX.html

This is done with a 

<servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>/controller/*</url-pattern>
</servlet-mapping>


request.getPathInfo() is "dir/fileXXX.html"


fileXXX.html in reality are JSPs, which the Controller
servlet forwards to.

In the JSPs are relative links to images and CSS.
Well, as in the browsers view the html files are below
/controller, it requests these relative links also
below /controller, but the controller cannot and
should not handle CSS and images. 

This problem surfaces often in the newsgroups, but I
did not find a solution.

Is there a good, portable solution to the problem ?

Ideally would be a
<url-pattern-exclude>*.css,*.jpg,*.gif</url-pattern-exclude>
Tag in web.xml.

Thanks, Juergen


__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools

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


Re: Relative links do not work with controller servlet.

Posted by Harry Mantheakis <ha...@mantheakis.freeserve.co.uk>.
Hi Jurgen


> I want to run all requests through a controller
> servlet.


That's a good thing to do, and I do it the same way - using extra path
information to determine where to send a request once the controller
receives it. I happen to have some cribs on this subject, so here goes...

When a Servlet forwards a request to a JSP, the JSP document is read 'out of
context' and the relative HTML links in that document cannot be interpreted.

Typically this means that images fail to load, and style sheet definitions
are not enforced. The relative HTML links in the JSP are 'broken'.

I *think* this is your problem.

The solution is to specify a 'base URL' for all the links in a JSP document.

The HTML <BASE> element - which must be placed within the <HEAD> element of
an HTML document - serves this purpose.

Using the <BASE> element will help ensure your image and CSS links are not
broken when a Servlet forwards a request to a JSP. It will also make your
JSPs more portable, by allowing you to specify relative URLs for all your
resource links.

The value of the HREF attribute in the <BASE> element must be an absolute
URI. The following example illustrates the HREF attribute being set with a
hard coded literal value:

    <head>
        <base href="http://localhost:8080/">
    </head>

Note the trailing slash, just after the port number. This is necessary.

The above example assumes you are using the default 'ROOT' context in a
Servlet container running locally. (The port number '8080' is the Tomcat
default. If you are using port '80' you do not need to specify the port
number in the URI.)

If your application has its own context, then you must add the context name
to the URI:

    <head>
        <base href="http://localhost:8080/context-name/">
    </head>

Again, note the trailing slash, just after the context name.

The above examples illustrate the use of hard-coded literal values to set
the base URL.

If the web application were to be deployed to another server, with a
different base URL, one would have to edit each and every JSP that specified
a base URL. In a large application that would be a tiresome and error-prone
exercise!

There is a simple solution to this problem: You can dynamically retrieved
the base URL from the ServletContext initialisation parameters at runtime:

    <% String baseURL = application.getInitParameter( "baseURL" ); %>

And then set the HREF attribute value with a JSP expression:

    <head>
        <base href="<%= baseURL %>">
    </head>

I have a custom tag that takes care of this in all my JSPs.

Initialisation parameters are defined in a web application's 'web.xml'
deployment descriptor file. If the base URL were to change, one would simply
edit the 'web.xml' parameter, and leave the JSPs untouched!

When you specify a base URL you must take care to specify relative URLs
correctly for all the links in a JSP document.

The fundamental rule is: do NOT use leading forward slashes in the URLs.

An image file named 'flower.gif' located in the context root directory would
be referenced as follows:

    <img src="flower.gif">

Notice there is no leading forward slash. The same rule applies to links for
HTML and JSP documents located in the context root directory:

    <a href="shopping.jsp"><img src="basket.gif"></a>

If a link is to a resource that is located in a sub-directory, forward
slashes are used to delineate the pathname, but a slash is still not
required at the start of the URL:

    <img src="images/flower.gif">

In the above example, the 'flower.gif' file is assumed to be located in a
folder named 'images' which is itself located in the context root directory.

Finally, this is how you would specify a raw link to a Servlet named 'Login'
that is packaged under the ubiquitous 'com.foo.bar' name:

    <form action="servlet/com.foo.bar.Login" method="post">

Servlets can (and should) be registered and mapped in a web application's
'web.xml' deployment descriptor file. This enables you to reference them
with simple names.

For example, if the above 'com.foo.bar.Login' Servlet was mapped to
"/login", then the link to this Servlet would be:

    <form action="login" method="post">

Again, notice there is no leading forward slash.


Good luck.

Harry Mantheakis
London, UK


> 
> The requests should be like
> /controller/dir/fileXXX.html
> 
> This is done with a
> 
> <servlet-mapping>
>       <servlet-name>Controller</servlet-name>
>       <url-pattern>/controller/*</url-pattern>
> </servlet-mapping>
> 
> 
> request.getPathInfo() is "dir/fileXXX.html"
> 
> 
> fileXXX.html in reality are JSPs, which the Controller
> servlet forwards to.
> 
> In the JSPs are relative links to images and CSS.
> Well, as in the browsers view the html files are below
> /controller, it requests these relative links also
> below /controller, but the controller cannot and
> should not handle CSS and images.
> 
> This problem surfaces often in the newsgroups, but I
> did not find a solution.
> 
> Is there a good, portable solution to the problem ?
> 
> Ideally would be a
> <url-pattern-exclude>*.css,*.jpg,*.gif</url-pattern-exclude>
> Tag in web.xml.
> 
> Thanks, Juergen
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail SpamGuard - Read only the mail you want.
> http://antispam.yahoo.com/tools
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 


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