difference between servlet lifecycle and filter lifecycle

前端 未结 2 1170
猫巷女王i
猫巷女王i 2020-12-30 04:41

is there any difference between servlet and filter lifecycle?.

2条回答
  •  悲哀的现实
    2020-12-30 05:18

    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.

提交回复
热议问题