What is the primary purpose of using filters in web applications?

  • To handle HTTP requests and responses
  • To manage session information
  • To perform tasks before or after the request is handled by a servlet
  • To store data in the client's browser
The primary purpose of using filters in web applications is to perform tasks before or after the request is handled by a servlet, allowing for common processing tasks to be centralized.

At which point in the request processing are filters applied in a web application?

  • After the response is generated by the servlet
  • At the end of the servlet lifecycle
  • Before the request reaches the servlet
  • During servlet initialization
Filters are applied before the request reaches the servlet, allowing for pre-processing of the request or post-processing of the response.

Which interface must a class implement to act as a filter in a web application?

  • Filter
  • HttpFilter
  • Servlet
  • WebFilter
To act as a filter in a web application, a class must implement the Filter interface.

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.

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.

To pass control to the next entity in the filter chain, a filter must call the _______ method.

  • continue()
  • doFilter()
  • forward()
  • next()
To pass control to the next entity in the filter chain, a filter must call the doFilter() method, which allows the request to proceed to the next entity in the chain.

Which of the following is true about ServletConfig?

  • It is a configuration object
  • It is a servlet class
  • It is a servlet interface
  • It is used for session management
ServletConfig is an interface that provides a servlet with its initialization parameters. It is used to pass configuration information to the servlet and is not a class or interface related to session management.

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.