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" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值