I am having a jsp page on which i am having textbox,textarea and a combobox. while getting the values in Filter from request I want to know whether it is textrarea or so on. So is it possible? also in filter can i change the values of request?
2 Answers
A filter is a servlet. You can get and change values of parameters from a request just as well as in the usual servlet.
public class CheckFilter implements Filter {
public void init(FilterConfig filterConfig) {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String contentTextarea = request.getParameter("myTextarea");
String modifyContentTextarea = changeContent(contentTextarea);
chain.doFilter(request, response);
}
private String changeContent(String content) {
//to do smth with value of content
return modifyContent;
}
public void destroy() {
}
}
And mark your textarea in JSP like this:
<textarea name="myTextarea"></textarea>

- 3,546
- 2
- 25
- 38
-
I want to change the value and set back to the request in the same parameter. So is it possible? – TaherT May 21 '12 at 07:05
-
1An instance of class `HttpServletRequest` doesn't have a method `setParameter()`. Use class [HttpServletRequestWrapper](http://docs.oracle.com/cd/E17802_01/products/products/servlet/2.3/javadoc/javax/servlet/http/HttpServletRequestWrapper.html) to solve your issue. You can create subclass of `HttpServletRequestWrapper` and override the method `getParameter()` to return your specified value. Then pass an instance of your custom `HttpServletRequestWrapper` to `chain.doFilter()` instead of the original request. – kapandron May 21 '12 at 07:36
-
Also you can get a value of a parameter of a request, modify it and set as an attribute in the request with metod `request.setAttribute("myTextarea", valueParam)`. And then get the value of this attribute in a servlet with method `request.getAttribute("myTextarea")` and work with it. – kapandron May 21 '12 at 07:37
It's not possible to determine the whether you've used a textarea/radio/checkbox to populate a parameter as the request just contains the key/value pair of the parameter name and value, you should know what form field was used to generate the parameters you're expecting on the server already, I don't see the need for this. If you really needed this for some reason you would need to append another parameter to the request containing the information about which form field was used to set a parameter but this is going to start getting very messy very quickly.
For the second part, yes you can modify any values in a request inside of a servlet or filter.

- 266
- 3
- 9
-
I want to change the value and set back to the request in the same parameter. So is it possible? – TaherT May 21 '12 at 07:07
-
1Check this link, it contains information I think will be useful. http://stackoverflow.com/questions/1413129/modify-request-parameter-with-servlet-filter – Rene May 21 '12 at 07:14