I have a filter where I'm setting request.setCharacterEncoding("UTF-8") after fetching the parameters, for which the encoding is not set. In the process method I'm just checking if request contains any special chars which are removed.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
getParams(request);
chain.doFilter(new RequestWrapper((HttpServletRequest) request), response);
}
private void getParams(ServletRequest request)
{
HttpServletRequest httpRequest = (HttpServletRequest) request;
Map paramMap = httpRequest.getParameterMap();
Set entrySet = paramMap.entrySet();
for (Iterator iterator = entrySet.iterator(); iterator.hasNext();)
{
Map.Entry entry = (Map.Entry) iterator.next();
String s = process(entry.getValue().toString());
//entry.setValue(s);
}
}
In the givencode I'm setting CharacterEncoding in the next filter in the chain.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
However, if I'm setting it in first filter before fetching parameters its working fine.
So is it necessary to set CharacterEncoding before fetching the params. I'm not clear with how things are working.