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 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值