Filter基本使用
过滤器Filter作为Web的组件,能对所有Web资源(Jsp,Servlet,,静态图片或静态HTML文件等),进行过滤拦截。
1.编写java类实现Filter接口,并实现其doFilter方法。
class FilterDemo implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//过滤器初始化
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
//让过滤器放行
//过滤器相互之间是不清楚它的上一个或下一个是谁,所有的调用都是服务器在处理
//Filter执行顺序可以由<filter_mapping>的顺序决定
filterChain.doFilter(req, resp);
}
@Override
public void destroy() {
//过滤器销毁
}
}
2.web.xml中配置Filter
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
<filter>
<filter-name>FilterDemo</filter-name>
<filter-class>com.filter.FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDemo</filter-name>
<!-- 指过滤哪些资源 /*拦截所有 -->
<url-pattern>/hello</url-pattern>
<!-- 如果请求转发需要如下配置 -->
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
</web-app>
Filter配置细节
1.一个Filter可以有多个<url-pattern>,可以对多个资源进行过滤。
2.Filter默认只是对一次正常的请求(在地址栏中请求一次)做拦截。
3.File可以用反射配置
Filter配置细节
-
REQUEST
默认配置,如果目标资源是通过RequestDispatcher的include()或forward()访问时,那么该过滤器就不会被调用。 -
INCLUDE
如果目标资源是通过RequestDispatcher的include()访问时,那么该过滤器将被调用。除此之外,该过滤器不会被调用。 -
FORWARD
如果目标资源是通过RequestDispatcher的forward()访问时,那么该过滤器将被调用,除此之外,该过滤器不会被调用。 -
ERROR
如果目标资源是通过声明式异常处理机制调用时,那么该过滤器将被调用。除此之外,过滤器不会被调用。
Filter链
-
在web应用中,可以存在多个Filter,这些Filter组合起来即为一个Filter链。
-
web服务器根据Filter在web.xml文件中的注册顺序,决定先调用哪个Filter。
-
当第一个Filter的doFilter()被调用时,web服务器会创建一个代表Filter链的FilterChain对象传递给该方法,web服务器会检查FilterChain对象中是否还有Filter,如果有,则调用第2个Filter,如果没有,则调用目标资源。
Listener
Listener用于监听Web应用中的某些信息(对象或信息的增删改),然后作出相应的响应处理。
1.ServletContextListener
用于监听Web应用的启动与关闭
@WebListener
public class MyContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//应用开启时调用
String switchUserName = sce.getServletContext()
.getInitParameter("switch_user_name");
System.out.println("打开应用的用户:" + switchUserName);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//应用关闭时调用
sce.getServletContext().getRealPath("img");
}
}
在web.xml中配置Listerner
<web-app>
<listener>
<listener-class>com.listener.MyContextListener</listener-class>
</listener>
</web-app>
在Servlet3.0中,Listener可以直接在代码中通过注解来完成,无需在web.xml中配置。
@WebListener //表明以下是个监听器
public class MyContextListener implements ServletContextListener {
}
2.ServletContextAttributeListener
用于监听ServletContext范围(application)内属性的改变。
@WebListener
public class MyListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
String name = scae.getName();
Object value = scae.getValue();
System.out.println("属性名:" + name + "," + "属性值为:" + value);
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
//替换时触发
}
}
public class ReviewServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("user");
//向Application添加属性,即触发Listener
getServletContext().setAttribute("Demo", new User(user, 1));
}
}
输出结果
属性名:Demo,属性值为:User{name='Test', age=1}
3.ServletRequestListener,ServletRequestAttributeListener
ServletRequestListener:用于监听用户请求
ServletRequestAttributeListener:用于监听request范围内属性的变化
@WebListener
public class MyListener implements ServletRequestListener,
ServletRequestAttributeListener {
@Override
public void requestInitialized(ServletRequestEvent sre) {
//用户请求到达时触发
HttpServletRequest servletRequest =
(HttpServletRequest) sre.getServletRequest();
String ip = servletRequest.getRemoteAddr();
String requestURI = servletRequest.getRequestURI();;
System.out.println("IP:" + ip + "请求的资源:" + requestURI);
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
//用户请求结束后触发
}
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
String name = srae.getName();
Object value = srae.getValue();
System.out.println("Name:" + name + "," + value.toString());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}
public class ReviewServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("user");
req.setAttribute("Demo",user);
}
}
4.HttpSessionListener,HttpSessionAttributeListener
HttpSessionListener:用于监听用户session开始与结束
HttpSessionAttributeListener:用于监听HttpSession属性的增删改
@WebListener
public class MyListener implements HttpSessionListener,
HttpSessionAttributeListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
String userName = se.getSession().getAttribute("user").toString();
String id = se.getSession().getId();
System.out.println("数据:" + userName + ",会话ID:" + id);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
}
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
String name = se.getName();
String value = se.getValue().toString();
System.out.println("Name:" + name + ",Value:" + value);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
}
}
public class ReviewServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("user");
req.getSession().setAttribute("user",user);
}
}
网友评论