在地址栏中输入要访问的地址,传值过去,如http://localhost:8080/test/login_login?name=中国&password=china
在action中得到的是乱码,不知道要怎么解码?
方法一:
http://xxx.do?ptname='我是中国人'
String strPtname = request.getParameter("ptname");
strPtname = new String(strPtname.getBytes("ISO-8859-1"), "UTF-8");
方法二:
<%@ page contentType="text/html;charset=gb2312" %>
<%
//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);
}
%>
public String chinatoString(String str)
{
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
function URLencode(sStr)
{
return escape(sStr).
replace(//+/g, '%2B').
replace(//"/g,'%22').
replace(//'/g, '%27').
replace(////g,'%2F');
}
方法三:
如果用jstl的话,可以自己写一个el的function,调用URLEncoder.encode来编码。
IE缺省对URL后面的参数是不编码发送的,但是tomat缺省是按ISO8859-1来进行URL解码,因此才会出现上述错误。好的做法是:
1、在URL参数中确保用UTF-8编码之,方法可以用js函数encodeURI(),或调用自定义的el function;
2、设置server.xml中的Connector熟悉URIEncoding="UTF-8",确保解码格式与编码格式统一;
方法四:
for(var i=0;i<document.links.length;i++){ document.links[i].href=encodeURI(document.links[i].href); }在action中,String s=request.getParameter("s");
s=new String(s.getBytes("iso-8859-1"),"gbk");
以上方法是收聚了一些网友所讲的解决方法 。
把tomcat下 conf 下的 server.xml 里面的标签修改成这样
connectionTimeout="20000" [b]URIEncoding="utf-8"[/b]
redirectPort="8443" />
注意加粗部分!
[code="java"]
先 java.net.URLEncoder.encode
然后 java.net.URLDecoder.decode
[/code]
[code]
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
*
[/code]
1楼正解,其他方式不太好使
方法一:tomcat配置修改
[code="xml"]
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="30"
maxThreads="150"
maxSpareThreads="75"
URIEncoding="GBK">
[/code]
方法二:写一个过滤器
[code="java"]
public class CharacterEncodingFilter implements Filter {
private String edcoding=null;
private FilterConfig filterConfig=null;
private boolean ignore=true;
public void destroy() {
edcoding=null;
filterConfig=null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
if(ignore==true||request.getCharacterEncoding()==null){
String encoding=setCharacterEncoding(request);
if(encoding!=null){
request.setCharacterEncoding(encoding);
}
}
filterChain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
this.edcoding=filterConfig.getInitParameter("encoding");
String value=filterConfig.getInitParameter("ignore");
if(value==null){
this.ignore=true;
}else if(value.equalsIgnoreCase("true")){
this.ignore=true;
}else{
this.ignore=false;
}
}
public String setCharacterEncoding(ServletRequest request){
return this.edcoding;
}
}
[/code]
web.xml中配置:
[code="xml"]
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
[/code]
最好不要传中文,非不得已传中文也不要直接传,写一个转码的方法将中文转成字符串,另一端接受之前在转回来(如base64转码)
兄弟最好用UTF-8的别用GBK。推荐 alyouge 和 OpenMind 的方法。