This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion regarding filters (HFSJ)

 
B Misra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having two basic problems with HFSJ Filter's chapter:

1) In HFSJ in page 715 it says we can not just modify the response after call to chain.doFilter() as by when the execution pointer returns the request already have been posted back to the client as it do not wait for the filter method to finish. But in page 723 it is flushing the custom output stream after the call to chain.doFilter() - please explain where I am making a mistake?

2) In page 735 question no. 5 it says
"Filters may be used to create request or response wrappers" - Correct
"Wrappers may be used to create request or response filters." - Incorrect

Why??
i can see the 2nd statement is absurd but why the first one is correct!?


Thanks in advance.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B Misra wrote:1) In HFSJ in page 715 it says we can not just modify the response after call to chain.doFilter() as by when the execution pointer returns the request already have been posted back to the client as it do not wait for the filter method to finish. But in page 723 it is flushing the custom output stream after the call to chain.doFilter() - please explain where I am making a mistake?


Both are correct. In your first query it is talking about a normal response object(HttpServletRespone) so the last component in the chain may flush (committed) the response before the control comes to the filter. In second query, when you have a wrapped object you are not flushing the actual response object but the wrapper object which wraps the actual response object . Did I get your question right?

2) In page 735 question no. 5 it says
"Filters may be used to create request or response wrappers" - Correct
"Wrappers may be used to create request or response filters." - Incorrect

i can see the 2nd statement is absurd but why the first one is correct!?


Because,inside the filter we create wrapper object and passes it to the chain.doFilter() method.

 
B Misra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:
B Misra wrote:1) In HFSJ in page 715 it says we can not just modify the response after call to chain.doFilter() as by when the execution pointer returns the request already have been posted back to the client as it do not wait for the filter method to finish. But in page 723 it is flushing the custom output stream after the call to chain.doFilter() - please explain where I am making a mistake?


Both are correct. In your first query it is talking about a normal response object(HttpServletRespone) so the last component in the chain may flush (committed) the response before the control comes to the filter. In second query, when you have a wrapped object you are not flushing the actual response object but the wrapper object which wraps the actual response object . Did I get your question right?



Thanks for the reply Vijitha.
So in the first case the actual request object will always be committed before the execution returns to filter but in 2nd case writing to the custom response does not commit the actual response object within the custom response, until explicitly flushed?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually when you flushes the custom output stream in the filter (that is when the control returns to the filter) it'll flush the actual output stream (which it wraps) so the container will take care of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic