编码和Servlet API:setContentType或setCharacterEncoding [英] Encoding and Servlet API: setContentType or setCharacterEncoding

查看:50
本文介绍了编码和Servlet API:setContentType或setCharacterEncoding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道幕后是什么.实际上,似乎我们可以通过以下方式设置编码:

Just wonder what is behind the scene. Actually it seems that we can set the encoding with:

  • response.setContentType("text/html; charset = UTF-8")
  • response.setCharacterEncoding("UTF-8")

有什么区别?

推荐答案

void setCharacterEncoding(String charset)将发送到客户端的响应的字符编码(MIME字符集)设置为例如UTF-8.如果已经通过 setContentType(java.lang.String) setLocale(java.util.Locale)设置了字符编码,则此方法将覆盖它.使用 text/html 的字符串调用 setContentType(java.lang.String)并使用UTF-8的字符串调用此方法等效于调用 setContentType ,其字符串为 text/html;charset = UTF-8 .

void setCharacterEncoding(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.


void setContentType(String type)如果尚未提交响应,则设置要发送到客户端的响应的内容类型.给定的内容类型可以包括字符编码规范,例如 text/html; charset = UTF-8 .

void setContentType(String type) Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8.

这篇关于编码和Servlet API:setContentType或setCharacterEncoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆