How does a filter modify the response of a servlet or JSP in the filter chain?
- By modifying the request object
- By modifying the response object
- By replacing the servlet output
- By skipping the filter chain
A filter modifies the response by manipulating the response object in the filter chain. It can modify headers, content, or perform other operations on the response before it reaches the client.
In the context of filter configuration, what is the purpose of the tag in web.xml?
- Associates a filter with a servlet or URL pattern
- Defines filter initialization parameters
- Indicates the filter lifecycle
- Specifies the filter execution order
The tag in web.xml associates a filter with a servlet or URL pattern, specifying where the filter should be applied in the web application.
When multiple filters are defined, in what order are they executed?
- Alphabetical order
- Order of declaration in web.xml
- Order specified in filter-mapping
- Random order
Filters are executed in the order of declaration in the web.xml file. The order can be important when one filter's output is used as input to another filter in the chain.
How can a filter pass control to the next entity in the filter chain?
- chain.continueFiltering(request, response)
- chain.doFilter(request, response)
- filterChain.doFilter(request, response)
- filterChain.passControl(request, response)
To pass control to the next entity in the filter chain, the correct method is chain.doFilter(request, response). It invokes the next filter or the servlet in the chain.
What is the impact of a filter throwing an unchecked exception during the execution of its doFilter method?
- The container catches the exception and stops processing the request.
- The exception is ignored, and processing continues.
- The filter chain continues to the next filter or servlet.
- The response is sent without further processing.
If a filter throws an unchecked exception, the container catches it. However, the impact is that the processing of the request stops, and the response is not sent.
How can a filter be configured to process requests for specific servlets or URL patterns?
- By implementing the processOnly method in the filter.
- By setting the filter-url property in the deployment descriptor.
- By specifying servlet names or URL patterns in the filter's configuration.
- By using the @ProcessFor annotation in the filter code.
A filter can be configured to process requests for specific servlets or URL patterns by specifying servlet names or URL patterns in the filter's configuration in the deployment descriptor.
The __________ method is used to initialize the filter with configuration parameters.
- configure(FilterConfig config)
- init(FilterConfig config)
- initialize(FilterConfig config)
- setup(FilterConfig config)
The init(FilterConfig config) method is used to initialize the filter with configuration parameters.
The __________ method is called by the web container to indicate to a filter that it is being taken out of service.
- close()
- destroy()
- release()
- terminate()
The destroy() method is called by the web container to indicate to a filter that it is being taken out of service.
If a filter needs to perform different actions based on whether the request is an HTTP GET or POST, where should this logic be implemented?
- In the destroy() method
- In the doFilter() method
- In the init() method
- In the service() method
The logic for different actions based on HTTP GET or POST requests should be implemented in the doFilter() method of the filter. This method is responsible for processing the request and can be customized for different scenarios.
How would you ensure a filter only processes requests for a specific servlet?
- Check the servlet name in the configuration
- Set a specific attribute in the request
- Use if conditions in the doFilter() method
- Use the url-pattern in the filter mapping
To ensure a filter processes requests for a specific servlet, configure the filter mapping in the deployment descriptor (web.xml) using the url-pattern element with the appropriate servlet mapping.