This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes modifying request parameter values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "modifying request parameter values" Watch "modifying request parameter values" New topic
Author

modifying request parameter values

Dan Freeman
Greenhorn

Joined: Jan 23, 2004
Posts: 4
Does any one know if it is possible to modify the parameter values in the request object. Then when the request is forwarded to another servlet, that servlet will read the modified value(s)?
By browsing through the servlet api and the struts api there are no methods to do this. So, I'm looking for some code that reads the input stream, modifies the headers and then forwards that request stream.
Thanks,
Dan
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I hope I understood what you are asking. But couldn't you wrap the ServletRequest in the java.servlet.http.HttpServletRequestWrapper object, subclass it and provide your implementation of the getParameter(String str) method.
Craig.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Also, this could be used in conjunction with or not with the javax.servlet.Filter interface.
my $0.02
Craig.
Dan Freeman
Greenhorn

Joined: Jan 23, 2004
Posts: 4
Originally posted by cj jack:
I hope I understood what you are asking. But couldn't you wrap the ServletRequest in the java.servlet.http.HttpServletRequestWrapper object, subclass it and provide your implementation of the getParameter(String str) method.
Craig.

I tired sub classing the java.servlet.http.HttpServletRequestWrapper object but I do not have access to the parameter map in it, as it is private or friendly. If I had access to it, I could then modify it and set the parameter values, add/modify values. Seems it would be a handy thing to do. Maybe I'd have to modify the source for this. Or, if the parameter map is friendly I could add a file in the java.servlet.http package and rejar.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
As an aside, do you really need to change the parameters? The usual way to accomplish this sort of operation is to process the parameter values and put the results in the request context. As a simple example ...
in the sending servlet/JSP put something like:

then in the destination servlet/JSP do something like:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Dan Freeman
Greenhorn

Joined: Jan 23, 2004
Posts: 4
Thanks Frank, this is what I wound up doing. I just didn't want to add the logic to the called servlet to do the extra check. I guess modifying the request parameter values directly could have some side effects that could violate the servlet container, or something like that?
It's intersting though, because in Jsp with the tag:
<jsp:include page="/jsp/myJsp.jsp" flush="true" >
<jsp aram name="myParamName" value="<%=myVar%>" />
</jsp:include>
you can add or modify parameters, but you can't do this in servlet code.
Some sort of difference between an include and forwarding a request...
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Dan wrote: I tired sub classing the java.servlet.http.HttpServletRequestWrapper object but I do not have access to the parameter map in it, as it is private or friendly. If I had access to it, I could then modify it and set the parameter values, add/modify values. Seems it would be a handy thing to do. Maybe I'd have to modify the source for this. Or, if the parameter map is friendly I could add a file in the java.servlet.http package and rejar.
In this situation I would simply add an extra Map to the new "wrapper" class, and override the getParameter method to check my map first:

Does that make sense?
Dan Freeman
Greenhorn

Joined: Jan 23, 2004
Posts: 4
That makes sense, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: modifying request parameter values
 
Similar Threads
getParameterValues() qestion.
Retrive data from requst object without using getParameter()
Regarding ActionContext's ContextMap
file upload using doPut()
how can I read form fields in another html or jsp page