Monday, September 17, 2007

Settings Parameters in HttpServletRequest

Sometimes we need to set some parameters in a request when forwarding the request to a new jsp or a servlet. We can achieve this thing using the class javax.servlet.http.HttpServletRequestWrapper.

We have to extend a the above class and define few methods like setParameter() and getParameter() and we are ready to go !!


public class RequestWrapper extends HttpServletRequestWrapper {
private HashMap fakedParameters;
private RequestWrapper(HttpServletRequest nested)
{
super(nested);
fakedParameters = new HashMap(nested.getParameterMap());
}
public void setParameter(String key, String value)
{
fakedParameters.put(key, value);
}
public String getParameter(String name)
{
return (String) fakedParameters.get(name);
}
}


we can then use this wrapper object as a new request !!


No comments: