Question

I need the following serverside redirect or forward on a Tomcat server: the url http://portal.customer_name needs to be renamed/redirected to http://portal.customer_name/customer_name. So in short, an url without context path to be redirected to the same url with context path. I managed to do this with a filter, using request.getRequestDispatcher().forward(request, response); but the same doesn't seem te work with a valve.

I got the following so far:

public void invoke(Request request, Response response) throws IOException, ServletException {
    String context = getForwardpath(request);
    request.getRequestDispatcher(context).forward(request, response);
    this.getNext().invoke(request, response);
}

private String getForwardpath(Request rq){
    String context = rq.getServletContext().getContextPath();
    /.../
    return context;
}

I deployed some servlets which seem to receive the original url but not the redirect. Is it possible to make a serverside redirect like this at all. Please note that I need a valve, not a filter.

Was it helpful?

Solution

Did you happen to search for this before posting? Google comes up with a blog post that discusses this topic wrt Tomcat and includes an implementation of such a Valve, as well as a JBoss implementation of one. So I'd say, "Yes, it's definitely possible." If one of these doesn't suit your needs, you could certainly at least use their source code to guide you in the right direction and then come back with a more specific question if you run into trouble.

Update: There's no such thing as a "server-side redirect" the way you're using the term. Redirecting means sending a 3xx series response code back to the user agent to tell it something about the resource it requested. That being said, I haven't done any Valve development myself, but from the description of Valve.invoke(), I'm guessing if you just put the Valve high enough up in the Tomcat configuration, you could just modify the request URI on the way in and let it be handled normally.

OTHER TIPS

You need crossContext="true" in ./conf/context.xml (tomcat) if you change the request to a different context on the same instance.

https://tomcat.apache.org/tomcat-7.0-doc/config/context.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top