is there any difference between servlet and filter lifecycle?.
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.
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.
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.
Techopedia Explains Servlet Life CycleInstantiation. Initialization. Client request handling.
No, both a servlet and a filter:
init(..)
method is calleddestroy(..)
method is calledSo 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With