Filter是什么?
JavaWeb的一个重要组件,可以对发送到servlet的请求进行拦截过滤,也可以对响应进行拦截
Filter是实现了Filter接口的Java类
Filter需要在web.xml中进行注册,也可以通过注解来注册。
如何创建一个Filter—只需要创建一个类,实现Filter接口即可。
- package afternoon.filter;
-
- import javax.servlet.*;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- /*
- 字符编码集的过滤器
- 过滤拦截什么请求?
- */
- @WebServlet
- public class EncodingFilter implements Filter {
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
-
- HttpServletResponse response = (HttpServletResponse) servletResponse;
- servletRequest.setCharacterEncoding("utf-8");
- servletResponse.setCharacterEncoding("utf-8");
-
- //放行
- filterChain.doFilter(servletRequest, servletResponse);
-
- /*
- 给response添加响应头,text/html,后台向前台输出字符串的时候
- 当后台向前台输出的是json串的格式时,不需要设置这个。
- */
- response.addHeader("content-type","text/html;charset=utf-8");
- }
- }
专门用于对其他对象身上发生的事件或状态改变进行监听和响应的处理
主要监听作用域的对象的改变
监听器监听的是域对象,不是监听对象的某个地址
servlet、filter、listener
- package afternoon.listener;
-
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
-
- /*
- 监听域对象自身的创建和销毁
- */
- public class HelloListener implements HttpSessionListener, ServletRequestListener, ServletContextListener {
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- ServletContextListener.super.contextInitialized(sce);
- }
-
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- ServletContextListener.super.contextDestroyed(sce);
- }
-
- @Override
- public void requestDestroyed(ServletRequestEvent sre) {
- ServletRequestListener.super.requestDestroyed(sre);
- }
-
- @Override
- public void requestInitialized(ServletRequestEvent sre) {
- ServletRequestListener.super.requestInitialized(sre);
- }
-
- @Override
- public void sessionCreated(HttpSessionEvent se) {
- HttpSessionListener.super.sessionCreated(se);
- }
-
- @Override
- public void sessionDestroyed(HttpSessionEvent se) {
- HttpSessionListener.super.sessionDestroyed(se);
- }
- }
- package afternoon.listener;
-
- import javax.servlet.ServletContextAttributeEvent;
- import javax.servlet.ServletContextAttributeListener;
- import javax.servlet.ServletRequestAttributeEvent;
- import javax.servlet.ServletRequestAttributeListener;
- import javax.servlet.annotation.WebListener;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- /*
- 监听域对象中属性的增加和删除
- */
-
- @WebListener
- public class HelloAttributeListener implements ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener {
-
- @Override
- public void attributeAdded(ServletContextAttributeEvent scae) {
- ServletContextAttributeListener.super.attributeAdded(scae);
- }
-
- @Override
- public void attributeRemoved(ServletContextAttributeEvent scae) {
- ServletContextAttributeListener.super.attributeRemoved(scae);
- }
-
- @Override
- public void attributeReplaced(ServletContextAttributeEvent scae) {
- ServletContextAttributeListener.super.attributeReplaced(scae);
- }
-
- @Override
- public void attributeAdded(ServletRequestAttributeEvent srae) {
- ServletRequestAttributeListener.super.attributeAdded(srae);
- }
-
- @Override
- public void attributeRemoved(ServletRequestAttributeEvent srae) {
- ServletRequestAttributeListener.super.attributeRemoved(srae);
- }
-
- @Override
- public void attributeReplaced(ServletRequestAttributeEvent srae) {
- ServletRequestAttributeListener.super.attributeReplaced(srae);
- }
-
- @Override
- public void attributeAdded(HttpSessionBindingEvent se) {
- HttpSessionAttributeListener.super.attributeAdded(se);
- }
-
- @Override
- public void attributeRemoved(HttpSessionBindingEvent se) {
- HttpSessionAttributeListener.super.attributeRemoved(se);
- }
-
- @Override
- public void attributeReplaced(HttpSessionBindingEvent se) {
- HttpSessionAttributeListener.super.attributeReplaced(se);
- }
- }
-
- package afternoon.listener;
-
- import javax.servlet.http.HttpSessionActivationListener;
- import javax.servlet.http.HttpSessionEvent;
-
- /*
- 监听绑定到session中的某个对象的状态的事件监听
- */
- public class HelloSessionListener implements HttpSessionActivationListener {
-
-
- //在钝化之前被调用
- @Override
- public void sessionWillPassivate(HttpSessionEvent se) {
- HttpSessionActivationListener.super.sessionWillPassivate(se);
- }
-
- //在活化之后被调用
- @Override
- public void sessionDidActivate(HttpSessionEvent se) {
- HttpSessionActivationListener.super.sessionDidActivate(se);
- }
-
- /*
- 活化:从磁盘中读取session对象
- 钝化:向磁盘中写入session对象
- */
- }
活化:从磁盘中读取session对象
钝化:向磁盘中写入session对象
@WebListener
注解加上以后才可以监听域对象
注解
@WebListener
XML配置
- <listener>
- <listener-class>afternoon.listener.HelloListener</listener-class>
- </listener>