这个是很久以前的笔记,最近遇到一个编码问题,重新把它翻出来了。
这个只和java servlet有关,现在通常都用各种框架,很少会直接用到Servlet了。
查看servlet源代码的方法。因为servlet只是一些接口,并不是真正的实现,所以,如果想看真正的代码。
要去下对应的服务器的实现的源代码。比如Tomcat的代码在这里:
http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.33/src/
在servlet里有两种方法可以输出:
PrintWriter writer = response.getWriter(); ServletOutputStream outputStream = response.getOutputStream();
response.setCharacterEncoding("utf-8"); //这句话并不能解决编码问题 ServletOutputStream outputStream = response.getOutputStream(); outputStream.println("中文");我们在浏览器上,可以查看页面编码,可以发现的确是utf-8编码,但是为什么response.setCharacterEncoding("utf-8"),而还是乱码?
真正的罪人是ServletOutputStream,它根本没有实现编码转换。我们可以看下它是怎样实现的:
public void print(String s) throws IOException { if (s==null) s="null"; int len = s.length(); for (int i = 0; i < len; i++) { char c = s.charAt (i); // // XXX NOTE: This is clearly incorrect for many strings, // but is the only consistent approach within the current // servlet framework. It must suffice until servlet output // streams properly encode their output. // if ((c & 0xff00) != 0) { // high order byte must be zero String errMsg = lStrings.getString("err.not_iso8859_1"); Object[] errArgs = new Object[1]; errArgs[0] = new Character(c); errMsg = MessageFormat.format(errMsg, errArgs); throw new CharConversionException(errMsg); } write (c); } }
我们再用PrintWriter来输出:
response.setCharacterEncoding("utf-8"); PrintWriter writer = response.getWriter(); writer.println("中文");
我们再看看PrintWriter是怎样工作的:
在Tomcat中PrintWriter实际上是org.apache.catalina.connector.CoyoteWriter类,
public void print(String s) { if (s == null) { s = "null"; } write(s); } public void write(String s, int off, int len) { if (error) return; try { ob.write(s, off, len); } catch (IOException e) { error = true; } } public void write(String s) { write(s, 0, s.length()); } public void write(String s, int off, int len) { if (error) return; try { ob.write(s, off, len); //ob是org.apache.catalina.connector.OutputBuffer类 } catch (IOException e) { error = true; } }
public void write(String s, int off, int len) throws IOException { if (suspended) return; charsWritten += len; if (s == null) s = "null"; //这里进行编码转换,conv的声明:protected C2BConverter conv; //在调试过程中可以看到C2BConverter中的存放的正是utf-8编码。 conv.convert(s, off, len); conv.flushBuffer(); }
response.setCharacterEncoding("utf-8"); ServletOutputStream outputStream = response.getOutputStream(); PrintStream printStream = new PrintStream(outputStream); printStream.write("中文".getBytes("utf-8"));
要想在tomcat中一劳永逸解决乱码问题,可以这样做:
1.设置tomcat,conf/server.xml文件中,useBodyEncodingForURI="true":
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" useBodyEncodingForURI="true"/>2.增加一个filter:
public class CodeFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } @Override public void destroy() { } }3.在web.xml中配置filter:
<filter> <filter-name>CodeFilter</filter-name> <filter-class>com.leg.filter.CodeFilter</filter-class> </filter> <filter-mapping> <filter-name>CodeFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping>