I am using a javax.servlet.http.HttpServletRequest to implement a web application.
I have no problem to get the parameter of a request using the getParameter method. However I don't know how to set a parameter in my request.
I am using a javax.servlet.http.HttpServletRequest to implement a web application.
I have no problem to get the parameter of a request using the getParameter method. However I don't know how to set a parameter in my request.
You can't, not using the standard API.
HttpServletRequest
represent a request received by the server, and so adding new parameters is not a valid option (as far as the API is concerned).You could in principle implement a subclass of
HttpServletRequestWrapper
which wraps the original request, and intercepts thegetParameter()
methods, and pass the wrapped request on when you forward.If you go this route, you should use a
Filter
to replace yourHttpServletRequest
with aHttpServletRequestWrapper
:From your question, I think what you are trying to do is to store something (an object, a string...) to foward it then to another servlet, using RequestDispatcher(). To do this you don't need to set a paramater but an attribute using
and then
Sorry, but why not use the following construction:
As mentioned in the previous posts, using an HttpServletReqiestWrapper is the way to go, however the missed part in those posts was that apart from overriding the method getParameter(), you should also override other parameter related methods to produce a consistent response. e.g. the value of a param added by the custom request wrapper should also be included in the parameters map returned by the method getParameterMap(). Here is an example:
If you really want to do this, create an HttpServletRequestWrapper.
The missing getParameterMap override ended up being a real problem for me. So this is what I ended up with: