Servlet事件
事件基本上是某种事情的发生。更改对象的状态称为事件。
Event classes(事件类)
The event classes are as follows:
- ServletRequestEvent
- ServletContextEvent
- ServletRequestAttributeEvent
- ServletContextAttributeEvent
- HttpSessionEvent
- HttpSessionBindingEvent
Event interfaces(事件接口)
The event interfaces are as follows:
- ServletRequestListener
- ServletRequestAttributeListener
- ServletContextListener
- ServletContextAttributeListener
- HttpSessionListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- HttpSessionActivationListener
HttpSessionEvent事件
更改会话session对象时,会通知HttpSessionEvent。此事件对应的侦听器接口是HttpSessionListener。
我们可以在这个事件中执行一些操作,比如统计总登录用户和当前登录用户,维护登录时间、注销时间等用户详细信息的日志。
HttpSessionListener接口方法
HttpSessionListener接口中声明了两种方法,必须由servlet程序员实现这些方法才能执行某些操作。
public void sessionCreated(HttpSessionEvent e): is invoked when session object is created.
public void sessionDestroyed(ServletContextEvent e): is invoked when session is invalidated.
HttpSessionEvent和HttpSessionListener示例,用于计算总登录用户数和当前登录用户数(代码略)
过滤器
Filters are compontents that you can use and configure to perform some filtering tasks. Filter is used for pre-processing of requests and post-processing of responses. You can have any number of filters for pre-processing of a request and post-processing of a response. Filters are configured in the deployment descriptor of a web application.
过滤器是如何工作的?
- 当请求到达Web容器时,它将检查是否有过滤器具有与请求的URL匹配的URL模式。
- Web容器使用匹配的URL模式找到第一个过滤器,然后执行过滤器的代码。
- 如果另一个过滤器具有匹配的URL模式,则将执行其代码。 这将继续进行,直到没有带有匹配URL模式的过滤器为止。
- 如果没有发生错误,则请求传递到目标servlet。因此我们知道,只有当所有相关的过滤器都成功执行时,请求才会传递到目标servlet。
- The servlet returns the response back to its caller. The last filter that was applied to the request is the first filter applied to the
response.- At last the response will be passed to the Web Container which passes it to the client.
如果有多个过滤器匹配模式,那么过滤器的执行顺序是怎么确定的呢?
过滤器API
过滤器API是ServletAPI的一部分。过滤器接口位于javax.servlet包裹。
为了创建过滤器,我们必须实现过滤器接口。过滤器接口提供了过滤器的以下生命周期方法:
- void init(FilterConfig FilterConfig):由web容器调用,以指示过滤器将其放置到服务中。
- void doFilter(ServletRequest request,ServletResponse response,FilterChain chain):由于客户端请求链的末端资源,因此每次通过链条传递请求/响应对时容器都会调用该容器。
- void destroy():由web容器调用,以向筛选器指示正在停止服务。
过滤链filterchain
FilterChain object is used to invoke the next filter in the chain, or if the calling filter is the last filter in the chain then the rosource at the end of the chain invoked. The resources at the end of Filter chain can either be a target Servlet(in case of request flow) or the Client(in case of response flow) as described in the diagram above.
过滤器案例
In this example we are using Filter to (authenticate/验证)(check correct username and password). Here index.html will ask username and password from the user, MyFilter will validate the password entered by the user, if the user has entered “1234” as password, then he will be forwarded to first servlet else the index.html will be shown again to the user.
This is exactly what we used to do earlier using two servlet classes earlier, one for validation and the other to Welcome the user. Now we will insert a Filter for validating the user.
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//设置请求的编码和响应的编码以及数据格式
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
String password = req.getParameter("password");
if (password.equals("1234")) {
chain.doFilter(req, resp);
} else {
out.println("you have enter a wrong password");
RequestDispatcher requestDispatcher = req.getRequestDispatcher("filterTest.html");
requestDispatcher.include(req, resp);
}
out.println("do it after servlet in filter");
}
过滤器还能干什么?
统计请求访问某个页面的次数