aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question 65 Final mock exam - HFSJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question 65 Final mock exam - HFSJ" Watch "Question 65 Final mock exam - HFSJ" New topic
Author

Question 65 Final mock exam - HFSJ

Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 52

A programmer is creating a filter for a Java EE web application. Given the following code:



What change(s) are necessary to create a valid filter? (Choose all that apply.)

a. No changes are necessary
b. A destroy() method must be added.
c. The doFilter() method's body must be changed.
d. The init() method's signature must be changed.
e. The doFilter() method's arguments must be changed.
f. The doFilter() method's exceptions must be changed.


The answers given are BCDE.

C is correct because


if nothing else, doFilter() must invoke chain.doFilter()


What confuses me is a filter can choose NOT to call chain.doFilter thereby blocking others filters, so why is this correct? Would it generate an error if the body of doFilter did nothing, surely not?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1715
    
  25

Hi Paul,

What confuses me is a filter can choose NOT to call chain.doFilter thereby blocking others filters, so why is this correct? Would it generate an error if the body of doFilter did nothing, surely not?

I understand your confusion, but I will try to convince you that HFSJ is correct.

If you don't call chain.doFilter() your web-application just blocks every request by going quiet. Yes you will be able to compile, deploy and run your web-app, but no request will ever be served by the servlet's behind your filter in your web-app. This doensn't sound like a good thing to do...

What you normally decide in a filter is whether you will allow requests (then do call chain.doFilter()) or disallow requests (by forwarding to an error page or something like that)

Regards,
Frits
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Hi Paul,
I also was puzzled by this answer option and made some test. In fact you are not getting any compilation error, deployment error nor exception at runtime. The only problem is that the server sends back empty response with code 200 OK. So the user has no clue what is going on.

I checked again Servlet 3.0 Specification and API and here are some quotes that are ALMOST saying that you are supposed to do either chain.doFilter or fill out the response:

Specification:
6.2.1 Filter Lifecycle
...
The doFilter method of a filter will typically be implemented following this or
some subset of the following pattern:
1. The method examines the request’s headers.
...
4. The filter may invoke the next entity in the filter chain. The next entity may be
another filter, or if the filter making the invocation is the last filter configured in the
deployment descriptor for this chain, the next entity is the target Web resource. The
invocation of the next entity is effected by calling the doFilter method on the
FilterChain object, and passing in the request and response with which it was
called or passing in wrapped versions it may have created.
The filter chain’s implementation of the doFilter method, provided by the
container, must locate the next entity in the filter chain and invoke its doFilter
method, passing in the appropriate request and response objects.
Alternatively, the filter chain can block the request by not making the call to invoke
the next entity, leaving the filter responsible for filling out the response object.

...


API doFilter method
A typical implementation of this method would follow the following pattern:
1. Examine the request
2. Optionally wrap the request object with a custom implementation to filter content or headers for input filtering
3. Optionally wrap the response object with a custom implementation to filter content or headers for output filtering
4.
- Either invoke the next entity in the chain using the FilterChain object (chain.doFilter()),
- or not pass on the request/response pair to the next entity in the filter chain to block the request processing
5. Directly set headers on the response after invocation of the next entity in the filter chain.

Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 52
Ok thanks guys, that's pretty much what I thought, the body of doFilter can be left blank, but it doesn't make any sense to do so.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question 65 Final mock exam - HFSJ