aspose file tools*
The moose likes Servlets and the fly likes Replacing Request Parameter Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Replacing Request Parameter Values" Watch "Replacing Request Parameter Values" New topic
Author

Replacing Request Parameter Values

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi, I was making a wrapper class for my filter class. This class trims, and strips off all the "<" and ">" characters. Before I continue, here's my wrapper and filter classes.




and Here's the Servlet filter



as you can see above, I need to alter the request parameters with the results of getParameterValues(String) from the RequestWrapper class. I was told the getParameterValues(String) method would automatically be invoked when chain.doFilter() is called, but it seems untrue. Is there any way I can alter the request parameter values received by the servlet in any way? Making a request attribute doesn't seem to be the best solution. Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
Creating a wrapper around the request is the correct way to go, now you just need to override

to return your trimmed and < > removed parameters.

You can never replace the original request parameters, you just use a wrapper class to pass back different parameter values.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

As you may have noticed, there is no setParameter() method.
You'll have to figure out another way, like using attributes instead.


[My Blog]
All roads lead to JavaRanch
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Darren, Satou thank you very much.


Satou

I am using Struts, creating a request attribute would create so much work for me and would therefore render this feature(of setting of bean properties automatically) useless. There must be another way...

Darren



I already did this, here...




I still can't trim or replace the characters I need to filter. Thank you very much for your answers they were very helpful.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The getParameterValues method isn't automatically going to be invoked in the filter but it doesn't need to. Since your passing your wrapper to the doChain method, any servlets or JSPs that receive that requestWrapper will use it's getParameterValues instead of the one in the original request.

What problems are you having?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Ben, the problem is that the getParameterValues() method is not invoked in my ActionServlet at all... In my Action servlet, the execute method still has the HttpServletRequest as the argument for request. Is that ok? If that's ok, I'm not using the HttpServletRequest implicitly in my execute method since this is automatically done for my by struts. So what I do is...


ApplicantFormBean applicant = (ApplicantFormBean)form;

in the background, this is setting values for the bean, maybe something like this:

applicant.getName().setFirstName( request.getParameter("firstName) );

so, after struts did all the dirty work, I could use the bean method like this...

applicant.getName().getFirstName();

the latter should get the firstName parameter(the trimmed one), however, it doesn't... What do you think is the problem? Thanks a lot, I hope I could hear more answers.
[ July 26, 2006: Message edited by: Timothy Sam ]
Darren Edwards
Ranch Hand

Joined: Aug 17, 2005
Posts: 69
By using debug statements in your filter have you established if it is being called? (simple System.out.println(..) statements will write to /logs/catalina.out in tomcat).

I'm not familiar with struts configuration, but it seems as though your filter is not being used.

As an aside - if you are using this to clean invalid data from request parameters it is not a good solution. What happens if some user data actually requires the user to enter trailing whitespace or < > characters? You should be able to setup validation (within struts) on your user data to make sure it does not contain invalid characters. Also make sure you escape user data when displaying it back to the user; use of <c ut value="blah" /> will automatically escape html entities.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
use of <cut value="blah" /> will automatically escape html entities.



Now that seems to be a better solution!


Well, I'm quite certain though that only the single servlet(.do) I mapped will be needing the filter.

I don't know yet if this problem is already solved or not... Got to try again later. Thanks!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Timothy Sam:
Ben, the problem is that the getParameterValues() method is not invoked in my ActionServlet at all... In my Action servlet, the execute method still has the HttpServletRequest as the argument for request. Is that ok? If that's ok, I'm not using the HttpServletRequest implicitly in my execute method since this is automatically done for my by struts. So what I do is...



The filter, if invoked, should take care of that because it intercepts the request and replaces the servletRequest object with the wrapper object that you've created. When the struts class gets ahold of it IT will call the getParameterValues method.

You may also want to override getParameter to add some debug statements; just to make sure that Struts isn't using it instead of getParameterValues.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing Request Parameter Values