Servlet,filter,listener,interceptor

Source: Internet
Author: User

Servlet

A servlet is a Java application running on the server side, with platform-and protocol-independent features, and the ability to dynamically generate Web pages that work in the middle tier of client requests and server responses. The main function of a Servlet is to interactively browse and modify data to generate dynamic Web content.

1) 客户端发送请求至服务器端;2) 服务器将请求信息发送至 Servlet;3) Servlet 生成响应内容并将其传给服务器。响应内容动态生成,通常取决于客户端的请求;4) 服务器将响应返回给客户端。在 Web 应用程序中,一个 Servlet 在一个时刻可能被多个用户同时访问。这时 Web 容器将为每个用户创建一个线程来执行 Servlet。如果 Servlet 不涉及共享资源的问题,不必关心多线程问题。但如果 Servlet 需要共享资源,需要保证 Servlet 是线程安全的。
Filter (STRUTS2)

Filter is a reusable code fragment that can be used to transform HTTP requests, responses, and header information. Filter is not like a servlet, it cannot produce a request or response, it simply modifies a request to a resource, or modifies a response from one. The filter in the servlet is a server-side program that implements the Javax.servlet.Filter interface, the main purpose of which is to filter character encoding, make some business logic judgments, and so on. The principle is that as long as you configure the Web. xml file to intercept the client request, it will help you intercept the request, at this time you can set the request or response (requests, Response) unified encoding, simplify the operation, but also to make logical judgments, such as whether the user has logged on, There is no permission to access the page and so on work. It starts with your web app, initializes only once, and then intercepts requests that are destroyed only when your web app is stopped or redeployed.filter can be considered a "variant" of the servlet, it is mainly used for preprocessing the user request, and can also post-process the HttpServletResponse, which is a typical processing chain.it differs from a servlet in that it cannot generate a response directly to the user。 The complete process is that the filter pre-processes the user request, then gives the request to the servlet for processing and generates a response, and finally the filter then processes the server response.


filter has several uses:
Intercept the client's httpservletrequest before HttpServletRequest arrives at the servlet.
You can also modify the HttpServletRequest header and data as needed to check the httpservletrequest.
Intercept HttpServletResponse before HttpServletResponse arrives at the client.
You can also modify the HttpServletResponse header and data as needed to check the httpservletresponse.
There are several types of filter:
User-Authorized Filter:filter is responsible for checking the user request and filtering the user's illegal request according to the request.
Log filter: Records some special user requests in detail.
Filter that is responsible for decoding: Includes request decoding for non-standard encoding.
XSLT filter that can change XML content.
Filter is responsible for intercepting multiple requests or responses, and one request or response can be intercepted by multiple requests.

Listener

Listener: Listener, literally can see listener mainly used for listening. With listener, you can listen to one of the execution actions in the Web server and respond accordingly to its requirements. A popular language is a functional component that automatically executes code when a application,session,request three object is created or is added to a modified delete property. For example, Spring's director listener will instantiate our configured Bean object when the server starts, Hibernate session listener will listen to the session activity and life cycle, responsible for creating, closing session and other activities.
The servlet listener listener, which is a server-side program that implements the Javax.servlet.ServletContextListener interface, is launched with the launch of the Web application, initialized only once, and destroyed as the Web application stops. The main role is to do some initialization of the content to add work, set some basic content, such as some parameters or some fixed object, etc.

Interceptor

Interceptor: Is programming in aspect-oriented, that is, calling a method before your service or a method, or invoking a method after a method, is a Java-based reflection mechanism. For example, a dynamic proxy is a simple implementation of an interceptor that prints out a string (or other business logic) before you invoke the method, or you can print out a string after you invoke the method, even if you do business logic when you throw an exception.

the difference between Serlet and filter:
The 1,servlet process is short, and after the URL is transmitted, it is processed and then returned or turned to a page of its own designation. It is primarily used to control the business before it is processed.
2,filter flow is linear, after the URL, after the inspection, can keep the original process continues to execute downward, by the next filter, Servlet received, etc., and the servlet will not continue to pass down after processing. The filter function can be used to keep the process going as it is, or to dominate the process, and the servlet's functionality is primarily used to dominate the process. Filter can be used for character encoding filtering, detection of user login filtering, prohibit page cache, etc.
3, Servlet,filter are for URLs and so on, and listener is an object-oriented operation, such as the creation of a session, the occurrence of session.setattribute, and doing something when such an event occurs.
Can be used to: spring consolidates struts, injects properties for struts action, implements Web application timed tasks, online demographics, etc.

Interceptor differs from filter:
(1) Not configured in Web. XML, but complete configuration in Struts.xml, with action
(2) Which interceptor can be specified by the action to work before receiving

The difference and connection between filters and interceptors in Struts2:
(1), interceptors are based on the Java reflection mechanism, and filters are based on function callbacks.
(2), the filter relies on the servlet container, while the interceptor is not dependent on the servlet container.
(3), interceptors only work on action requests, while filters can work on almost any request.
(4), the interceptor can access the action context, the object in the value stack, and the filter cannot.
(5), in the life cycle of the action, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized.

Servlet,filter,listener,interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.