Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between servlet lifecycle and filter lifecycle

is there any difference between servlet and filter lifecycle?.

like image 213
Dead Programmer Avatar asked Sep 24 '10 10:09

Dead Programmer


People also ask

What is the difference between servlet and filter?

Filter provides functionality which can be “attached” to any web resource. Servlet used for performing action which needs for particular request as user login, get response based on user role, interacts with database for getting data, business logic execution, and more.

What is the servlet lifecycle?

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet. The servlet is initialized by calling the init() method. The servlet calls service() method to process a client's request.

How is servlet life cycle different from JSP life cycle?

Servlets are faster as compared to JSP, as they have a short response time. JSP is slower than Servlets, as the first step in the JSP lifecycle is the conversion of JSP to Java code and then the compilation of the code. Servlets are Java-based codes. JSP are HTML-based codes.

What are the four stages of a servlet life cycle?

Techopedia Explains Servlet Life CycleInstantiation. Initialization. Client request handling.


2 Answers

No, both a servlet and a filter:

  • are instantiated (once) when the context starts
  • the init(..) method is called
  • they handle each request - first it passes through all filters and then reaches the servlet
  • when the context is destroyed (i.e. when your container stops, or your application is undeployed from the manager console), the destroy(..) method is called
like image 73
Bozho Avatar answered Oct 01 '22 01:10

Bozho


So far I was also wondering the differences. I created a web project to observe life-cycle of them. It can be check-outed at

http://dntuan-java-workspace.googlecode.com/svn/trunk/simple-web

Once it is deployed on tomcat, you can observe logs from the console to see that filters are initialized before context is started. Whereas servlet is only initialized when a request is made (e.g. http://localhost:8080/simple-web/servlet/life.jsp)


More information from JSR-000315 JavaTM Servlet 3.0:

2.3.1 Loading and Instantiation

The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request.

6.2.1 Filter Lifecycle

After deployment of the Web application, and before a request causes the container to access a Web resource, the container must locate the list of filters that must be applied to the Web resource as described below. The container must ensure that it has instantiated a filter of the appropriate class for each filter in the list, and called its init(FilterConfig config)method.

like image 26
Tuna Avatar answered Sep 30 '22 23:09

Tuna