#filter、listener
# filter(过滤器)
1.作用
- 用来处理项目中的公共需求。很多请求都包含的业务
- 如:记录日志、过滤敏感词、权限检查
2.开发步骤- 创建一个类,实现过滤器接口Filter- 在web.xml里配置这个类->: 过滤器由服务器自动调用
# 实例
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("查询资费"); } }
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.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 { /** * 该方法是处理公共业务的方法。 * filter相当于是Servlet们的管家, * tomcat在调用servlet前将请求提交给filter,filter有权让请求继续,也有权让请求终止。 * tomcat就是调用doFilter方法让Filter统一处理请求的,调用前它会创建好request和response并传入, * 创建类型:RequestFacade和ResponseFacade */ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { System.out.println("在前面记日志"); chain.doFilter(req, res);//请求继续,向后执行 System.out.println("在后面记日志"); } /** * 关闭tomcat时调用此方法 */ public void destroy() { System.out.println("销毁LogFilter"); } /** * tomcat启动时会自动实例化Filter,然后调用其init()来初始化Filter。 * 调用时会传入config,该对象和Filter是1对1关系,可以给Filter预置参数(web.xml)。 * 该对象和ServletConfig用法完全一样。 */ public void init(FilterConfig filterConfig) throws ServletException { System.out.println("初始化LogFilter"); } }
<?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>jsp6</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可以过滤哪些请求 --> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
# Listener
监听的部分由很多种,这里不多做赘述。
放一个例子参考,其他可百度。
package web; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; public class MyListener implements ServletRequestListener,ServletRequestAttributeListener { //tomcat销毁request前自动调用此方法 public void requestDestroyed(ServletRequestEvent e) { System.out.println("销毁request"); } //tomcat 创建request前自动调用此方法 public void requestInitialized(ServletRequestEvent e) { System.out.println("创建request"); System.out.println(e.getServletRequest()); } public void attributeAdded(ServletRequestAttributeEvent srae) { System.out.println("向request添加值"); } public void attributeRemoved(ServletRequestAttributeEvent srae) { System.out.println("向request输出值"); } public void attributeReplaced(ServletRequestAttributeEvent srae) { System.out.println("向request替换值"); } }
<listener> <listener-class>web.MyListener</listener-class> </listener>
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("attribute", "attribute"); } }