JSP页面之间的中文参数传递问题

         关于中文参数传递问题困扰了我2天了,终于在今天无意中给解决了!把我这两天的搜索,以及个人的思考拿来与大家分享。

     解决方案--:使用java.net.URLEncoder.encode()可以对要传递的中文进行编码 。简单的程序如下:a.jsp

<% @ page contentType="text/html;charset=gb2312"  %>  
< a   href ="ds.jsp?url=<%=java.net.URLEncoder.encode(" 编码的是这里","GB2312")% > ">点
击这里
</ a >  
ds.jsp:
<%
//request.setCharacterEncoding("GBK");
if(request.getParameter("url")!=null)

    str
=request.getParameter("url");
    str
=java.net.URLDecoder.decode(str,"GB2312");
    str
=new String(str.getBytes("ISO-8859-1"));
    out.print(str);

%>

 

解决方案二:用JavaScript 实现UrlEncode和UrlDecode :

< script  language ="javascript" >
/*这里开始时UrlEncode和UrlDecode函数*/
function UrlEncode(str){
    
var ret="";
    
var strSpecial="!"#$%&'()*+,/:;<=>?[]^`{|}~%";
    for(var i=0;i<str.length;i++){
        var chr = str.charAt(i);
        var c=str2asc(chr);
        if(parseInt("0x"+c) > 0x7f){
            ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
        }else{
            if(chr==" "){
                ret+="+";
            }else if(strSpecial.indexOf(chr)!=-1){
                ret+="%"+c.toString(16);
            }else{
                ret+=chr;
            }
        }
    }
    return ret;
}
function UrlDecode(str){
    var ret="";
    for(var i=0;i<str.length;i++){
        var chr = str.charAt(i);
        if(chr == "+"){
            ret+=" ";
        }else if(chr=="%"){
            var asc = str.substring(i+1,i+3);
            if(parseInt("0x"+asc)>0x7f){
                ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
                i+=5;
            }else{
                ret+=asc2str(parseInt("0x"+asc));
                i+=2;
            }
        }else{
            ret+= chr;
        }
    }
    return ret;
}
</script> 
配置完后,在加上了这个过滤器类就在action中就可以直接从form中接收gb2312编码的数据了,返回时自然也是gb2312了。但是这个好像需要servlet 2.2以上的容器。
解决方案四:
(1)推荐JSP和HTML的编码都设置为UTF-8;
(2)对于Tomcat4.x,为WEB程序编写一个监控所有URL请求的过滤器Servlet,在doFilter方法中对request参数执行request.setCharacterEncoding("UTF-8");,则在WEB程序中将不受中文问题的困扰,无论你采用什么样的WEB框架。 如方案三
3)对于Tomcat5.x,在上述步骤的基础上,只要修改安装目录中的config/server.xml,添加参数URIEncoding,将其设置为UTF-8。
按照上面的设定之后,在JSP或Servlet中取GET或POST过来的数据只需要如下一行常规的代码就可以了:String p1=request.getParameter("p1"); 
4) 对于STRUTS,也可以直接使用它在Formbean中存储的表单数据了。
解决方案五:
     a.jsp
<% @ page language="java" pageEncoding="gb2312" %>  
< html:link  page ="/form/gq_play.jsp"  paramId ="aa"  paramName ="list"  paramProperty ="gq_adress"  target ="_blank" >
试听
</ html:link >  

gq_play.jsp

<% @ page language="java" pageEncoding="gb2312" %>  
< head >
< html:base  />
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312" >    
</ head >
<% String  aa=new String(request.getParameter("aa").getBytes("ISO-8859-1"),"UTF-8");   
 out.println(aa);
%>

显示结果:D:/mymusic/饶天亮 - 做你的爱人.mp3      说明我的结果成功了。但当我将UTF-8 转换成gb2312确仍然是乱码。其中原由还不明白,不知哪为高人可以指点一二?

解决方案三:当用Request对象获取客户提交的汉字代码的时候,会出现乱码时使用此方案。
首先在web.xml上配置如下:

< filter >
< filter-name > Set Character Encoding </ filter-name >
< filter-class > com.huahang.tj.struts.filters.SetCharacterEncodingFilter </ filter-class >
< init-param >
< param-name > encoding </ param-name >
< param-value > GB2312 </ param-value >
</ init-param >
< init-param >
< param-name > ignore </ param-name >
< param-value > true </ param-value >
</ init-param >
</ filter >
< filter-mapping >
< filter-name > Set Character Encoding </ filter-name >
< servlet-name > action </ servlet-name >
</ filter-mapping >

然后写一个JAVA类,是一个过滤器如下:
package  com.huahang.tj.struts.filters;

import  javax.servlet. * ;

import  java.io.IOException;

 

public   class  SetCharacterEncodingFilter  implements  Filter  {

 

protected String encoding = null;

protected FilterConfig filterConfig = null;

protected boolean ignore = true;

public void destroy() {

this.encoding = null;

this.filterConfig = null;

}


public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain)

throws IOException, ServletException {

if (ignore || (request.getCharacterEncoding() == null)) {

String encoding 
= selectEncoding(request);

if (encoding != null)

request.setCharacterEncoding(encoding);

}


chain.doFilter(request, response);

}


public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value 
= filterConfig.getInitParameter("ignore");
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);
}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值