Friday, November 18, 2005

Which one is Instantiated first. Servlet or Filter?


Here is what the specification says:

When a web application is deployed into a container, the following steps must be performed, in this order, before the web application begins processing client requests.

• Instantiate an instance of each event listener identified by a element in the deployment descriptor.

• For instantiated listener instances that implement ServletContextListener, call the contextInitialized() method.

• Instantiate an instance of each filter identified by a element in the deployment descriptor and call each filter instance’s init() method.

• Instantiate an instance of each servlet identified by a element that includes a element in the order defined by the load-onstartup element values, and call each servlet instance’s init() method.

So filters are instantiated before Servlets.

Give a scenario where we use Filters.

One way I use filters is for authentication. When an internal controller(servlet) of the web app is requested, a filter checks to make sure that an authentication token is in the session. if so, the request is allowed to proceed normally. Otherwise, the filter forwards to the login mechanism.


0 Comments:

Post a Comment

<< Home