I have seen a web application which deals with file processing (reciving applications from
a third party application and storing them in database for further usage).
That particular web application is also having a servlet filter configured whose only basic purpose is to set the character encoding to UTF-8.
For example :
public class ResponseFilterExample implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain) throws IOException, ServletException {
filterchain.doFilter(request, response);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
}
}
Now my question is, what is the difference between
request.setCharacterEncoding("UTF-8");
and
response.setCharacterEncoding("UTF-8");
?
Well, the difference is that one sets the encoding on the request, the other sets the encoding on the response.
The above doc links explain in more detail.
ServletRequest.setCharacterEncoding():ServletResponse.setCharacterEncoding():