Request Encoding ,Page Encoding,Response Encoding 以及中文乱码的原因

来源:互联网 发布:warframe淘宝买白金 编辑:程序博客网 时间:2024/06/05 14:31

首先先把文档给大家贴过来,

Request Encoding

The request encoding is the character encoding in which parameters in an incoming request are interpreted. Currently, many browsers do not send a request encoding qualifier with theContent-Type header. In such cases, a web container will use the default encoding--ISO-8859-1--to parse request data.

If the client hasn't set character encoding and the request data is encoded with a different encoding from the default, the data won't be interpreted correctly. To remedy this situation, you can use theServletRequest.setCharacterEncoding(String enc) method to override the character encoding supplied by the container. To control the request encoding from JSP pages, you can use the JSTLfmt:requestEncoding tag. You must call the method or tag before parsing any request parameters or reading any input from the request. Calling the method or tag once data has been read will not affect the encoding.

Page Encoding

For JSP pages, the page encoding is the character encoding in which the file is encoded.pageEncoding是指定该jsp文件用什么编码编译成class

For JSP pages in standard syntax, the page encoding is determined from the following sources:

  • The page encoding value of a JSP property group (see Setting Properties for Groups of JSP Pages) whose URL pattern matches the page.
  • The pageEncoding attribute of the page directive of the page. It is a translation-time error to name different encodings in thepageEncoding attribute of the page directive of a JSP page and in a JSP property group.
  • The CHARSET value of the contentType attribute of thepage directive.  
    注意,这三种方法是有优先级的,第一个最优先,比如你写了
    <%@ page language="java" pageEncoding="UTF-8"%><%@ page contentType="text/html;charset=iso8859-1"%>

    那么jsp会随着pageEncoding="UTF-8"编码编译成class。

If none of these is provided, ISO-8859-1 is used as the default page encoding.

For JSP pages in XML syntax (JSP documents), the page encoding is determined as described in section 4.3.3 and appendix F.1 of the XML specification.

The pageEncoding and contentType attributes determine the page character encoding of only the file that physically contains thepage directive. A web container raises a translation-time error if an unsupported page encoding is specified.

Response Encoding

The response encoding is the character encoding of the textual response generated by a web component. The response encoding must be set appropriately so that the characters are rendered correctly for a given locale. A web container sets an initial response encoding for a JSP page from the following sources:

  • The CHARSET value of the contentType attribute of thepage directive
  • The encoding specified by the pageEncoding attribute of thepage directive
  • The page encoding value of a JSP property group whose URL pattern matches the page
注意,这三种方法是有优先级的,第一个最优先。

比如你写了

jsp1:

<%@ page language="java" pageEncoding="UTF-8"%><%@ page contentType="text/html;charset=iso8859-1"%><html><body><%out.print("JSP的中文处理");%></body></html>
会发现是乱码,而如果加了response.setCharacterEncoding("UTF-8");则不是乱码

jsp2:

<%@ page language="java" pageEncoding="UTF-8"%><%@ page contentType="text/html;charset=iso8859-1"%><html><body><%response.setCharacterEncoding("UTF-8");out.print("JSP的中文处理");%></body></html> 

咱们看一下两个jsp生成的servlet

jsp1:显示的页面是乱码

 response.setContentType("text/html;charset=iso8859-1");   //<%@ page contentType="text/html;charset=iso8859-1"%>这句的作用      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write("\r\n");      out.write("\r\n");      out.write("\r\n");      out.write("<html>\r\n");      out.write("<body>\r\n");out.print("JSP鐨勪腑鏂囧鐞?);      out.write("\r\n");      out.write("</body>\r\n");      out.write("\r\n");      out.write("</html>");

jsp2:显示的页面不是乱码

  try {      response.setContentType("text/html;charset=iso8859-1");      pageContext = _jspxFactory.getPageContext(this, request, response,      null, true, 8192, true);      _jspx_page_context = pageContext;      application = pageContext.getServletContext();      config = pageContext.getServletConfig();      session = pageContext.getSession();      out = pageContext.getOut();      _jspx_out = out;      out.write("\r\n");      out.write("\r\n");      out.write("\r\n");      out.write("<html>\r\n");      out.write("<body>\r\n");response.setCharacterEncoding("UTF-8"); //response.setCharacterEncoding("UTF-8");这句的作用out.print("JSP鐨勪腑鏂囧鐞?);      out.write("\r\n");      out.write("</body>\r\n");      out.write("\r\n");      out.write("</html> ");

为什么2个jsp编译的servlet类中中文都是乱码?(out.print("JSP鐨勪腑鏂囧鐞?);),不太清楚这个现象(虽然不影响我们分析),如果你知道,可以回复给我哈。

参考:http://docs.oracle.com/javaee/1.4/tutorial/doc/WebI18N5.html

http://bijian1013.iteye.com/blog/1841029

原创粉丝点击