Can a filter modify the request and response objects?
- No
- Yes, both request and response objects
- Yes, only request object
- Yes, only response object
Filters have the ability to modify both the request and response objects, allowing them to manipulate data during the processing of a request.
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.
Filters can be used to implement ________, which is a common requirement in web applications.
- authentication
- authorization
- caching
- logging
Filters can be used to implement authorization, which is a common requirement in web applications.
If a filter needs to perform different actions based on the type of HTTP request, which method or object should it use to determine this?
- doFilter()
- getContentType()
- getRequestDispatcher()
- getRequestType()
The doFilter() method in a filter is responsible for performing different actions based on the type of HTTP request. This method is invoked by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain.
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.
What is a session ID in the context of web applications?
- A Java programming language keyword
- A servlet initialization parameter
- A unique identifier for a user's session
- An HTML form element
A session ID in the context of web applications is a unique identifier assigned to each user's session, enabling the server to distinguish between different user sessions.