每次Servlet中总会有各种各样的中文请求,为了避免在每个Servlet种都添加request.setCharacterEncoding(“GB2312”)或者response.setCharacterEncoding(“GB2312”),为此可以考虑添加一个中文过滤器,下面给大家演示一个案例:
1、创建一个中文过滤器类(CharacterEncodingFilter):
package com.xiaomo;
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 CharacterEncodingFilter implements Filter {
private FilterConfig config;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//获取Filter初始化参数
String encoding = config.getInitParameter("encoding");
if(encoding != null && !"".equals(encoding)){
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
System.out.println("encoding:"+encoding);
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
}
2、在部署描述文件中配置过滤器:
<!-- 定义一个过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>com.xiaomo.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、自己写一个需要用到中文的Servlet:
package com.xiaomo;
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public class HttpSessions extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html,charset=GB2312");
// response.setCharacterEncoding("GB2312");
HttpSession session = request.getSession();
String heading = null;
Integer accessCount = (Integer) session.getAttribute("accessCount");
if(accessCount == null){
accessCount = new Integer(1);
heading = "欢迎您,首次登陆该页面!";
}else{
heading = "欢迎您,再次登陆该页面!";
accessCount = accessCount +1;
}
session.setAttribute("accessCount", accessCount);
PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>Session Tracking Example</title></head>");
out.println("<body><center>");
out.println("<h4>"+heading+"<a href='httpSessions'>再次访问</a></h4>");
out.println("<table border='0'>");
out.println("<tr bgcolor=\"ffad00\"><td>信息<td>值\n");
String state = session.isNew()?"新会话":"旧会话";
out.println("<tr><td>会话状态:<td>"+state+"\n");
out.println("<tr><td>会话ID:<td>"+session.getId()+"\n");
out.println("<tr><td>创建时间:<td>"+new Date(session.getCreationTime())+"\n");
out.println("<tr><td>最近访问时间:<td>"+new Date(session.getLastAccessedTime())+"\n");
out.println("<tr><td>最大不活动时间:<td>"+session.getMaxInactiveInterval()+"\n");
out.println("<tr><td>Cookie:<td>"+request.getHeader("Cookie")+"\n");
out.println("<tr><td>已访问次数:<td>"+accessCount+"\n");
out.println("</table>");
out.println("</center></body></html>");
}
}
4、在web.xml种配置改HttpSessions ,启动tomcat,查看效果:
大家可以看到,第一个是没有配置中文过滤器,出现了乱码,配置了中文过滤器之后中文出来了,没有乱码现象。
出处:http://blog.csdn.net/cl05300629/article/details/9329257 作者:伫望碧落