首先先把文档给大家贴过来,
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.
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:
pageEncoding
attribute of the
page
directive of the page. It is a translation-time error to name different encodings in the
pageEncoding
attribute of the page directive of a JSP page and in a JSP property group.
CHARSET
value of the
contentType
attribute of the
page
directive.
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=iso8859-1"%>
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.
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:
CHARSET
value of the
contentType
attribute of the
page
directive
pageEncoding
attribute of the
page
directive
比如你写了
jsp1:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=iso8859-1"%>
<%
out.print("JSP的中文处理");%>
会发现是乱码,而如果加了response.setCharacterEncoding("UTF-8");则不是乱码
jsp2:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=iso8859-1"%>
<%
response.setCharacterEncoding("UTF-8");
out.print("JSP的中文处理");%>
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("\r\n");
out.write("\r\n");
out.print("JSP鐨勪腑鏂囧鐞?);
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("");
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("\r\n");
out.write("\r\n");
response.setCharacterEncoding("UTF-8"); //response.setCharacterEncoding("UTF-8");这句的作用
out.print("JSP鐨勪腑鏂囧鐞?);
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write(" ");
参考:http://docs.oracle.com/javaee/1.4/tutorial/doc/WebI18N5.html
http://bijian1013.iteye.com/blog/1841029