隨筆 - 147  文章 - 71  trackbacks - 0
          <2011年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(1)

          隨筆分類(146)

          隨筆檔案(147)

          文章分類(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          一、確保jsp頁面中<%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>和<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">編碼為UTF-8;

          二、post方式

          配置字符過濾器

          package com.util.filter;

          import java.io.IOException;

          import javax.servlet.Filter;
          import javax.servlet.FilterChain;
          import javax.servlet.FilterConfig;
          import javax.servlet.ServletException;
          import javax.servlet.ServletRequest;
          import javax.servlet.ServletResponse;

          /**
          * 采用Filter統一設置處理字符集 
          @author songfeng
          @version 1.0
          * Apr 1, 2010
          */

          public class CharsetEncodingFilter implements Filter {

          private String encoding = "UTF-8";

          public void destroy() {
          }


          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
              FilterChain filterChain) 
          throws IOException, ServletException {
             servletRequest.setCharacterEncoding(
          this.encoding);
             filterChain.doFilter(servletRequest, servletResponse);
          }


          public void init(FilterConfig filterConfig) throws ServletException {
             
          this.encoding = filterConfig.getInitParameter("encoding");
          }

          }

          配置web.xml

          <filter>
             
          <filter-name>CharsetEncodingFilter</filter-name>
             
          <filter-class>com.util.filter.CharsetEncodingFilter</filter-class>
             
          <init-param>
              
          <param-name>encoding</param-name>
              
          <param-value>UTF-8</param-value>
             
          </init-param>
          </filter>

          <filter-mapping>
             
          <filter-name>CharsetEncodingFilter</filter-name>
             
          <url-pattern>*.jsp</url-pattern>
          </filter-mapping>

          <filter-mapping>
             
          <filter-name>CharsetEncodingFilter</filter-name>
             
          <url-pattern>/servlet/*</url-pattern>
          </filter-mapping>

          三、get方法

          1、配置tomcat中server.xml

          <Connector port="80" protocol="HTTP/1.1"
                         connectionTimeout="20000"
                         redirectPort="8443" disableUploadTimeout="true" URIEncoding="UTF-8"/>

          2、在要傳遞參數的時候進行轉碼(如不轉碼奇數中文字符最后一個字符亂碼)

          (1)JSP傳值方式:
          跳轉頁:
          <a href=info.jsp?info="<%= java.net.URLEncoder.encode("中文漢字","GBK") %>">跳轉</a>
          接收頁
          <%
               String info_str = new String(request.getParameter("info"),"ISO8859-1");
               out.print(info_str); //輸出接收值
          %>
          (2)JS傳值方式:
          先用encodeURI()進行編碼
          var p = "你好嗎?";
          var url = "aaa.jsp?param=" + encodeURI(p);
          然后在服務器端要解碼
          <%
          String param = request.getParameter("p");
          param = new String(param.getBytes("ISO-8859-1"),"UTF-8");
          %>
          四、ajax亂碼

          在要傳遞中文參數的js中轉碼:encodeURIComponent(“×××”)

          posted on 2011-10-26 20:46 飛翔天使 閱讀(4594) 評論(2)  編輯  收藏 所屬分類: JSP

          FeedBack:
          # re: jsp中UTF-8亂碼解決方案 2013-12-17 09:48 Dooun
          感謝分享  回復  更多評論
            
          # re: jsp中UTF-8亂碼解決方案 2013-12-17 09:55 Dooun
          我有個問題,我看教程里說的是有pageEncoding就不需要charset了不是嗎,這兩個里面不是有一個是優先的嗎?

          還有,不知道樓主是否了解MySQL與JSP的編碼同一問題(不用GBK,用utf-8,因為要處理多國語言)
          游客登錄,方便的話
          QQ:2818429259
          謝謝  回復  更多評論
            
          主站蜘蛛池模板: 东乌珠穆沁旗| 城口县| 汾西县| 三明市| 佳木斯市| 榆树市| 扶风县| 灯塔市| 平塘县| 吉林市| 松原市| 武宣县| 冀州市| 商南县| 丰台区| 壶关县| 大名县| 兴城市| 图片| 宁国市| 盱眙县| 广宗县| 密云县| 德阳市| 南澳县| 曲靖市| 平江县| 方正县| 凤冈县| 友谊县| 襄垣县| 攀枝花市| 齐河县| 育儿| 望江县| 佛冈县| 西宁市| 沙河市| 闵行区| 克东县| 常山县|