Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > WEB开发中,中文传参问题的解决办法
【标  题】:WEB开发中,中文传参问题的解决办法
【关键字】:WEB
【来  源】:http://blog.csdn.net/qjyong/archive/2006/11/28/1418982.aspx

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

Your Ad Here

  因为我在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  

eclipse3.2快捷键:【上一篇】
用链接方式安装eclipse插件:【下一篇】
【相关文章】
  • SCA程序设计——Entry Point : 让Web服务发布简单起来
  • 用 JProfiler4 调优 Weblogic 和 Tomcat 的视频(原创)
  • 技巧和诀窍:如何在Web.config中注册用户控件和自定义控件
  • Web服务与网格计算融合
  • 运行部署在Weblogic上的Web应用时,过一段时间程序就提示数据连接断了,重起Weblogic就好了。过一段时间又出...
  • www.dazhel.com 网站正式上线WEB 2.0 AJAX 打折门户
  • Asp.net Web控件失效
  • web.config点滴:更改login控件对密码安全性的要求
  • Java WEB 应用字符编码解决方案
  • .net 2.0 webservice 异步调用在winform中的使用例子。
  • 【随机文章】
  • 广播和多播
  • 关于String类的笔试题及解答
  • 通过Windows Live Alerts订阅博客园首页RSS
  • javascript判断radio是否为空
  • .net精华:C#中对注册表的操作
  • linux 文件系统及操作,用户管理
  • aix 版本显示问题
  • delphi的运行时浅析(学习中)
  • 3DS MAX教程:设定基本材质
  • 画格子类
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.