使用过滤器可以避免中文乱码的问题出现,我从网上找的一个过滤器类,并且按照大家说的方法进行,可是我在页面上传递一个中文参数到servlet里面,结果是乱码,按照大家说的在Servlet中加入request.setCharacterEncoding("gbk");还是乱码,最后我只好使用
new String(strTmp.getBytes("ISO-8859-1"),"gb2312");的方法进行转换,使用。大家帮我看看,有什么地方不对呀!!!另外谁有可以使用的过滤器给我发到[email protected]最好能有详细的说明。谢谢!!!import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;public class EncodingFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig; // Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} // Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) { try {
request.setCharacterEncoding("GBK");
filterChain.doFilter(request, response); } catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
} // Clean up resources
public void destroy() {
}
}并且在web-inf.xml中加入了
<filter>
<filter-name>encodingfilter</filter-name>
<filter-class>org.util.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
new String(strTmp.getBytes("ISO-8859-1"),"gb2312");的方法进行转换,使用。大家帮我看看,有什么地方不对呀!!!另外谁有可以使用的过滤器给我发到[email protected]最好能有详细的说明。谢谢!!!import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;public class EncodingFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig; // Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} // Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) { try {
request.setCharacterEncoding("GBK");
filterChain.doFilter(request, response); } catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
} // Clean up resources
public void destroy() {
}
}并且在web-inf.xml中加入了
<filter>
<filter-name>encodingfilter</filter-name>
<filter-class>org.util.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
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;
import javax.servlet.UnavailableException;/**
* Example filter that sets the character encoding to be used in parsing the
* incoming request
*/
public class SetCharacterEncodingFilter implements Filter { /**
* Take this filter out of service.
*/
public void destroy() {
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)throws IOException, ServletException { request.setCharacterEncoding("gb2312"); // 传递控制到下一个过滤器
chain.doFilter(request, response);
} public void init(FilterConfig filterConfig) throws ServletException {
}
}
WEB。XML里加上<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>SetCharacterEncodingFilter</filter-class>
</filter>
其他的需要注意的地方:
1、保证文件的编码相同
2、保证web 页面的申明charset 和文件编码一样
就是过滤器能把请求中的字符转换
但是对于url中的参数却不能转换,所以还得单独写个转码的方法
不知道是不是和楼主是一样的问题
除非你在IE 浏览器里把这个设置去掉。所以才是乱码
sevlet-example下有个字符集filter
在Connector里添加“URIEncoding”参数,如下
<Connector ... URIEncoding="GBK" ... />
希望能对其他遇到这些问题的朋友有所帮助:
我的理解是过滤器只是针对表单提交的数据,而url传递的中文只能通过设置
成统一的编码解决,如:
servlet.xml中在Connector里添加“URIEncoding”参数,如下
<Connector ... URIEncoding="UTF-8" ... />
然后再将网页和Servlet里面的编码设置成UTF-8
即网页中是<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Servlet中加入request.setCharacterEncoding("UTF-8");
一、设置过滤器filter地址栏,及get类型的表单
一、<Connector ... URIEncoding="UTF-8" ... />
二、在iternet浏览器里"工具/internet选项/高级选项/总是以utf-8格式发送url勾选"好象这样就可以了<%@ page contentType="text/html;charset=UTF-8" language="java" %>这个好象不是必要的,现在在网吧里,无法试验