解决servlet的out输出流html中文乱码

代码如下。两种方法可以解决。

1.

 response.setContentType("text/html");

       PrintWriter out = response.getWriter();

       out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

       out.println("<HTML>");

       out.println("  <HEAD><TITLE>中文乱码解决</TITLE></HEAD>");

       out.println("  <BODY>");

       out.print("    This is ");

       out.print(this.getClass());

--------------------------------------------------      

code如下 

      /*

      *解决办法,就是将字符串str的格式转换成别的格式,一般是gb1803就可以了。编码格式自己选。

       */

       String str="<center><p>哈哈哈哈 哈哈哈哈哈 哈哈哈哈哈</p></center>";  

       byte[] b = str.getBytes("gb18030");  

       str= new String(b,"iso8859-1");  

       out.println(str); 

    

       out.println("  </BODY>");

       out.println("</HTML>");

       out.flush();

       out.close(); 

 

2.   

-----------------------------------------------------------------------

code如下 

 

      /*

      * 第二招 就是一句话, response.setContentType("text/html;charset=GB2312"); 即可解决问题

      */

       response.setContentType("text/html;charset=GB2312"); 

       PrintWriter out= response.getWriter();

       out.println("<html>");

       out.println("<head>");

       out.println("<title>JasperReports - Web Application Sample</title>");

       out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");

       out.println("</head>");

       out.println("<body bgcolor=\"white\">");

       out.println("<span class=\"bnew\">JasperReports 提示信息:</span>");

       String str = "<center><p><pre style='color:red;border=2px'>报表服务器正在处理其他文件,请您稍后重试。</pe> </p></center>";

       out.println(str); 

       out.println("</body>");

       out.println("</html>");

       out.flush();

       out.close();

3.我将自己的源码全拿出来,你们自己试吧,总能搞定的。搞不定了继续百度。。

      

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

       /*第一招

      response.setContentType("text/html");

       PrintWriter out = response.getWriter();

       out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

       //out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=GB18030\">") ;

       out.println("<HTML>");

       out.println("  <HEAD><TITLE>刘振宇</TITLE></HEAD>");

       out.println("  <BODY>");

       out.print("    This is ");

       out.print(this.getClass());

       String str="<center><p>哈哈哈哈 哈哈哈哈哈 哈哈哈哈哈</p></center>";  

       byte[] b = str.getBytes("gb18030");  

       str= new String(b,"iso8859-1");  

       out.println(str); 

       //String str= "";

       //out.println(", "+str+" using the GET method");

       out.println("  </BODY>");

       out.println("</HTML>");

       out.flush();

       out.close();

        */

        /*第二招

       //response.setContentType("text/html");

       response.setContentType("text/html;charset=GB2312");

 

       PrintWriter out = response.getWriter();

       out.println("<html>");

       out.println("<head>");

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

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

 

       //response.setCharacterEncoding("gb2312");//设置PrintWriter的编码

       //response.setContentType("text/html;charset=utf-8");//可增加Content-Type头字段

       out.println("<title>JasperReports - Web Application Sample</title>");

       out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");

       out.println("</head>");

       out.println("<body bgcolor=\"white\">");

       out.println("<span class=\"bnew\">JasperReports 提示信息:</span>");

        String str = "<center><p><pre style='color:red;border=2px'>报表服务器正在处理其他文件,请您稍后重试。</pe> </p></center>";

       out.println(str); 

       //e.printStackTrace(out);

       out.println("</body>");

       out.println("</html>");

       out.flush();

       out.close();

    }

     */

 

 }

                  2012-911  许昌

转载于:https://www.cnblogs.com/ae6623/archive/2012/09/11/4416668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值