Java EE 7 Application Developer Exam: Specifying Filter Invocation Order

Filters for Authorization and Narrowing Results by Search Criteria

Question

You created two filters for your web application by using the @WebFilter annotation, one for authorization and the other for narrowing results by the provided search criteria.

The authorization filter must be invoked first.

How can you specify this?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The correct answer is A. setting the priority attribute of the @WebFilter annotation for each of the filters.

In Java EE 7, filters can be defined and configured using the @WebFilter annotation, which allows you to define a filter class and specify the URL patterns that the filter should apply to. Additionally, the @WebFilter annotation provides several attributes that can be used to configure the behavior of the filter.

One of these attributes is the "priority" attribute, which allows you to specify the order in which filters should be applied. The priority attribute is an integer value, where a lower number means a higher priority. Filters with higher priority are applied before filters with lower priority.

To ensure that the authorization filter is invoked before the filter for narrowing search results, you can set the priority attribute of the authorization filter to a lower number than the priority attribute of the narrowing filter.

For example, if you have two filters:

less
@WebFilter(filterName="authorizationFilter", urlPatterns={"/secure/*"}, priority=1) public class AuthorizationFilter implements Filter { // filter code here } @WebFilter(filterName="narrowingFilter", urlPatterns={"/search/*"}, priority=2) public class NarrowingFilter implements Filter { // filter code here }

In this case, the authorization filter has a priority of 1, while the narrowing filter has a priority of 2. This means that the authorization filter will be applied before the narrowing filter.

Note that it is not necessary to use the @Priority annotation, as this annotation is not specific to filters and may cause unintended consequences if used improperly. Instead, the priority attribute of the @WebFilter annotation should be used to specify the order in which filters should be applied.

Option B (placing the filter mapping elements in the required order in the web.xml deployment descriptor) is not correct because the @WebFilter annotation is used to define filters in Java EE 7, and the web.xml deployment descriptor is not required.

Option C (placing @WebFilterMapping annotations in the required order) is not a valid option as there is no @WebFilterMapping annotation. Instead, the URL patterns for the filter are specified in the @WebFilter annotation.

Option D (specifying the filter precedence order by using the @Priority annotation) is not the recommended way to specify filter order because the @Priority annotation is not specific to filters and may cause unintended consequences if used improperly.