URL routing and relative links in Tomcat 5.5

I'm trying to add URL routing to a web application running in Tomcat 5.5.

I want to make the dynamic page URLs more friendly, changing main.jsp?type=test&group=info to main/test/info as is all the rage.

I have set up the following in the web.xml

  <servlet>
    <servlet-name>main</servlet-name>
    <jsp-file>/main.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>main</servlet-name>
    <url-pattern>/main/*</url-pattern>
  </servlet-mapping>

and in main.jsp I use request.getPathInfo() to get the data that I need to do the routing. All this seems to work fine, and when I browse to http://mytestserver/example/main/test/info I get the html that I expect.

The problem is that any relative links on that page, images, style-sheets, javascript, etc., are now all broken, as they are being interpreted as relative to the full URL.

I can't think of an easy way to use static links because the URLs on the test server and production server are at different levels: http://mytestserver/example/ maps to http://example.com/ in production, and I really do not want to have to write code to wrap every internal link on the site or set up a virtual server for each site on the test server.

Is there anything else I can do?

Answers


I have a system like this at work.

We use a package called urlrewrite, which basically is the apache tomcat rewriter done up as a j2ee filter. It's cool, but I ran into this same problem.

To fix it, I wrote another filter which is inserted into the filter chain before the rewriter. Its job is to grab the pathinfo before the rewriter mangles it, and to stick that info into a request attribute. It resolves the pathinfo of the request against the pathinfo of the web application root, to work out how many "../" you need to get to the root of the web app (from the client's point of view).

The other components of the system then use the content of that attribute to build their relative urls. Works great.

The system is here.

Look for any taxon (MACROPODIDAE is a fine choice). All the rest-y looking links get turned into reqest parameters by the url remapper.

email me at work - Paul.Murray@environment.gov.au if you would like some source. It's not secret, but it is work so I need permission.


There are a few approaches you can use that might make your life easier:

  1. Put a base element in the head of your document. If you add a <base href="http://myserver/example/main"/> tag, then <a href="test/info">Hi!</a> will link to http://myserver/example/main/test/info.
  2. Create a jsp tag (or a function) that will generate absolute urls for you, so something like: <myserver:a href="test/info">This is a link</myserver:a> And then you can have logic in that tag to create an absolute url. Most app frameworks like struts and spring already have such tags defined.

Need Your Help

Uninitialized constant error constant Skill:Description

sql ruby-on-rails database uninitialized-constant

I am trying to make a form and submit a skill and description into a database. I am almost done however I am getting this error after submitting my info:

Android Rendering Problems Couldn't resolve resource @string/app_name

java xml android-intent android-activity

I'm new to android programming and I don't understand what I'm doing wrong.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.