關於中文參數傳遞問題困擾了我2天了,終於在今天無意中給解決了!把我這兩天的搜索,以及個人的思考拿來與大家分享。
解決方案--:使用java.net.URLEncoder.encode()可以對要傳遞的中文進行編碼 。簡單的程序如下:a.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<a href="ds.jsp?url=<%=java.net.URLEncoder.encode("編碼的是這裏","GB2312")%>">點
擊這裏</a>
<a href="ds.jsp?url=<%=java.net.URLEncoder.encode("編碼的是這裏","GB2312")%>">點
擊這裏</a>
<%
//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);
}
%>
//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>
/*這裏開始時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");
(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>
<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);
%>
<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>
<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>
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);
}
}
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);
}
}