aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on Filters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt on Filters" Watch "Doubt on Filters" New topic
Author

Doubt on Filters

Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
I am trying to understand "How a response filter works ?".
A wrraper object is passed to chain.doFilter(req,resp);
Could someone explain me the flow of code from here..? :roll:


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Sanjeev BhimaRao
Ranch Hand

Joined: Jul 17, 2006
Posts: 71
Once you pass the wrapped response object from filter1, the next filter (filter2) in the list, filter2 writes to this response object, (by calling getWriter() or whatever the overloaded method is, assuming that is the last filter).

Once filter2 is done with writing, the control returns to the calling filter1, which already has access to the Wrapped response object. The important aspect is that, the final response has not yet been sent to the client. It is still in the container, safely tucked inside the wrapped response object. Filter1, which has this response object, can do whatever it wants to do with it and then send it to the client.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

There is nothing like Request Filter/response Filter...

there are only Filters....that act on request object before it is processed and response before it is sent back to client..

Client->Browser->Server->Filter
Code prior to chain.doFilter()
this code generally contains what we want to do with the request..any manipulations before it is processed


chain.doFilter()..
Filter->Servlet
This calls takes the flow to the actual resource requested(servlet...)
performs the actual logic

Servlet->Filter
And now the flow comes back to the code we might have put after
chain.doFilter() to manipulate the response using any wrapper object..

Filter->Client
Response goes to client

I havent mentioned here multiple filter/Request or Response Wrapper...

Regards
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Ok thanks for the replies..
But..say there is only 1 filter

the req and wrapped_resp are sent to the servlet after calling
chain.doFilter(req,wrapped_response);

now after the servlet is done implementing its logic does the control come back to filter..so that the code written after chain.doFilter could be executed.. ?? and if it does..then what purpose is served by using wraped response instead of direct response object ?? :roll:
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Found this info online-- this solves my doubt to a certain extent
http://edocs.bea.com/wls/docs61/webapp/filters.html

Filtering the Servlet Response Object

You can use filters to post-process the output of a servlet by appending data to the output generated by the servlet. However, in order to capture the output of the servlet, you must create a wrapper for the response. (You cannot use the original response object, because the output buffer of the servlet is automatically flushed and sent to the client when the servlet completes executing and before control is returned to the last filter in the chain.) When you create such a wrapper, WebLogic Server must manipulate an additional copy of the output in memory, which can degrade performance.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
now after the servlet is done implementing its logic does the control come back to filter..so that the code written after chain.doFilter could be executed.. ??


yes....the flow does go through .....


and if it does..then what purpose is served by using wraped response instead of direct response object ??


Your last post ....addresses this point...for the need for a response wrapper
[ January 08, 2007: Message edited by: A Kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on Filters