1

An existing Java site is designed to run under "/" on tomcat and there are many specific references to fixed absolute paths like "/dir/dir/page".

Want to migrate this to Java EE packaging, where the site will need to run under a context-root e.g. "/dir/dir/page" becomes "/my-context-root/dir/dir/page"

Now, the context-root can be easily with ServletRequest.getContextPath(), but that still means a lot of code changes to migrate a large code base. Most of these references are in literal HTML.

I've experimented with using servlet filters to do rewrites on the oubound HTML, and that seems to work fine. But it does introduce some overhead, and I wouldn't see it as a permanent solution. (see EnforceContextRootFilter-1.0-src.zip for the servlet filter approach).

Are there any better approaches to solving this problem? Anything obvious I'm missing? All comments appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
tardate
  • 16,424
  • 15
  • 50
  • 50
  • I realize may be stating the obvious, but JavaEE apps can still be deployed to the root context (/). This configuration is usually handled in a appserver-specific deployment file (e.g. jboss-web.xml, sun-web.xml). – jt. Sep 27 '08 at 05:03
  • @jt good point, and I did ask if I was missing anything obvious;-) In my case I need the app to be deployed along with other apps on the same server, and can't hog the root context (and also not acceptable to run on another virtual host). – tardate Sep 27 '08 at 06:44

2 Answers2

1

Check out a related question

Also consider URLRewriteFilter

Another thing (I keep editing this darn post). If you're using JSP (versus static HTML or something else) you could also create a Tag File to replace the common html tags with links (notably a, img, form). So <a href="/root/path">link</a> can become <t:a href="/root/path">link</t:a>. Then the tag can do the translation for you.

This change can be easily done "en masse", using something like sed.

sed -e 's/<a/<t:a/g' -e 's/<\/a>/<\/t:a>/g' old/x.jsp > new/x.jsp

Form actions may be a bit trickier than sed, but you get the idea.

Community
  • 1
  • 1
Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • @will thanks for the x-ref. I actually posted there and prop'd your answer. I forked this question to specifically address migrating existing code, whereas the other q is really about best practice context-handling in code you are building I think – tardate Sep 27 '08 at 06:48
  • URLRewriteFilter has very flexible inbound rule capabilities, however outbound mapping is currently restricted to rewriting urls that go through response.encodeURL(). – tardate Sep 27 '08 at 07:38
0

the apache world used Redirects(mod_rewrite) to do the same.

The Servlet world started using filters

The ruby world (or the RoR) does more of the same stuff and they call it routing.

So, there's no getting around it (Unless you want to use smart regex through out -- which has been tried and it works just fine).

anjanb
  • 12,999
  • 18
  • 77
  • 106
  • apache rewrites could be used in the case also, but I prefer the filter approach since it is local to the specific app. Ensuring the apache rewrite doesn't mess other deployed apps I think would be tough if you have many deployed on the same server. – tardate Sep 27 '08 at 06:50
  • btw, yes I am a huge fan of routing in rails. It is the way URLs should always have been managed, but unfortunately the java world started down a different path a long time ago;-) – tardate Sep 27 '08 at 06:51