Unit08: 过滤器、监听器
下面这些小案例主要演示的filter的执行时机:
web
package web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddCostServlet extends HttpServlet { @Override protected void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("增加资费"); } }
package web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FindCostServlet extends HttpServlet { @Override protected void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { System.out.println("查询资费"); req.setAttribute("name", "zhangsan"); } }
package web; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class GuolvFilter implements Filter { public void destroy() { System.out.println("销毁GuolvFilter"); } public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("在前面过滤敏感词"); chain.doFilter(req, res); System.out.println("在后面过滤敏感词"); } String words; public void init(FilterConfig cfg) throws ServletException { System.out.println("初始化GuolvFilter"); words = cfg.getInitParameter("words"); System.out.println("words:"+words); } }
package web; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class LogFilter implements Filter { public void destroy() { System.out.println("销毁LogFilter"); } public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("在前面记日志"); //请求继续,向下执行 chain.doFilter(req, res); System.out.println("在后面记日志"); } public void init(FilterConfig arg0) throws ServletException { System.out.println("初始化LogFilter"); } }
package web; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; public class MyListener implements ServletRequestListener, ServletRequestAttributeListener { public void requestDestroyed(ServletRequestEvent e) { //request销毁时调用 System.out.println("销毁request"); System.out.println(e.getServletRequest()); } public void requestInitialized(ServletRequestEvent arg0) { //request创建时调用 System.out.println("创建request"); } public void attributeAdded(ServletRequestAttributeEvent arg0) { System.out.println("向request添加数据"); } public void attributeRemoved(ServletRequestAttributeEvent arg0) { // TODO Auto-generated method stub } public void attributeReplaced(ServletRequestAttributeEvent arg0) { // TODO Auto-generated method stub } }
上面这个小案例,演示监听器。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>jsp5</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>findCost</servlet-name> <servlet-class>web.FindCostServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>findCost</servlet-name> <url-pattern>/findCost</url-pattern> </servlet-mapping> <servlet> <servlet-name>addCost</servlet-name> <servlet-class>web.AddCostServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>addCost</servlet-name> <url-pattern>/addCost</url-pattern> </servlet-mapping> <filter> <filter-name>log</filter-name> <filter-class>web.LogFilter</filter-class> </filter> <filter-mapping> <filter-name>log</filter-name> <!-- 声明Filter对哪些请求路径有效,即 用户在访问哪些路径时Tomcat调用此Filter. --> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 多个Filter其顺序以mapping的配置顺序为准 --> <filter> <filter-name>guolv</filter-name> <filter-class>web.GuolvFilter</filter-class> <init-param> <param-name>words</param-name> <param-value> 靠,TMD,NND </param-value> </init-param> </filter> <filter-mapping> <filter-name>guolv</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 注册监听器,Tomcat会在相应事件发生时, 自动调用此监听器的对应的方法. --> <listener> <listener-class> web.MyListener </listener-class> </listener> </web-app>