Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Extending HttpServletRequestWrapper to change the URL format

843842Aug 30 2008 — edited Aug 31 2008
Hi,

I am using Struts 2.0 for all my web apps.

I would like to convert a none usual url format to a standard url format without having to redirect the user.

This is my goal:
http://myhost.com/MyAppName/MyActionName.action/Param1Value/Param2Value/Param3Value

Redirecting the user works, but I don't want this, so I attempted to override the HttpServletRequestWrapper class to overwrite the URI and push parameters into the request as required. But I am ending to an error page and I don't understand why.

This is MyClass:
public class FlightHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private String uri;
    private String queryString;

    public FlightHttpServletRequestWrapper(HttpServletRequest httpServletRequest) {
        super(httpServletRequest);

        // New url pattern
        String context = httpServletRequest.getContextPath();

        String paramStr = httpServletRequest.getRequestURI().split(context)[1];
        String params[] = paramStr.split("/");

        String origin = params[2];
        String destination = params[4];
        String originName = params[3];
        String destinationName = params[5];

        StringBuffer buf = new StringBuffer("model.from=");
        buf.append(origin);
        buf.append(" -- [");
        buf.append(originName.replaceAll("_", " "));
        buf.append("]");
        buf.append("&model.to=");
        buf.append(destination);
        buf.append(" -- [");
        buf.append(destinationName.replaceAll("_", " "));
        buf.append("]");
        buf.append("&model.flightType=RT&model.search=true");

        String realOrigin = origin + " -- [" + originName.replaceAll("_", " ") + "]";
        String realDestination = destination + " -- [" + destinationName.replaceAll("_", " ") + "]";

        setAttribute("model.from", realOrigin);
        setAttribute("model.to", realDestination);
        setAttribute("model.flightType", "RT");
        setAttribute("model.search", "true");

        queryString = buf.toString();
        uri = context + "/flightSearch.action";
    }

    public String getRequestURI() {
        return uri;
    }

    public StringBuffer getRequestURL() {
        StringBuffer buffer = new StringBuffer(uri);
        buffer.append("?");
        buffer.append(queryString);

        return buffer;
    }
}
What I am getting from Tomcat is the following error:
The requested resource (/TripSearch/flightSearch.action) is not available.

But this should be it.

Anybody sees / knows what I am missing ?

- Pascal

Edited by: Euphreme__ on Aug 30, 2008 7:24 AM

Edited by: Euphreme__ on Aug 30, 2008 7:26 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 28 2008
Added on Aug 30 2008
1 comment
850 views