zoukankan      html  css  js  c++  java
  • 解决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  许昌

  • 相关阅读:
    Asp.net弹出浏览器客户端确认对话框代码 Carlwave
    VS 2005 与SQL Server 2005整合优势在哪里?(from csdn.net) Carlwave
    如何让搜索引擎收录我的站点 Carlwave
    超强扩展性的DNNDotNetNuke模块功能分类列表(from 中国DNN) Carlwave
    DotNetNuke命名空间概述 Carlwave
    Most Popular Questions and Answers on ASP.NET Whidbey(from asp.net forums,write by ASP.NET Team) Carlwave
    火箭官方宣告麦蒂缺阵五周 季后赛前景蒙上阴影 Carlwave
    asp.net有效使用缓存(转) Carlwave
    《Business Rules Engine Overview》《业务规则引擎概述》write by Mark Kamoski Carlwave
    中国详细省市县自治区名称列表(含access数据库和sql2000备份数据库) Carlwave
  • 原文地址:https://www.cnblogs.com/ae6623/p/4416668.html
Copyright © 2011-2022 走看看