文章目录 Filter01.java Filter02.java Filter03.java Filter04.java
Filter01.java
package com. aistart. filter ; import javax. servlet. * ;
import javax. servlet. annotation. WebFilter ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
@WebFilter ( "/product/*" )
public class Filter01 implements Filter { @Override public void init ( FilterConfig filterConfig) throws ServletException { System . out. println ( "我初始化了1" ) ; } @Override public void doFilter ( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException , ServletException { System . out. println ( "普通的过滤" ) ; HttpServletRequest request = ( HttpServletRequest ) servletRequest; HttpServletResponse response = ( HttpServletResponse ) servletResponse; System . out. println ( "过滤前" ) ; HttpSession session = request. getSession ( ) ; Object user = session. getAttribute ( "user" ) ; if ( user == null ) { response. getWriter ( ) . println ( "还未登录,请登录" ) ; filterChain. doFilter ( request, response) ; } else { filterChain. doFilter ( request, response) ; } System . out. println ( "过滤后" ) ; } @Override public void destroy ( ) { System . out. println ( "我死了,遗言是...." ) ; }
}
Filter02.java
package com. aistart. filter ; import javax. servlet. * ;
import javax. servlet. annotation. WebFilter ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ; @WebFilter ( "*.jsp" )
public class Filter02 implements Filter { @Override public void init ( FilterConfig filterConfig) throws ServletException { } @Override public void doFilter ( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException , ServletException { System . out. println ( "jsp的过滤生效了" ) ; HttpServletResponse response = ( HttpServletResponse ) servletResponse; HttpServletRequest request = ( HttpServletRequest ) servletRequest; filterChain. doFilter ( request, response) ; } @Override public void destroy ( ) { }
}
Filter03.java
package com. aistart. filter ; import javax. servlet. * ;
import javax. servlet. annotation. WebFilter ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ; @WebFilter ( "/*" )
public class Filter03 implements Filter { @Override public void init ( FilterConfig filterConfig) throws ServletException { } @Override public void doFilter ( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException , ServletException { System . out. println ( "所有的过滤前1" ) ; filterChain. doFilter ( ( HttpServletRequest ) servletRequest, ( HttpServletResponse ) servletResponse) ; System . out. println ( "所有的过滤1后" ) ; } @Override public void destroy ( ) { }
}
Filter04.java
package com. aistart. filter ; import javax. servlet. * ;
import javax. servlet. annotation. WebFilter ;
import javax. servlet. http. HttpServletRequest ;
import javax. servlet. http. HttpServletResponse ;
import java. io. IOException ; @WebFilter ( "/*" )
public class Filter04 implements Filter { @Override public void init ( FilterConfig filterConfig) throws ServletException { } @Override public void doFilter ( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException , ServletException { System . out. println ( "过滤前2" ) ; filterChain. doFilter ( ( HttpServletRequest ) servletRequest, ( HttpServletResponse ) servletResponse) ; System . out. println ( "过滤后2" ) ; } @Override public void destroy ( ) { }
}