File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Can we change a parameter in a HttpServletRequest? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we change a parameter in a HttpServletRequest?" Watch "Can we change a parameter in a HttpServletRequest?" New topic
Author

Can we change a parameter in a HttpServletRequest?

ming zhu
Greenhorn

Joined: Feb 13, 2001
Posts: 16
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

Joined: Aug 01, 2002
Posts: 54
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

Joined: Feb 13, 2001
Posts: 16
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we change a parameter in a HttpServletRequest?