在jsp中用request.getParameter();传值的时候,如果有中文是不是就会出现乱码啊?
我在jsp中用request.getParameter();取值的时候就出现了乱码,但是用session就没问题,是怎么回事啊?
------解决方案--------------------你看你的request.setEncod...编码是否设置成了gbk或者gb2312了,呵呵。还有你输出的页面是否这是了编码为gbk或者2312
------解决方案--------------------在request.getParameter();前面加上这句代码request.setCharacterEncoding("GBK");就好了。
------解决方案--------------------
------解决方案--------------------把所有页面的编码设成统一的能支持中文的就可以了,也可以直接在数据库和web服务器设置支持中文的字符编码
------解决方案--------------------request.setCharacterEncoding("GBK");//这是针对POST的解决方法
如果是GET提交的话
需要用到URLENCODER和URLDECODER两个类.来解决编码问题
------解决方案--------------------给你一个过滤器(不过得xml中配置一下)
package com.dlayer.common;
import
java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter{
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void init(FilterConfig config){
this.filterConfig = config;
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;
}
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);
//response.setCharacterEncoding(encoding);
//response.setContentType("text/html;charset="+encoding);
}
}
chain.doFilter(request,response);
}
public void destroy(){
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request){
return (this.encoding);
}
}
------解决方案--------------------xml
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>com.explink.dao.common.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
------解决方案--------------------