WEB开发中,中文传参问题的解决办法

来源:互联网 发布:mac字体安装后不显示 编辑:程序博客网 时间:2024/05/17 08:01

  因为我在jsp页面上对字符的编码方式都习惯用UTF-8, 所以本文用UTF-8格式做例子,其它编码方式(如常用的GBK,GB2312),只需要把UTF-8替换为相应编码方式。
(web.xml 和 server.xml 不用特意去改, 原本是怎样就直接用即可).

前台页面参数传递到后台程序的方式主要可分POST 和 GET,现在就从这两种方式讲:

一:POST方式:
用Filter解决: Tomcat 已经做了个例子, 直接拿来用。
在tomcat安装目录下的webapps/jsp-examples/WEB-INF/classes/filters/目录下有个SetCharacterEncodingFilter.java文件,把它复制到自己项目的/WEB-INF/classes/filters/里
它的源码类似以下:
//=========================================================
/*
 * 类名: SetCharacterEncodingFilter.java
 * 作者: qiujy
 * 版本: V1.0
 * 创建日期:2005-12-2
 *
 * Copyright 2006 qiujy All rights reserved.
 */
package com.xumh.common.util;
import javax.servlet.Filter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import java.io.IOException;
/**
 * the filter for code
 */
public class SetCharacterEncodingFilter implements Filter {

 protected FilterConfig config = null;
 protected String encoding = null;
 protected boolean ignore = true;
 public SetCharacterEncodingFilter() {
  super();
 }
 public void destroy() {
  this.encoding = null;
  this.config = null;
 }
 public void doFilter(
  ServletRequest request,
  ServletResponse response,
  FilterChain chain)
  throws ServletException, IOException {
  if (this.ignore || request.getCharacterEncoding() == null) {
   request.setCharacterEncoding(this.encoding);
  }
  chain.doFilter(request, response);
 }
 public void init(FilterConfig config) {
  this.config = config;
  this.encoding = this.config.getInitParameter("encoding");
  String value = this.config.getInitParameter("ignore");
  if (value == null) {
   this.ignore = true;
  } else if (value.equalsIgnoreCase("true")) {
   this.ignore = true;
  } else {
   this.ignore = false;
  }
 }
}

//=========================================================

然后修改/WEB-INF/web.xml, 新增以下的

     <filter>
        <filter-name>SetCharacterEncoding</filter-name>
        <filter-class>filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
      <filter-name>SetCharacterEncoding</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

 
提示: 如果多个项目都想用这个方法, 可以把filters/SetCharacterEncodingFilter.class 复制到{tomcat}/shared/classes里, 再修改{有需要用的项目}/WEB-INF/web.xml, 新增以上的几行即可。

二、GET方式:
修改tomcat安装目录下的conf/server.xml文件,找到 <Connector port=80<Connector port="8080" 这行
在这个标签的中新增一句URIEncoding="UTF-8", 即<Connector port=8080 URIEncoding="UTF-8" ...略...>
其它照原来的,不用动。

这样, 用<form method="get">的话,直接接收就已经是中文了.

最后注意:如果编码为UTF-8且用link方式时,如
<a href="/mytest/test.jsp?str='中文'">test</a>
则接收到的str是正常的"中文"二字,若为
<a href="/mytest/test.jsp?str='中文字'">test</a>
则接收到的str是的"中文?",第三个字乱码。也就是说偶数个中文字是正常的,奇数个中文字最后一个会乱码,我也不知道为什么。(但是编码采用的是GBK或GB2312:则正常)

解决编码为UTF-8且用link方式时,就要先把中文部分先编码, 如
 <%String str = "中文";
    str = java.net.URLEncoder.encode(str,"UTF-8");
%>
<a href="/mytest/test.jsp?str=<%=str%>">test</a>
(注:若用javascrpit也可进行utf-8编码:encodeURIComponent("中文"))

接收到的中文字不管奇数个还是偶数个都正常了。。

这样, 基本上就解決了post和get的乱码问题。

另外. 在修改web.xml 和 server.xml后一定要记得重启Tomcat  

原创粉丝点击