jQuery in Action, 2nd edition
The moose likes Servlets and the fly likes Can we change a parameter in a HttpServletRequest? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can we change a parameter in a HttpServletRequest?
 
Similar Threads
Help in database connections
Servlet handling multiple requests from applet
Value shows up in url
regarding session transfer from servlet to jsp
problem in retrieving the parameters through request.getParameter()