Java过滤器-Filter过滤器的使用
过滤是SERVLET2。3规范新有的功能,目前TOMCAT4,WEBLOGIC7都已支持。它能实现很多以前使用不便或很难实现的功能,在产品体系中,我们可以很好地使用SERVLET过滤,使得各个部件可以在保持系统统一权限控制的前提下,来实现各个独自的个性权限系统。
一、使浏览器不缓存页面的过滤器 - import javax.servlet.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- /**
- * 用于的使 Browser 不缓存页面的过滤器
- */
- public class ForceNoCacheFilter implements Filter {
-
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
- {
- ((HttpServletResponse) response).setHeader("Cache-Control","no-cache");
- ((HttpServletResponse) response).setHeader("Pragma","no-cache");
- ((HttpServletResponse) response).setDateHeader ("Expires", -1);
- filterChain.doFilter(request, response);
- }
- public void destroy()
- {
- }
- public void init(FilterConfig filterConfig) throws ServletException
- {
- }
- }
复制代码 二、检测用户是否登陆的过滤器
1.写个过滤器
- mport javax.servlet.Filter;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.FilterChain;
- import java.io.IOException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpServletResponse;
- public class RightFilter
- implements Filter {
- public void init(FilterConfig filterConfig) throws ServletException {
- }
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest) request;
- HttpServletResponse res = (HttpServletResponse) response;
- HttpSession session = req.getSession(true);
- //从session里取的用户名信息
- String username = (String) session.getAttribute("username");
- //判断如果没有取到用户信息,就跳转到登陆页面
- if (username == null || "".equals(username)) {
- //跳转到登陆页面
- res.sendRedirect("http://"+req.getHeader("Host")+"/login.jsp");
- }
- else {
- //已经登陆,继续此次请求
- chain.doFilter(request,response);
- }
- }
- public void destroy() {
- }
- }
复制代码 2.然后在web.xml里配置需要登陆权限验证的JSP文件:
a.如果是某个具体的JSP文件(如a.jsp)需要登陆验证
<web-app>
<filter>
<filter-name>right</filter-name>
<filter-class>com.taihuatalk.taihua.common.RightFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>right</filter-name>
<url-pattern>/a.jsp</url-pattern>
</filter-mapping>
</web-app>
b.如果是某一个目录(如a/目录)整个目录下的文件都需要登陆验证:
<web-app>
<filter>
<filter-name>right</filter-name>
<filter-class>com.taihuatalk.taihua.common.RightFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>right</filter-name>
<url-pattern>/a/*</url-pattern>
</filter-mapping>
</web-app>
三、字符编码的过滤器
- import javax.servlet.*;
- import java.io.IOException;
- /**
- * 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题
- */
- public class CharacterEncodingFilter
- implements Filter
- {
- protected FilterConfig filterConfig = null;
- protected String encoding = "";
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
- {
- if(encoding != null)
- servletRequest.setCharacterEncoding(encoding);
- filterChain.doFilter(servletRequest, servletResponse);
- }
- public void destroy()
- {
- filterConfig = null;
- encoding = null;
- }
- public void init(FilterConfig filterConfig) throws ServletException
- {
- this.filterConfig = filterConfig;
- this.encoding = filterConfig.getInitParameter("encoding");
- }
- }
复制代码 2、在web.xml中做一些设置
<filter>
<filter-name>code</filter-name>
<filter-class>CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>code</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
servlet过滤器的执行顺序就是servlet过滤器在web.xml里面映射文件filter-mapping的顺序
|