开发者

Filters in tomcat

开发者 https://www.devze.com 2023-01-29 04:54 出处:网络
I am facing a task to add a dependent jar application to an existing one. The existing one does not use to much of locale benefits and so o开发者_如何学Cn, but my new one should.

I am facing a task to add a dependent jar application to an existing one. The existing one does not use to much of locale benefits and so o开发者_如何学Cn, but my new one should.

So like I have now: localhost:8080/old-app

I want to have also: localhost:8080/[en|fr|...]/new-module

Could anyone point me the direction, because even if I think I get the idea of filters, filter-mapping, I cannot manage to solve it.

I would like to keep the old one and also have access to the new one.


Deploy new-module as ROOT.war (or set path in /META-INF/context.xml to /). Use Tuckey's URLRewriteFilter to rewrite specific URL's and transform the language part to a request parameter so that it's available by request.getParameter(). It's much similar to Apache HTTPD's mod_rewrite.

An alternative to URLRewriteFilter is to homegrow a custom filter which does like the following in doFilter() method.

String uri = request.getRequestURI();
if (uri.matches("^/\\w{2}(/.*)?$")) {
    request.setAttribute("language", uri.substring(1, 3));
    request.getRequestDispatcher(uri.substring(3)).forward(request, response);
} else {
    chain.doFilter(request, response);
}

Map this on an url-pattern of /*. The language will be available by request.getAttribute("language") on the forwarded resource.


If you dont want the applications name as context root e.g. localhost:8080/appname but under / directly you have to put it into the tomcat/webapps/ROOT folder. To get more sophisticated URL mappings working have a look at http://ocpsoft.com/prettyfaces/

0

精彩评论

暂无评论...
验证码 换一张
取 消