• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we change a parameter in a HttpServletRequest?

 
ming zhu
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I want to do:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOExceptin, ServletException {

String url = �AuthenticationServlet�;
String username = request.getParameter(�username�);
if (username.indexOf("something")>=0) {
// I want to change the parameter
// called password in request
// before I forward it to the
// AuthenticationServlet
}
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(request, response);
}
Any suggestions?
 
Ken Pelletier
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to be able to achieve this by implementing a Filter that uses an HttpServletRequestWrapper subclass of your own.
The wrapper would override the relevant getParameter* method(s) to return the value for the particular parameter(s) you're interested in "changing".
I don't know whether the getParameterMap() and getParameterValues() implementations in HttpServletRequestWrapper call the underlying getParameter() method, and therefore would not need to be overridden in additon to getParameter() ( my guess is not, but you'd need to verify this ).
This way, all servlets downstream in the chain will unknowingly interact with the wrapped request and get the "changed" parameter value you're wrapper returns.
 
ming zhu
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken,
Thanks for the help. Do you mean I need do the following:
public class newRequest extends HttpServletRequestWrapper {
String getParameter(String par) {
if (par.indexOf("password") == 0) {
return "my_new_password";
} else {
return super(par);
}
}
}
And then in the doPost method:
rd.forward(newRequest, response);
Thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic