Servlet打印HTML页面乱码问题

代码就在下面,最开始我以为是request和response的编码问题,还去搞了一个filter,结果最后发现跟filter半毛钱关系都没有,都是因为没有打印这么一句

out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">");

input.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8     <form action="InputServlet" method="post">
 9         请输入内容:<input type="text" name="info">
10         <input type="submit" value="提交">
11     </form>
12 </body>
13 </html>

InputServlet.java

package org.lxh.servletdemo ;

import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;

public class InputServlet extends HttpServlet{
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException,IOException{
        String info = req.getParameter("info") ;    // 假设参数名称为info
        System.out.println(this + ": " + info); // @Debug
        PrintWriter out = resp.getWriter() ;
        out.println("<html>") ;
        // out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"); // 少了这一句就出现乱码,加上这一句,你不用filter都可以
        out.println("<head><title>MLDNJAVA</title></head>") ;
        out.println("<body>") ;
        out.println("<h1>" + info + "</h1>") ;
        out.println("</body>") ;
        out.println("</html>") ;
        out.close() ;
    }
    public void doPost(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException,IOException{
        this.doGet(req,resp) ;
    }
}

 

---------------------2014-01-09更新-----------------------

 

呃,验证了一下,上面说的是没有问题的。

刚才又试了试,如果你在代码中使用了req.setCharacterEncoding("utf-8"),那么会导致out.println("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>MLDNJAVA</title></head>")失效。这个时候,就必须resp.setContentType("text/html;charset=utf-8")来搭配,否则页面仍然是乱码

 

代码如下:

 1 public class InputServlet extends HttpServlet{
 2     public void doGet(HttpServletRequest req,HttpServletResponse resp)
 3               throws ServletException,IOException{
 4         req.setCharacterEncoding("utf-8"); // 这个必须有,否则response页面的中文会出现乱码
 5         
 6         String info = req.getParameter("info") ;    // 假设参数名称为info
 7         
 8         // System.out.println(this + ": " + info); // @Debug
 9         
10         //resp.setCharacterEncoding("utf-8"); //! 这个与req.setCharacterEncoding("utf-8")搭配解决不了response页面的中文乱码的问题
11         
12         resp.setContentType("text/html;charset=utf-8"); // 这个必须有,否则response页面的中文会出现乱码
13         
14         PrintWriter out = resp.getWriter() ;
15         out.println("<html>") ;
16         out.println("<head><title>MLDNJAVA</title></head>");
17         //out.println("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>MLDNJAVA</title></head>"); //! 这个与req.setCharacterEncoding("utf-8")搭配解决不了response页面的中文乱码问题,通过浏览器查看InputServlet生成的页面的源码,看不到<meta>的内容
18         out.println("<body>") ;
19         out.println("<h1>" + info + ":" + new Date() + "</h1>") ;
20         out.println("</body>") ;
21         out.println("</html>") ;
22         out.close() ;
23     }
24     public void doPost(HttpServletRequest req,HttpServletResponse resp)
25               throws ServletException,IOException{
26         this.doGet(req,resp) ;
27     }
28 }

 

posted @ 2013-07-08 10:43  rldts  阅读(769)  评论(0编辑  收藏  举报