解决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/p/4416668.html