tags:

views:

153

answers:

2

I always wondered why there exists no removeParameters() method in Servlet API. What could be the motive behind this design? Here is a scenario: I am posed with a challenge in a proprietary MVC framework that I am compelled to use. This framework uses a Controller Servlet that hosts an algorithm in it's post method:

doPost() {
//create instance of action - just like struts action
action.init
action.preexecution
if(redirection state is not set)
action.process
action.postprocess
action.finish
}

The only way I can skip process of any particular action would be by setting a redirection url. The Controller Servlet is FINAL. Now, when I do a requestdispatcher.forward from say the preexecution method of an action, the controller will go ahead and execute the rest of the methods and not skip the rest. I cannot change this behavior, neither can I set the redirect, coz I need to do a forward. It works fine as long as I am not forwarding request to the same action. When a request is forwarded to the same action, the http parameters are all the same. This would take it into a never ending loop. Hence, I am compelled to add extra parameters indicating that it is a repeat request and should be treated differently. Not sure if my problem made sense, but thought this is a good forum to post the same.

+6  A: 

Umm... because it would serve no purpose? Request parameters are sent by the client to the server. The server is free to ignore them, but what practical effect would you expect such a removeParameter() method to have?

Edit: Request parameters are meant for the communication between server and client. For server-internal communication, you can use request attributes, which can be set and removed.

Michael Borgwardt
edited my post with more information that should answer your question
Jay
+3  A: 

EDIT: McDowell reminded me of HttpServletRequestWrapper, so I'm changing the below to make it a little less work... Thanks McD!

You can decorate the request to "hide" parameters you don't want and/or add extra parameters.

Something like (off the top of me head -- no compiling so the API might be a tweak off...)

public class MyParameterHider extends HttpServletRequestWrapper {
    public MyParameterHider(HttpServletRequest request) {
        super(request);
    }
    public String getParameter(String name) {
       if ("parameterToHide".equals(name))
           return null;
       return realRequest.getParameter(name);
    }
    // similar for getParameterNames and getParameterMap - don't include the hidden parm
    // all other methods are strictly pass-through and are automatically
    //   handled by HttpServletRequestWrapper
}

In your forward, just wrap the request in a ParameterHider when calling doFilter:

dispatcher.forward(new MyParameterHider(request), response);

Patterns FTW!

Hope this helps!

Scott Stanchfield
this is a nice solution to the problem!
Jay
thanks, glad to assist! just so happens I've had to do something similar in the past ;)
Scott Stanchfield
It would be better to extend HttpServletRequestWrapper if possible - that interface has changed in the past and there'd be less to implement.
McDowell
Good point McD! I forgot about that one. I'll tweak the above...
Scott Stanchfield
@McD @Scott - Thank you for you repsonses. They were helpful.
Jay
no problem! glad to help!
Scott Stanchfield