What is the correct order of method calls in the lifecycle of a filter?

  • destroy(), init(), doFilter()
  • doFilter(), init(), destroy()
  • init(), destroy(), doFilter()
  • init(), doFilter(), destroy()
The correct order of method calls in the lifecycle of a filter is init(), doFilter(), destroy(). Filters are initialized first, then the doFilter() method is called for each request, and finally, the destroy() method is called when the filter is removed from service.

How can filters be used to manage cross-cutting concerns in a web application?

  • Filters are applied only to static resources
  • Filters are not suitable for managing cross-cutting concerns
  • Filters are used to manage request and response modifications
  • Filters can only be applied to servlets
Filters are commonly used in web applications to manage cross-cutting concerns by allowing the modification of both request and response objects before they reach the servlet or after the servlet has processed them.

What is the scope of servlet initialization parameters?

  • Application scope
  • Page scope
  • Request scope
  • Session scope
Servlet initialization parameters have application scope, meaning they are accessible throughout the entire application.

How do filters interact with the rest of the servlet processing pipeline?

  • Filters can modify both request and response
  • Filters can modify only the request
  • Filters can modify only the response
  • Filters have no interaction with the servlet processing pipeline
Filters in a servlet container can interact with both the request and response objects in the servlet processing pipeline. This enables them to modify the incoming request before it reaches the servlet and manipulate the outgoing response after the servlet has processed the request.

What mechanism is used to chain multiple filters together in a web application?

  • FilterChain
  • FilterGroup
  • FilterPipeline
  • FilterSequence
The FilterChain mechanism is used to chain multiple filters together in a web application. Filters are executed in the order they are added to the chain, allowing for the sequential processing of the request and response.

How can filters dynamically modify the request and response in a servlet-based application?

  • Using the alter() method
  • Using the doFilter() method
  • Using the modify() method
  • Using the transform() method
Filters can dynamically modify the request and response by implementing the doFilter() method. This method provides access to the request and response objects, allowing for manipulation before and after the request reaches the servlet.

In what scenario would a filter be used instead of a servlet?

  • For authentication and logging
  • For handling HTTP requests
  • For managing session state
  • For processing business logic
Filters are typically used for tasks such as authentication, logging, and modifying the request and response. In scenarios where the primary purpose is not to generate a response, but to perform pre or post-processing, filters are preferred over servlets.

A filter is configured in the web application's __________ file.

  • config.xml
  • filter.xml
  • index.jsp
  • web.xml
A filter is configured in the web application's web.xml file.

The __________ method of a filter is used for initialization.

  • filterInit()
  • init()
  • initFilter()
  • initialize()
The init() method of a filter is used for initialization.

Filters can modify the __________ or __________ objects before they reach a servlet.

  • request, response
  • request, session
  • response, context
  • session, context
Filters can modify the request or response objects before they reach a servlet.