servlet 发送到客户端的数据编码设置

setCharacterEncoding

void setCharacterEncoding(java.lang.String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by   setContentType(java.lang.String)  or   setLocale(java.util.Locale), this method overrides it. Calling   setContentType(java.lang.String)  with the   String  of   text/html  and calling this method with the   String  of   UTF-8  is equivalent with calling   setContentType  with the   String  of   text/html; charset=UTF-8.

This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed. 


上面是api的内容:

用法:setCharacterEncoding("UTF-8");

上述api的基本翻译如下:

给准备要发送到服务端响应对象response数据的MIME字符集编码进行设置,举个例子 UTF-8

如果setCharacterEnconding方法已经设置,会覆盖另外两种方法的设置。

setContentType("String of text/html; charset=UTF-8");

上面的解释道这两种设置方式是等价的。

另外如果这个方法在getWriter方法之后设置,或者在response对象已经提交之后设置,此方法就失效了。

你可能感兴趣的:(servlet编码设置,setContextType)