URL傳遞中文時的亂碼


URL傳遞中文時的亂碼

一般遇到的亂碼問題可以通過下面的方式來解決:

引用

1.確定JSP頁面頭部是否有:
Java代碼
<%@ page encoding="GBK">
<%@ page contentType="text/html; charset=GBK" %> 

2.用這個轉碼:

//強烈建議頁面要編碼一致。下面的GBK也可改變但一定要和你的頁面的編碼一致
//  String param= new String(request.getParameter("paramName").getBytes("ISO-8859-1"), "GBK");

//寫一個方法,可以將url中傳入的字符不會出現亂碼

Public String translate (String str) {
    String tempStr = "";
    try {
      tempStr = new String(str.getBytes("ISO-8859-1"), "GBK");
      tempStr = tempStr.trim();
    }
    catch (Exception e) {
      System.err.println(e.getMessage());
    }
    return tempStr;
}

 

3.添加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;

public class SetCharacterEncodingFilter implements Filter {
  /**encoding 用來設定編碼格式
     * The default character encoding to set for requests that pass through this filter.
     */
    protected String encoding = null;

    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;


    /**igonre 用來設定是否忽略client端說制定的編碼,默認值爲true。
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;
    // --------------------------------------------------------- Public Methods

    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.encoding = null;
        this.filterConfig = null;
    }


   public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
 throws IOException, ServletException {

        // Conditionally select and set the character encoding to be used
        //已經設置了忽略client端設定的編碼或者request沒有設定編碼的時候,使用配置文件的編碼
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null)
                request.setCharacterEncoding(encoding);
        }

 // Pass control on to the next filter
        chain.doFilter(request, response);

    }

    public void init(FilterConfig filterConfig) throws ServletException {

       this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        //配置文件裏,下面兩種情況:(1)沒有設定ignore參數。
        //(2)ignore參數設置爲yes或true。當這兩種情況的時候,把ignore設置爲true。否則false。
        //通過ignore來達到控制:是否啓用Filter說實現的功能。
        if (value == null)
            this.ignore = true;
        else if (value.equalsIgnoreCase("true"))
            this.ignore = true;
        else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
        else
            this.ignore = false;

    }


      protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }

}


package com.gbs.filter;
web.xml 里加上
<filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>com.gbs.web.util.SetCharacterEncodingFilter</filter-class>

  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
</filter>
- <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

4.如果是通過"a.jsp?param=中文"傳遞參數,則需要:

a.在傳參數之前先把參數進行轉碼:java.net.URLEncoder.encode(param);
取值時用java.net.URLDncoder.decode(param);再轉回中文


b.在你的Tomcat目錄-->conf目錄-->server.xml裏找出這段:
<Connector
port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
<!--在裏邊加上這個參數-->URIEncoding="gb2312" />

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章