I have a web-app that I would like to extend to support multiple languages with new URLs. For example, www.example.com/home.do stays English, but www.example.com/es/home.do is Spanish. My first thought was to create a Filter which rewrites incoming urls like /es/home.do to /home.do (and sets the Locale in the Request); this works fine. The Filter wraps the ServletRequest with an HttpServletRequestWrapper which overrides getContextPath() to return the language:
class FakeContextRequest extends HttpServletRequestWrapper {
private String context = "";
FakeContextRequest(HttpServletRequest request, String context) {
super(request);
// snip some validation code
this.context = request.getContextPath() + context;
}
@Override
public String getContextPath() {
return this.context;
}
}
My Filter forwards to the appropriate request as follows:
FakeContextRequest fr = new FakeContextRequest(request, lang);
fr.getRequestDispatcher(newResourceName).forward(fr, response);
My problem is that the next servlet doesn't forward properly. The next servlet (typically a Struts ActionServlet) forwards to a JSP (often using Struts Tiles); when I get to the JSP the HttpServletRequest has been wrapped several times and the object in question reports the context to be empty (the root context, which is where the application is actually deployed).
I want the context to be re-written so that all my context-aware code that already exists can automatically insert the language into the URLs that are written. Is this possible?
Edit: I solved my problem by using a wrapped HttpServletResponse instead of a wrapped HttpServletRequest; I rewrite the URL in the response.encodeURL() method.