Filter、Servlet与Listener在Java的功能各有哪些?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
1、Filter的功能
filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个request到达servlet之前预处理request,也可以在离开 servlet时处理response.换种说法,filter其实是一个”servlet chaining”(servlet 链).
一个Filter包括:
1)、在servlet被调用之前截获;
2)、在servlet被调用之前检查servlet request;
3)、根据需要修改request头和request数据;
4)、根据需要修改response头和response数据;
5)、在servlet被调用之后截获.
服务器每次只调用setFilterConfig方法一次准备filter 的处理;调用doFilter方法多次以处理不同的请求.FilterConfig接口有方法可以找到filter名字及初始化参数信息.服务器可以设置 FilterConfig为空来指明filter已经终结。
每一个filter从doFilter()方法中得到当前的request及response.在这个方法里,可以进行任何的针对request及 response的操作.(包括收集数据,包装数据等).filter调用chain.doFilter()方法把控制权交给下一个filter.一个 filter在doFilter()方法中结束.如果一个filter想停止request处理而获得对response的完全的控制,那它可以不调用下 一个filter
例子:
首先新建一个Filter
package com.ee.filter; 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 { private FilterConfig filterConfig; public FilterConfig getFilterConfig() { System.err.println("...getFilterConfig..."); return filterConfig; } public void setFilterConfig(FilterConfig filterConfig) { System.err.println("...setFilterConfig..."); this.filterConfig = filterConfig; } @Override public void destroy() { System.err.println("...filter destroy..."); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.err.println("...doFilter..."); chain.doFilter(request, response);//看到这没,这只要是传递下一个Filter } @Override public void init(FilterConfig filterConfig) throws ServletException { System.err.println("...init Filter..."); this.filterConfig = filterConfig; } }
在web.xml里配置
<filter> <filter-name>LogFilter</filter-name> <filter-class>com.ee.filter.LogFilter</filter-class> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern> package com.ee.servlet; 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 LogServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.err.println("...doPost(req, resp)..."); } }
在web.xml中的配置:
<servlet> <servlet-name>LogServlet</servlet-name> <servlet-class>com.ee.servlet.LogServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LogServlet</servlet-name> <url-pattern> package com.ee.listener; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class OnlineUserListener implements HttpSessionListener, ServletContextListener, ServletContextAttributeListener { private long onlineUserCount = 0; public long getOnlineUserCount() { return onlineUserCount; } @Override public void attributeAdded(ServletContextAttributeEvent arg0) { } @Override public void attributeRemoved(ServletContextAttributeEvent arg0) { } @Override public void attributeReplaced(ServletContextAttributeEvent attributeEvent) { System.err.println("...attributeReplaced..."); } @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent arg0) { } @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { onlineUserCount ++; toUpdateCount(httpSessionEvent); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { onlineUserCount --; toUpdateCount(httpSessionEvent); } private void toUpdateCount(HttpSessionEvent httpSessionEvent){ httpSessionEvent.getSession().setAttribute("onlineUserCount", onlineUserCount); } }
Web.xml
<listener> <listener-class>com.ee.listener.OnlineUserListener</listener-class> </listener>
JSP页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>主页</title> </head> <body> <h5>你好!</h5> 在线人数:<h2><%=request.getSession().getAttribute("onlineUserCount") %></h2> </body> </html>
看完上述内容,你们掌握Filter、Servlet与Listener在Java的功能各有哪些的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网行业资讯频道,感谢各位的阅读!