这篇文章主要讲解了“怎么应用JavaEE的过滤器、监听、拦截技术”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么应用JavaEE的过滤器、监听、拦截技术”吧!
JavaWeb三大组件:Servlet,Listener,Filter。监听器就是指在应用程序中监听相关对象状态变化的组件。
指被监听对象。
ServletContext
ServletContextListener
生命周期监听,它有两个方法,出生时调用contextInitialized()
,销毁时调用contextDestroyed()
;
ServletContextAttributeListener
属性监听,它有三个方法,添加属性attributeAdded()
,替换属性attributeReplaced()
,移除属性时attributeRemoved()
。
HttpSession
HttpSessionListener
生命周期监听:它有两个方法,出生时调用sessionCreated()
,销毁时调用sessionDestroyed()
;
HttpSessioniAttributeListener
属性监听:它有三个方法,添加属性attributeAdded()
,替换属性attributeReplaced()
,移除属性attributeRemoved()
。
ServletRequest
ServletRequestListener
生命周期监听:它有两个方法,出生时调用requestInitialized()
,销毁时调用requestDestroyed()
;
ServletRequestAttributeListener
属性监听:它有三个方法,添加属性attributeAdded()
,替换属性attributeReplaced()
,移除属性attributeRemoved()
。
相关监听器
TheContextListener
public class TheContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("初始化:TheContextListener"); ServletContext servletContext = servletContextEvent.getServletContext() ; servletContext.setAttribute("author","cicada"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("销毁:TheContextListener"); } }
TheRequestListener
public class TheRequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent servletRequestEvent) { System.out.println("初始化:TheRequestListener"); } @Override public void requestInitialized(ServletRequestEvent servletRequestEvent) { System.out.println("销毁:TheRequestListener"); } }
TheSessionListener
public class TheSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("初始化:TheSessionListener"); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("销毁:TheSessionListener"); } }
RequestAttributeListener
public class RequestAttributeListener implements ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent evt) { System.out.println("Request添加属性:"+evt.getName()+";"+evt.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent evt) { System.out.println("Request移除属性:"+evt.getName()+";"+evt.getValue()); } @Override public void attributeReplaced(ServletRequestAttributeEvent evt) { System.out.println("Request替换属性:"+evt.getName()+";"+evt.getValue()); } }
web.xml配置文件
<!-- 监听器相关配置 --> <listener> <listener-class>com.node05.servlet.listener.TheContextListener</listener-class> </listener> <listener> <listener-class>com.node05.servlet.listener.TheSessionListener</listener-class> </listener> <listener> <listener-class>com.node05.servlet.listener.TheRequestListener</listener-class> </listener> <listener> <listener-class>com.node05.servlet.listener.RequestAttributeListener</listener-class> </listener> <session-config> <!-- 设置session失效时间为1分钟 --> <session-timeout>1</session-timeout> </session-config>
测试接口
public class ListenerServletImpl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); // 1、获取TheContextListener初始化数据 ServletContext servletContext = this.getServletContext() ; String author = String.valueOf(servletContext.getAttribute("author")) ; System.out.println("TheContextListener Author:"+author); // 2、Request属性设置 request.setAttribute("mood","smile"); request.setAttribute("mood","agitated"); // 3、Session创建,1分钟失效,调用销毁 HttpSession session = request.getSession(true) ; session.setAttribute("casually","casually"); response.getWriter().print("Hello:Listener"); } }
客户端请求Servlet时,先执行相关Filter,如果Filter通过,则继承执行请求的Servlet;如果Filter不通过,则不会执行用户请求的Servlet。过滤器可以动态地拦截请求和响应。
Filter接口定义了三个核心方法。
init()
应用程序启动时,服务器实例化Filter对象,并调用其init方法,读取web.xml配置,完成对象的初始化加载。
doFilter()
实际的过滤操作,请求达到服务器时,Servlet容器将先调用过滤器的doFilter方法。
destroy()
容器在销毁过滤器前调用该方法,释放过滤器占用的资源。
编写过滤器
public class ThePrintLogFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { String myName = filterConfig.getInitParameter("myName") ; System.out.println("myName:"+myName); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest ; HttpServletResponse response = (HttpServletResponse)servletResponse ; String name = request.getParameter("name") ; if (!name.equals("cicada")){ response.getWriter().print("User Error !"); return ; } chain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { System.out.println("ThePrintLogFilter destroy()"); } }
web.xml配置文件
<!-- 过滤器相关配置 --> <filter> <filter-name>thePrintLogFilter</filter-name> <filter-class>com.node05.servlet.filter.ThePrintLogFilter</filter-class> <init-param> <param-name>myName</param-name> <param-value>cicada</param-value> </init-param> </filter> <filter-mapping> <filter-name>thePrintLogFilter</filter-name> <url-pattern>/filterServletImpl</url-pattern> </filter-mapping>
测试接口
public class FilterServletImpl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.getWriter().print("Hello:Filter"); } }
Spring框架中的拦截器Interceptor类似于Servlet中的过滤器Filter,主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。请求转发不执行拦截、过滤;重定向执行拦截和过滤。
感谢各位的阅读,以上就是“怎么应用JavaEE的过滤器、监听、拦截技术”的内容了,经过本文的学习后,相信大家对怎么应用JavaEE的过滤器、监听、拦截技术这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。