servlet乱码问题总结

一、Servlet处理客户端提交的中文数据乱码问题
    例子程序:
1)html确定以什么编码将数据发送给服务器:
 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.   <head>  
  4.     <title>MyHtml.html</title>  
  5.       
  6.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  7.     <meta http-equiv="description" content="this is my page">  
  8.     <!-- 对于html页面则是下面语句控制客户端以什么编码打开页面,以及以什么编码将数据发送给服务器 -->  
  9.     <meta http-equiv="content-type" content="text/html; charset=utf-8">  
  10.   </head>  
  11.   <body>  
  12.     This is my HTML page. <br>  
  13.   </body>  
  14. </html>  

 

 
 jsp:
  1. <!-- 这里的pageEncoding属性的值指定了客户端以什么编码打开本页面,以及以什么编码将数据发送给服务器 -->  
  2. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.     <title>My JSP 'encoding.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!-- 
  19.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.     -->  
  21.   
  22.   </head>  
  23.      
  24.   <body>  
  25.      <!-- post提交中文数据 -->  
  26.      <form action="encoding" method="post">  
  27.         UserName:<input type="text" name="username"><br/>  
  28.         <input type="submit" value="submit">  
  29.      </form>   
  30.      <!-- get提交中文数据 -->  
  31.      <form action="encoding" method="get">  
  32.         UserName:<input type="text" name="username"><br/>  
  33.         <input type="submit" value="submit">  
  34.      </form>  
  35.      <!-- url提交中文数据 -->  
  36.      <a href="encoding?username=中国">click me</a>  
  37.   </body>  
  38. </html>  
2)处理客户端三种提交"中国“中文数据的乱码问题处理方式:
  1. package edu.request;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public class EncodingServlet extends HttpServlet {  
  12.   
  13.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  14.         throws ServletException, IOException {  
  15.         System.out.println("--------doPost---------");   
  16.         /** 
  17.          * 乱码原因: 
  18.          * 1.客户端会将encoding.jsp页面用户输入的"中国",以encoding.jsp的编码(这里是utf-8),将中文数据以utf-8进行编码 
  19.          *   产生二进制数据发送给服务器,如在UTF-8字符集中"中国"的二进制数据(98,99) 
  20.          * 2.Servlet处理程序默认会按照ISO8859-1字符集将二进制数据(98,99)编码为String数据,由于ISO8859-1字符集中98,99 
  21.          *   对应的字符不是中国,所以会乱码。 
  22.          * 解决办法: 
  23.          * 1.通过指定Servlet处理request中数据的字符集,让Servlet按照指定的字符集,组合二进制数据即可,即使Servlet按照 
  24.          * utf-8字符集重组获取参数的内容。指定方法如下: 
  25.          *   request.setCharacterEncoding("utf-8"); 
  26.          **/  
  27.         request.setCharacterEncoding("utf-8");  
  28.         String username = request.getParameter("username");  
  29.         System.out.println(username);  
  30.     }  
  31.       
  32.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  33.             throws ServletException, IOException {  
  34.         System.out.println("-------doGet------");  
  35.         /** 
  36.          * 乱码原因: 
  37.          *   乱码原因和上面以post方式提交中文数据的乱码原因相同 
  38.          * 解决办法: 
  39.          *   1.由于 request.setCharacterEncoding("utf-8");只对以post提交的数据有效,对以get和url提交的数据无效, 
  40.          *     其实url也是以get的方式提交数据的。 
  41.          *   2.这里只能采用"先打碎再重组"的方式获取正确的中文数据,由于客户端提交的中文是按照utf-8打碎后,传给服务器, 
  42.          *    而request.getParameter("username")取得的数据是Servlet按照默认的ISO8859-1字符集进行重组的,肯定是乱码的,所以应先按照ISO8859-1将request. 
  43.          *     getParameter("username")的数据打碎为字节,然后在按照utf-8将"客户端输入的按UTF-8打碎的字节"进行重组。 
  44.          *     具体为new String() 
  45.          *      
  46.          **/  
  47.         String username = request.getParameter("username");  
  48.         username = new String(username.getBytes("iso8859-1"),"UTF-8");  
  49.         System.out.println(username);         
  50.     }  
  51.       
  52. }  
按从上往下顺序,先后输入”中国后“,控制台输出:

 

 
二、Servlet处理服务器向客户端响应的中文数据乱码问题
1.服务器端程序向客户端写中文数据--以字节流写入response对象中
 
  1. //字节流输出中文乱码问题  
  2. public class OutputStreamServlet extends HttpServlet {  
  3.      
  4.     @Override  
  5.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  6.             throws ServletException, IOException {  
  7.         //指定浏览器什么码表打开服务器发送的数据  
  8.         //resp.setHeader("Content-Type", "text/html;charset=utf-8");//代码1  
  9.         //resp.setContentType("text/html;charset=utf-8");          //代码2  
  10.           
  11.         OutputStream out = resp.getOutputStream();  
  12.         String data = "中国";  
  13.         /* 
  14.          * 乱码分析: 
  15.          * 将data数据以utf-8码拆分成字节数据,但是浏览器默认是按照gb2312码将response对象中的数据合并为字符数据, 
  16.          * 所以就会出现乱码。 
  17.          * 解决办法: 
  18.          *   让浏览器以utf-8码来合并字节数据,有四种方法 
  19.          *   1.设置response的头信息:如代码1和代码2 
  20.          *   2.向浏览器写模拟头信息,如代码3: 
  21.          *   3.单击浏览器"查看"-->"字符编码"-->"UTF-8" 改变页面显示的字符编码 
  22.          */  
  23.         out.write("<meta http-equiv='content-type' content='text/html;charset=utf-8'>".getBytes()); //代码3  
  24.         out.write(data.getBytes("utf-8"));  
  25.           
  26.     }  
  27.     @Override  
  28.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  29.             throws ServletException, IOException {  
  30.         doGet(req, resp);  
  31.           
  32.     }  
  33. }  
2.服务器端程序向客户端写中文数据--以字符流写入response对象中
 
  1. public class PrintWriterServlet extends HttpServlet {  
  2.   
  3. //字符流输出中文乱码问题  
  4.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  5.             throws ServletException, IOException {  
  6.         //设置response使用的码表,以控制response以什么码表向浏览器写数据。  
  7.         response.setCharacterEncoding("utf-8");  
  8.         //指定浏览器以什么码表打开服务器发送的数据  
  9.         response.setHeader("content-type", "text/html;charset=utf-8");  
  10.           
  11.         //以上两句也可以用一句话代替  
  12.        // response.setContentType("text/html;charset=utf-8");  
  13.        
  14.         String data = "中国";  
  15.         PrintWriter out = response.getWriter();  
  16.         out.write(data);  
  17.           
  18.     }  
  19.   
  20.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  21.             throws ServletException, IOException {  
  22.   
  23.         doGet(request, response);  
  24.     }  
  25. }

 

posted @ 2017-06-22 15:28  walle1314  阅读(254)  评论(0编辑  收藏  举报