监听器
三种域对象的分类和性质
全局作用域对象共享数据发生变化时触发该监听器
首先定义一个servlet用来响应请求,更新全局作用域的共享数据来触发监听器
public class OneServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext app = req.getServletContext(); app.setAttribute("key",100); app.setAttribute("key",200); app.removeAttribute("key"); } }
监听器使用
@WebListener() public class OneListener implements ServletContextListener, ServletContextAttributeListener { /** * ServletContextListener负责监听 * application被初始化时调用 * @param sce */ @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("全局作用域对象被初始化"); } /** * ServletContextListener负责监听 * application被销毁时调用 * @param sce */ @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("全局作用域对象被销毁"); } /** * ServletContextAttributeListener负责监听 * application添加新的共享数据时触发调用 * @param scae */ @Override public void attributeAdded(ServletContextAttributeEvent scae) { System.out.println("新增共享数据"); } /** * ServletContextAttributeListener负责监听 * application更新共享数据时触发 * @param scae */ @Override public void attributeReplaced(ServletContextAttributeEvent scae) { System.out.println("更新共享数据"); } /** * ServletContextAttributeListener负责监听 * application共享数据被删除时触发 * @param scae */ @Override public void attributeRemoved(ServletContextAttributeEvent scae) { System.out.println("删除共享数据"); } }
web.xml注册监听器
<listener> <listener-class>com.bjpowernode.listener.OneListener</listener-class> </listener> <servlet> <servlet-name>OneServlet</servlet-name> <servlet-class>com.bjpowernode.controller.OneServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OneServlet</servlet-name> <url-pattern>/one.do</url-pattern> </servlet-mapping>
过滤器Filter
应用:过滤器拦截请求
public class OneFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { //从拦截的request上获得密码 String passwd = servletRequest.getParameter("password"); //根据密码判断当前请求的合法性 if("123456".equals(passwd)){ // 合法请求,交给下一个过滤器处理 filterChain.doFilter(servletRequest,servletResponse); }else { // 终止请求 servletResponse.setContentType("text/html;charset=UTF-8"); servletResponse.getWriter().print("密码错误,拒绝进入"); } } }
在web.xml注册
<filter> <filter-name>OneFilter</filter-name> <filter-class>com.bjpowernode.filter.OneFilter</filter-class> </filter> <filter-mapping> <filter-name>OneFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>
FilterChain 理解:
通过两个过滤器对请求进行增强
写一个初始页面index.jsp用来发送请求
<body> <form action="/myWeb/one.do" method="post"> 参数:<input type="text" name="uname"> <input type="submit" value="OneServlet"> </form> <form action="/myWeb/two.do" method="post"> 参数:<input type="text" name="uname"> <input type="submit" value="TwoServlet"> </form> </body>
两个servlet处理请求
public class OneServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String value = req.getParameter("uname"); System.out.println("One 得到的参数:" + value); } }
public class TwoServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String value = req.getParameter("uname"); System.out.println("Two 得到的参数:" + value); } }
两个过滤器链式调用,增强请求后再发送给servlet进行处理
OneFilter
@WebFilter(filterName = "OneFilter") public class OneFilter implements Filter { /** * 对拦截的请求进行处理 * @param req * @param resp * @param chain * @throws ServletException * @throws IOException */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); System.out.println("OneFilter 进行过滤"); chain.doFilter(req, resp); } }
TwoFilter
@WebFilter(filterName = "TwoFilter") public class TwoFilter implements Filter { /** * 对拦截的请求进行处理 * @param req * @param resp * @param chain * @throws ServletException * @throws IOException */ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); System.out.println("TwoFilter 进行过滤"); chain.doFilter(req, resp); } }
过滤器注册先后顺序会影响到其在调用链的位置
<filter> <filter-name>OneFilter</filter-name> <filter-class>com.bjpowernode.filter.OneFilter</filter-class> </filter> <filter-mapping> <filter-name>OneFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter> <filter-name>TwoFilter</filter-name> <filter-class>com.bjpowernode.filter.TwoFilter</filter-class> </filter> <filter-mapping> <filter-name>TwoFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>