The method __________ is used to set the length of the content body in the response.

  • setBodyLength()
  • setContentLength()
  • setContentSize()
  • setResponseLength()
The setContentLength() method is used to set the length of the content body in the response.

What is the difference between filter chaining and servlet chaining?

  • Filter chaining is the process of passing a request through multiple filters
  • Filter chaining is the same as servlet chaining
  • Servlet chaining involves calling multiple servlets in a sequence
  • Servlet chaining is the same as filter chaining
Filter chaining involves passing a request through multiple filters in a defined sequence, allowing each filter to process the request. Servlet chaining, on the other hand, involves calling multiple servlets in a sequence.

How do filters interact with different types of servlets and JSP pages?

  • Filters are applied only to static resources
  • Filters can be applied to both servlets and JSP pages
  • Filters can only be applied to servlets
  • Filters cannot be applied to JSP pages
Filters can be applied to both servlets and JSP pages, providing a versatile mechanism for preprocessing and postprocessing of requests and responses in different types of web components.

The __________ object provides the filter with its configuration information.

  • FilterConfig
  • FilterContext
  • ResponseConfig
  • ServletRequest
The FilterConfig object provides the filter with its configuration information. It is passed to the init() method of the filter during initialization.

In the deployment descriptor, the __________ tag is used to specify which requests are passed through a particular filter.

In the deployment descriptor, the tag is used to specify which requests are passed through a particular filter by associating it with a filter name and a URL pattern.

In a scenario where a filter needs to restrict access based on user roles, which object or method is essential for implementing this?

  • checkUserRole()
  • getRoleNames()
  • getUserPrincipal()
  • getUserRoles()
The getUserPrincipal() method is essential for implementing access restrictions based on user roles in a filter. It returns a java.security.Principal object representing the user making the request. The filter can then extract information from this object, such as the user's roles, to make decisions about whether to allow or deny access.

How would a filter log request information without altering the request itself?

  • filterConfig.log(request)
  • getServletContext().log(request)
  • logger.log(request)
  • request.log()
To log request information without altering the request itself, the filter should use the getServletContext().log(request) method. This allows the filter to log information through the servlet context, providing a way to record information without modifying the request or response objects.

What is the purpose of the FilterConfig object in Servlet filters?

  • To configure the order of filter execution
  • To define filter mappings
  • To manage filter lifecycle events
  • To store configuration parameters for a filter
The FilterConfig object is used to store configuration parameters for a filter, allowing developers to set up filter-specific settings.

Which XML element is used to declare a filter in a web application's deployment descriptor?

  • filter
  • filter-mapping
  • servlet-filter
  • web-filter
The element in the web application's deployment descriptor is used to declare a filter, specifying its name and class.

How do you specify the URL patterns to which a filter should apply?

  • Using the
  • Using the
  • Using the
  • Using the
URL patterns to which a filter should apply are specified using the element within the element in the web.xml deployment descriptor.