开发者

Setting character encodiing using ServletRequest and ServletResponse

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():

Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effect.

ServletResponse.setCharacterEncoding():

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜