jsp页面的中文转换问题一直是某些人头疼的问题。尤其是页面之间的参数传递和对数据库的读写。有些人还专门写了一些工具包。在页面上限制编码格式,在类中运用工具转换。很是麻烦。小弟的项目中用到了过滤器,现将有关过滤器的代码分享下,其实这些代码都是以前用到的,但是我google了一下,写全的不多。所以就下决心写一个全的。
<filter>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
以前的土办法,但是现在才知道,堂堂一个过滤器,写这点有个屁用啊。
开始我们的过滤器:
1 web.xml----添加如下代码。其中一个是字符编码的,一个是session管理的
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>com.guodu.util.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter>
<filter-name>UserFilter</filter-name>
<filter-class>com.guodu.util.UserFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2 CharacterEncodingFilter.java----字符的编码,基本上不改
package com.guodu.util;
import java.io.IOException;
import javax.servlet.*;
/**
* 实现中文转码问题的Filter
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: HuaSun</p>
* @author guoxin
* @version 1.0
*/
public class CharacterEncodingFilter implements javax.servlet.Filter {
protected String encoding = null;
/**
* 指定的FIlter的配置对象
*/
protected FilterConfig filterConfig = null;
/**
* 默认的是否忽略文字编码问题,
*/
protected boolean ignore = true;
/**
* 析构函数
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* 选定字符集编码
*
* @param request The servlet request we are processing
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
// System.out.println("系统中的编码格式" + encoding);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
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;
}
}
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
3 UserFilter.java----session的相关处理。不是创建的处理,是页面转向的处理。
package com.guodu.util;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserFilter implements javax.servlet.Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = request.getSession();
//String path = request.getServletPath();
String uriStr = httpRequest.getServletPath();
//System.out.println( uriStr+"******");
//
if (uriStr.equals("/LoginAction.do") || uriStr.equals("/login.jsp")
|| uriStr.equals("/image.jsp") || uriStr.equals("/go.jsp")
|| uriStr.equals("/admin.jsp")) { //==-1&&httpRequest.getSession().getAttribute("LoginInfo")== null ){
return true;
}
if (((Object) session.getAttribute("LoginInfo")) != null)
return true;
return false;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
//System.out.println("启动 用户过滤器");
if (processPreprocess(httpRequest, httpResponse)) {
chain.doFilter(request, response);
} else { // System.out.println("@@@@@@@@@@@@@");
httpResponse.sendRedirect(httpRequest.getContextPath()
+ "/go.jsp");
// out.print("<script>");
// out.print("<window.open(/"/esmpopr/login.jsp/",/"_parent/")>");
// out.print("</sctipt>");
}
} finally {
}
}
public void destroy() {
}
}
这个类只需要修改processPreprocess方法即可。
说明:
1所有的过滤器都要实现javax.servlet.Filter接口,并且覆盖doFilter方法。
2 <filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
也就是所有的jsp和do请求都走过滤器
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
也就是所有的do请求都走action这个servlet。这个servlet对应着CharacterEncoding这个过滤器。
以上:)