Home > Java > javaTutorial > body text

A detailed introduction to Filter, Servlet and Listener in Java

黄舟
Release: 2017-07-30 13:18:36
Original
1404 people have browsed it

This article mainly introduces the learning materials of Filter, Servlet and Listener in detail. It has certain reference value. Interested friends can refer to

Filter, Servlet and Listener in Java. Learning materials, I hope you all like it

1. Filter function

The filter function allows users to change a request and modify a response. Filter is not a servlet, it cannot generate a response, it can preprocess a request before it reaches the servlet, and it can also process the response when leaving the servlet. In other words, filter is actually a "servlet chaining" (servlet chain).

A Filter includes:

1), intercept before the servlet is called;
2), check the servlet request before the servlet is called;
3), based on Need to modify the request header and request data;
4), modify the response header and response data as needed;
5), intercept the servlet after it is called.

The server only calls each time The setFilterConfig method prepares the filter for processing once; the doFilter method is called multiple times to handle different requests. The FilterConfig interface has methods to find the filter name and initialization parameter information. The server can set FilterConfig to empty to indicate that the filter has terminated.
Each filter gets the current request and response from the doFilter() method. In this method, any operation on the request and response can be performed. (Including collecting data, packaging data, etc.). The filter calls chain.doFilter () method hands control to the next filter. A filter ends in the doFilter() method. If a filter wants to stop request processing and gain complete control of the response, it does not need to call the next filter
Example :
First create a new 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; 
 
/** 
 * @author Administrator 
 * 
 */ 
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; 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.Filter#destroy() 
  */ 
 @Override 
 public void destroy() { 
  System.err.println("...filter destroy..."); 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 
  */ 
 @Override 
 public void doFilter(ServletRequest request, ServletResponse response, 
   FilterChain chain) throws IOException, ServletException { 
  System.err.println("...doFilter..."); 
  chain.doFilter(request, response);//看到这没,这只要是传递下一个Filter 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 
  */ 
 @Override 
 public void init(FilterConfig filterConfig) throws ServletException { 
  System.err.println("...init Filter..."); 
  this.filterConfig = filterConfig; 
 } 
 
}
Copy after login

Configure it in 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>/*</url-pattern> 
</filter-mapping>
Copy after login

Start and run
You can see that... init Filter... is first printed when TOMCAT starts, and then you see...doFilter... printed during operation.

2. servlet function

1). What is a Servlet?


Servlet is a Java program that uses the Java Servlet application programming interface (API) and related classes and methods. In addition to the Java Servlet API, servlets can also use Java class packages that extend and add to the API. Servlets run on a Java-enabled Web server or application server and extend the capabilities of that server. Java servlets are to Web servers what Java applets are to Web browsers. Servlets are loaded into the Web server and executed within the Web server, while applets are loaded into the Web browser and executed within the Web browser. The Java Servlet API defines a standard interface between a servlet and a Java-enabled server, making servlets cross-server platform.


Servlets extend the capabilities of a server by creating a framework to provide request and response services on the Web. When a client sends a request to the server, the server can send the request information to the servlet and let the servlet build the response that the server returns to the client. Servlets can be loaded automatically when a Web server is started or when a client requests a service for the first time. After loading, the servlet continues to run until other clients make requests. Servlets have a wide range of functions. For example, Servlet can complete the following functions:


 (1) Create and return a complete HTML page containing dynamic content based on the nature of the customer's request.

 (2) Create a part of an HTML page (HTML fragment) that can be embedded into an existing HTML page.
 (3) Communicate with other server resources (including databases and Java-based applications).
 (4) Use multiple clients to handle connections, receive input from multiple clients, and broadcast the results to multiple clients. For example, a servlet could be a multi-participant game server.
 (5) When data transmission is allowed in single connection mode, open a new connection from the server to the applet on the browser and keep the connection
open. Applets can also initiate connections between the client's browser and the server, allowing the client and server to perform sessions simply and efficiently. Communication can be via custom protocols or standards such as IIOP.
 (6) Use MIME types to filter data for special processing, such as image conversion and server-side includes (SSI).
 (7) Standard routines that provide customized processing to all servers. For example, a servlet can modify how users are authenticated.

2). The life cycle of a Servlet

The life cycle of a Servlet begins when it is loaded into the Web server's memory and ends when the Servlet is terminated or reloaded.

(1) Initialization

Load the Servlet at the following times:
If the autoload option has been configured, it will be automatically loaded when starting the server
After the server starts, When the client makes a request to the Servlet for the first time
After reloading the Servlet, the server creates a Servlet instance and calls the Servlet's init() method. During the initialization phase, Servlet initialization parameters are passed to the Servlet configuration object.

(2) Request processing
For a client request arriving at the server, the server creates a "request" object and a "response" object specific to the request. The server calls the servlet's service() method, which is used to pass the "request" and "response" objects. The service() method obtains the request information from the "request" object, processes the request, and uses the "response" object's methods to pass the response back to the client. The service() method can call other methods to handle the request, such as doGet(), doPost() or other methods.

(3) Termination
When the server no longer needs the Servlet, or reloads a new instance of the Servlet, the server will call the Servlet's destroy() method.

3). Java Servlet API
The Java Servlet Development Tool (JSDK) provides multiple software packages, which are needed when writing Servlets. These include two basic packages for all servlets: javax.servlet and javax.servlet.http. The Java Servlet development tools can be downloaded from Sun's website. The following mainly introduces the HTTP Servlet application programming interface provided by javax.servlet.http.
HTTP Servlet uses an HTML form to send and receive data. To create an HTTP Servlet, extend the HttpServlet class, which is a subclass of GenericServlet that uses specialized methods to handle HTML tables. HTML forms are defined by the

and
tags. Forms typically contain input fields (such as text input fields, check boxes, radio buttons, and select lists) and buttons for submitting data. When submitting information, they also specify which servlet (or other program) the server should execute. The HttpServlet class includes init(), destroy(), service() and other methods. The init() and destroy() methods are inherited.

(1) init() method

In the life cycle of the Servlet, the init() method is only executed once. It is executed when the server loads the servlet. You can configure the server to load a servlet when it is started or when a client accesses the servlet for the first time. No matter how many clients access the servlet, init() is never executed repeatedly.
The default init() method usually meets the requirements, but it can also be overridden with a custom init() method, typically for managing server-side resources. For example, you might write a custom init() to load a GIF image only once, improving the performance of a Servlet returning a GIF image and containing multiple client requests. Another example is initializing a database connection. The default init() method sets the Servlet's initialization parameters and uses its ServletConfig object parameters to start the configuration, so all servlets that override the init() method should call super.init() to ensure that these tasks are still performed. Before calling the service() method, you should ensure that the init() method has been completed.

(2) service() method
The service() method is the core of Servlet. Whenever a client requests an HttpServlet object, the object's service() method is called, and a "request" (ServletRequest) object and a "response" (ServletResponse) object are passed to this method as parameters. The service() method already exists in HttpServlet. The default service function is to call the do function corresponding to the method of the HTTP request. For example, if the HTTP request method is GET, doGet() is called by default. Servlets should override do functionality for HTTP methods supported by the servlet. Because the HttpServlet.service() method checks whether the request method calls the appropriate handler, there is no need to override the service() method. Just override the corresponding do method and you're done.
When a client makes an HTTP POST request through an HTML form, the doPost() method is called. Parameters associated with the POST request are sent from the browser to the server as a separate HTTP request. When you need to modify server-side data, you should use the doPost() method.
When a client makes an HTTP GET request through an HTML form or directly requests a URL, the doGet() method is called. Parameters related to the GET request are added to the end of the URL and sent with this request. The doGet() method should be used when the server-side data will not be modified.
Servlet's response can be of the following types:
An output stream, which the browser interprets according to its content type (such as text/HTML).
An HTTP error response, redirect to another URL, servlet, JSP.

(3) destroy() 方法
  destroy() 方法仅执行一次,即在服务器停止且卸装Servlet 时执行该方法。典型的,将 Servlet 作为服务器进程的一部分来关闭。缺省的 destroy() 方法通常是符合要求的,但也可以覆盖它,典型的是管理服务器端资源。例如,如果 Servlet 在运行时会累计统计数据,则可以编写一个 destroy() 方法,该方法用于在未装入 Servlet 时将统计数字保存在文件中。另一个示例是关闭数据库连接。
当服务器卸装 Servlet 时,将在所有 service() 方法调用完成后,或在指定的时间间隔过后调用 destroy() 方法。一个Servlet 在运行service() 方法时可能会产生其它的线程,因此请确认在调用 destroy() 方法时,这些线程已终止或完成。

(4) GetServletConfig()方法
  GetServletConfig()方法返回一个 ServletConfig 对象,该对象用来返回初始化参数和  ServletContext。ServletContext 接口提供有关servlet 的环境信息。

(5) GetServletInfo()方法
  GetServletInfo()方法是一个可选的方法,它提供有关servlet 的信息,如作者、版本、版权。
  当服务器调用sevlet 的Service()、doGet()和doPost()这三个方法时,均需要 “请求”和“响应”对象作为参数。“请求”对象提供有关请求的信息,而“响应”对象提供了一个将响应信息返回给浏览器的一个通信途径。javax.servlet 软件包中的相关类为ServletResponse和ServletRequest,而javax.servlet.http 软件包中的相关类为HttpServletRequest 和 HttpServletResponse。Servlet 通过这些对象与服务器通信并最终与客户机通信。Servlet 能通过调用“请求”对象的方法获知客户机环境,服务器环境的信息和所有由客户机提供的信息。Servlet 可以调用“响应”对象的方法发送响应,该响应是准备发回客户机的。

例子:
创建一个servlet


/** 
 * 
 */ 
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; 
 
/** 
 * @author Administrator 
 * 
 */ 
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)..."); 
 } 
}
Copy after login

在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>/*</url-pattern><!-- 看到此没有,这个拦截所有路径 --> 
</servlet-mapping>
Copy after login

它的拦截规则:

当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉,剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下:
1.精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先 进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的servlet了。
2.最长路径匹配。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。
3.扩展匹配,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA的url-pattern:*.action
4.如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个default servlet,则容器会将请求丢给default servlet

3、Listener功能

它是基于观察者模式设计的,Listener 的设计对开发 Servlet 应用程序提供了一种快捷的手段,能够方便的从另一个纵向维度控制程序和数据。目前 Servlet 中提供了 5 种两类事件的观察者接口,它们分别是:4 个 EventListeners 类型的,ServletContextAttributeListener、ServletRequestAttributeListener、ServletRequestListener、HttpSessionAttributeListener 和 2 个 LifecycleListeners 类型的,ServletContextListener、HttpSessionListener。如下图所示:

Listener是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可以自动激发一些操作,比如监听在线的用户的数量。当增加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样就可以给在线人数加1。常用的监听接口有以下几个:

ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。
ServletContextListener监听ServletContext。当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

下面我们开发一个具体的例子,这个监听器能够统计在线的人数。在ServletContext初始化和销毁时,在服务器控制台打印对应的信息。当ServletContext里的属性增加、改变、删除时,在服务器控制台打印对应的信息。
要获得以上的功能,监听器必须实现以下3个接口:

HttpSessionListener
ServletContextListener
ServletContextAttributeListener

例子:


/** 
 * 
 */ 
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; 
 
/** 
 * @author Administrator 
 * 
 */ 
public class OnlineUserListener implements HttpSessionListener, 
  ServletContextListener, ServletContextAttributeListener { 
 private long onlineUserCount = 0; 
 
 public long getOnlineUserCount() { 
  return onlineUserCount; 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.ServletContextAttributeListener#attributeAdded(javax.servlet.ServletContextAttributeEvent) 
  */ 
 @Override 
 public void attributeAdded(ServletContextAttributeEvent arg0) { 
 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.ServletContextAttributeListener#attributeRemoved(javax.servlet.ServletContextAttributeEvent) 
  */ 
 @Override 
 public void attributeRemoved(ServletContextAttributeEvent arg0) { 
 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.ServletContextAttributeListener#attributeReplaced(javax.servlet.ServletContextAttributeEvent) 
  */ 
 @Override 
 public void attributeReplaced(ServletContextAttributeEvent attributeEvent) { 
  System.err.println("...attributeReplaced..."); 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) 
  */ 
 @Override 
 public void contextDestroyed(ServletContextEvent arg0) { 
 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) 
  */ 
 @Override 
 public void contextInitialized(ServletContextEvent arg0) { 
 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent) 
  */ 
 @Override 
 public void sessionCreated(HttpSessionEvent httpSessionEvent) { 
  onlineUserCount ++; 
  toUpdateCount(httpSessionEvent); 
 } 
 
 /* (non-Javadoc) 
  * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent) 
  */ 
 @Override 
 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 
  onlineUserCount --; 
  toUpdateCount(httpSessionEvent); 
 } 
 
 private void toUpdateCount(HttpSessionEvent httpSessionEvent){ 
  httpSessionEvent.getSession().setAttribute("onlineUserCount", onlineUserCount); 
 } 
}
Copy after login

Web.xml


<listener> 
 <listener-class>com.ee.listener.OnlineUserListener</listener-class> 
</listener>
Copy after login

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> 
 <h4>你好!</h4> 
 在线人数:<h1><%=request.getSession().getAttribute("onlineUserCount") %></h1> 
</body> 
</html>
Copy after login

The above is the detailed content of A detailed introduction to Filter, Servlet and Listener in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!