如下代码的中文内容在jsp页面显示问乱码,后台打印语句正常
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
out.println("<html><body><h1>开始Test01.. </h1></body></html>");
out.flush();
System.out.println("开始Test02.. ");
}
}
解决方法:
1、打开上段代码中的注释内容response.setContentType("text/html;charset=gb2312");
或request.setCharacterEncoding("GBK")
2、据说还可以在web.xml中增加过滤器,回头研究
参考文章:
http://www.enet.com.cn/article/2008/0229/A20080229170410.shtml