Equally, filters may also intercept the response and modify each header and physique of response, therefore you should use it each compress and encrypt the response earlier than sending it to the consumer.
Servlet Filter vs Servlet Listener
Earlier than in search of variations between a Servlet Filter and a Servlet Listener, let’s first see some similarities. This can enable you to grasp their variations higher:
1) Each Listener and Filter are declared in deployment descriptor or net.xml
2) Each are required to implement sure interfaces in order that net container can provide them name again.
Now, let’s examine some key variations between a Filter and a Listener in Servlet framework:
1. Declaration and Lifecycle
<web-app>
<listener>
<listener-class>org.springframework.net.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Filter1</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/servlet1</url-pattern>
</filter-mapping>
</web-app>
Then again, Listeners are created when utility is deployed and Servlet Container sends notification when an fascinating occasion is triggered.
2. Implementation
Each Filters implement javax.servlet.Filter interface, whereas listener can implement totally different interfaces relying upon which type of occasion they need to pay attention. For instance, in case you are on servlet context occasions then your pay attention must implement javax.servlet.ServletContextListener.
Equally, in case you are on listening HTTP session occasions then your listener class must implement javax.servlet.HttpSessionListener.
That is an extension of above level. Each filter must override only one doFilter() technique however you want to override a number of strategies relying upon which occasion you have an interest in.
For instance, in case you are fascinated by when http session is created and destroyed then you want to implement HttpSessionListener and override sessionCreated(HttpSessionEvent se) and sessionDestroyed(HttpSessionEvent se). You’ll obtain notification in these technique when a session was created and when a session is about to be invalidated.
3. Utilization
Filters are usually used for pre-processing of ServletRequest and post-processing of ServletResponse. You need to use filter to implement subtle performance e.g. safety elements like authentication and authorization.
A filter has entry each headers and physique of ServletRequest and ServletResponse.
On the whole, Filters are used to carry out filtering duties comparable to safety, authentication ,auditing of incoming requests from net pages, conversion, logging, compression, encryption and decryption, enter validation and so on.
You possibly can implement HttpSessionListener to pay attention for http session creation and invalidation to implement auto-logout performance in your utility.
4. FilterChain
You possibly can create a series of filters in Servlet primarily based Java net utility the place every filter ahead request to subsequent filter in chain and eventually to the servlet which is suppose to course of the request. The javax.servlet.FilterChain class is used to create filter chain.
Filters use the FilterChain to invoke the subsequent filter within the chain, or if the calling filter is the final filter within the chain, to invoke the useful resource on the finish of the chain.
That is all about distinction between Servlet Filter and Servlet Listener in Java or JEE. Simply do not forget that Filter is used for pre-processing and post-processing of request and response e.g. they’re used to implement safety associated options e.g. authentication and authorization. Spring Safety’s safety filter chain is an effective instance of that.
Then again, Servlet Listener is used to pay attention for life-cycle occasion and act accordingly. For instance, Spring’s ContextLoaderListener implements ServletContextListener and creats Spring managed bean when context is created and clear them up when context is destroyed. It receives callback on contextCreated(ServletContextEvent sce) and contextDestroyed(ServletContextEvent sce) when such occasion occur.
These are simply a few in style instance, there are a lot of subtle utilization of each Filter and Listener in Java EE world e.g. you may also de-register connection pool when context is destroyed to keep away from potential reminiscence leak because of DB connection pool maintaining reference of Servlet courses.
There are additionally listener which may pay attention for HttpSessionEvent e.g. when session is created, when session is timed out, you’ll be able to carry out quite a lot of clean-job with respect to consumer’s session by listening for these occasions.
- Distinction between @Compoent, @Service, and @Controller in Spring? (reply)
- Prime 5 Spring and Hibernate Coaching programs (programs)
- 15 Spring Boot Interview Questions with Solutions (questions)
- Distinction between @Autowired and @Inject in Spring? (reply)
- 20 REST with Spring Interview Questions for Net builders (questions)
- 6 Sources to study Spring Framework in Depth (assets)
- Prime 5 Programs to study Microservices in Spring? (programs)
- Distinction between @RequestParam and @PathVariable in Spring (reply)
- 5 Spring Framework Books For Java Builders (books)
- 5 Spring Boot Options Java developer ought to study (options)
- 10 Superior Spring Boot Programs for Java builders (programs)
- 3 methods to study Core Spring or Spring MVC higher (article)
- Prime 10 Spring Framework Interview Questions (questions)
- 5 Free Spring Framework and Spring Boot Programs (programs)
- 10 Spring MVC Annotations Each Java Dev ought to know (annotations)
Additional Studying
- Java Net Fundamentals
- Head First Servlet and JSP
- Servlt Interview Questions
- Oracle Licensed Java EE Net Developer Certification
- Servlet and JSP books
Thanks for studying this text to date. In case you like this interview query and my clarification then please share along with your good friend and colleagues. If in case you have any query or suggestion then please drop a remark and I will attempt to discover a solution for you.