• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can we change a parameter in a HttpServletRequest?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic