• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

modifying request parameter values

 
Dan Freeman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, this could be used in conjunction with or not with the javax.servlet.Filter interface.
my $0.02
Craig.
 
Dan Freeman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Dan Freeman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic