Tomcat8 console控制台不正常显示中文

这个问题捯饬了一下午,虽然好像不太影响正在学的东西,但是整不明白感觉人都不好了。

问题的起因是imooc教程介绍的时候用的是tomcat6,可是tomcat8改变了很多

最重要的一点

tomcat8之前的默认编码是ISO-8859-1,而tomcat8的默认编码为utf-8。

所以当我在代码里面这么写:


index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    




Insert title here


	
name:
age:
submit:

Servlet.java

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		PrintWriter out = response.getWriter();
		String name2 = new String(name.getBytes("iso-8859-1"),"UTF-8");
		out.println("name = "+name2+", age = "+age);
		System.out.println("name = "+name2);
		System.out.println("age = "+age);
		System.out.println("有可能");
		String encoding=System.getProperty("file.encoding");
		System.out.println("Default System Encoding: " + encoding);		
	}

页面的显示是ok的:

name = 张三, age = 12

但是输出在控制台永远显示是:

name = 张三
age = 12
有可能
Default System Encoding: UTF-8

神了个奇的。 我开始各种设置编码,按照网络上各种攻略,什么设置成GBK 各种设置,然而什么用都没有

最后才发现原来tomcat版本不一样,结果也会不一样。

charset=UTF-8 
pageEncoding="UTF-8"

把index.jsp改成:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    




Insert title here


	
name:
age:
submit:

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		PrintWriter out = response.getWriter();
		out.println("name = "+name+", age = "+age);
		System.out.println("name = "+name);
		System.out.println("age = "+age);
		System.out.println("有可能");
		String encoding=System.getProperty("file.encoding");
		System.out.println("Default System Encoding: " + encoding);
		
		
	}

好了  解决了

name = 张三
age = 12
有可能
Default System Encoding: UTF-8


参考文档:

http://my.oschina.net/ijustdoit/blog/397481

http://www.aichengxu.com/view/1148214

你可能感兴趣的:(Tomcat8 console控制台不正常显示中文)