过滤器(Filter)
过滤器是在客户端和请求资源之间,起一个过滤的作用,举个例子,比如我们要请求admin文件夹下的index.jsp这个页面,那么我们可以用一个过滤器,判断登录用户是不是管理员
-请求资源可以是servlet,jsp页面,html页面等
-在请求资源的前后执行
-请求和响应可以通过filter修改
过滤器常用在这几个方面:Session管理,权限验证,日志记录,字符编码转换
package servletdemo; 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 MyFilter implements Filter { @Override public void destroy() { // TODO 自动生成的方法存根 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("filter..."); chain.doFilter(request, response); // TODO 自动生成的方法存根 } @Override public void init(FilterConfig arg0) throws ServletException { // TODO 自动生成的方法存根 } }
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>javaeedemo2</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> <filter> <filter-name>MyFilter</filter-name> <filter-class>servletdemo.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
注意:这里ulr-parten是起一个过滤的作用,如果是/*,那么所有的资源请求都要经过这个过滤器,再假如要对admin文件夹下的文件过滤,就可以写成/admin/*
我们新建一个index.jsp页面进行访问,每次刷新页面,过滤器都会起作用
再看一个例子,使用Filter进行登录权限验证(访问admin下面的内容需要登录)
package servletdemo; 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; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import sun.misc.Regexp; public class MyFilter implements Filter { @Override public void destroy() { // TODO 自动生成的方法存根 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest)request; HttpServletResponse res=(HttpServletResponse)response; HttpSession session=req.getSession(); if(session.getAttribute("userlogin")==null) { System.out.println("please login..."); } else { req.getRequestDispatcher("../login.html").forward(request, response); } chain.doFilter(request, response); // TODO 自动生成的方法存根 } @Override public void init(FilterConfig arg0) throws ServletException { // TODO 自动生成的方法存根 } }
监听器(Listener)
监听器用于监听JAVA WEB程序中的事件,例如创建,修改,删除Session,request,context等,并触发相应的事件
下面我们就针对刚才提到的三个共享变量来学习监听器
1、ServletContextListener
package servletdemo; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO 自动生成的方法存根 System.out.println("contextDestroyed......"); } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO 自动生成的方法存根 System.out.println("contextInitialized......"); } }
运行WEB工程,无论是哪个页面首先启动,该监听器都会执行,所以一切全局的操作就可以写在这里
2、ServletContextAttributeListener
package servletdemo; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextAttributeListener { @Override public void attributeAdded(ServletContextAttributeEvent arg0) { // TODO 自动生成的方法存根 System.out.println("attributeAdded......"); } @Override public void attributeRemoved(ServletContextAttributeEvent arg0) { // TODO 自动生成的方法存根 System.out.println("attributeRemoved......"); } @Override public void attributeReplaced(ServletContextAttributeEvent arg0) { // TODO 自动生成的方法存根 System.out.println("attributeReplaced......"); } }
package servletdemo; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TestListenerServlet */ @WebServlet("/TestListenerServlet") public class TestListenerServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestListenerServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ServletContext ctx=this.getServletContext(); ctx.setAttribute("ctx_name", "ctx_value"); //替换 ctx.setAttribute("ctx_name", "ctx_value2"); //删除 ctx.removeAttribute("ctx_name"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
运行servlet,该监听器的事件会执行,效果如下
3、HttpSessionListener
package servletdemo; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent arg0) { // TODO 自动生成的方法存根 System.out.println("sessionCreated......"); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { // TODO 自动生成的方法存根 System.out.println("sessionDestroyed......"); } }
package servletdemo; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class TestListenerServlet */ @WebServlet("/TestListenerServlet") public class TestListenerServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestListenerServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub HttpSession session=request.getSession(); session.setAttribute("session_name", "session_value"); //销毁 session.invalidate(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
运行servlet,该监听器的事件会执行,效果如下
4、HttpSessionAttributeListener
package servletdemo; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class MyListener implements HttpSessionAttributeListener { @Override public void attributeAdded(HttpSessionBindingEvent arg0) { // TODO 自动生成的方法存根 System.out.println("attributeAdded......"); } @Override public void attributeRemoved(HttpSessionBindingEvent arg0) { // TODO 自动生成的方法存根 System.out.println("attributeRemoved......"); } @Override public void attributeReplaced(HttpSessionBindingEvent arg0) { // TODO 自动生成的方法存根 System.out.println("attributeReplaced......"); } }
package servletdemo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class TestListenerServlet */ @WebServlet("/TestListenerServlet") public class TestListenerServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestListenerServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession(); session.setAttribute("session_name", "session_value"); //替换 session.setAttribute("session_name", "session_value2"); //删除 session.invalidate(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
运行servlet,该监听器的事件会执行,效果如下
还有一个HttpRequestListener和HttpRequestAttributeListener,一运行servlet就会创建一个request,所以对应的监听器的add事件就会执行
还有监听器的配置文件代码:
<listener>
<listener-class>servletdemo.MyListener</listener-class>
</listener>