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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between Filter vs/ RequestDispatcher " Watch "Difference between Filter vs/ RequestDispatcher " New topic
Author

Difference between Filter vs/ RequestDispatcher

vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
Please any one explaine me what is main difference between Filter vs/ RequestDispatcher?
Every help would be appreciated.


Best Regards,<br />Vipin<br />MCA, SCJP5, SCWCD in progress
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Filter is something which you configure so as to pre process the request before they reach the servlet/JSP. It can also be something which modifies the response in addition to request.

RequestDispatcher is what you use to dispatch the request to some other resource. This dispatch can be either include or redirect.

Any other specific questions, please do ask.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hi Sandeep,

Thanks for reply....
Filter follow a specific chane is there any chane in RequestDispatcher?
Damien Gallagher
Ranch Hand

Joined: Dec 16, 2008
Posts: 38
Hi vipin

RequestDispatcher does not follow any chain similar to the FIlter mechanism

With the RequestDispatch you can dispatch a request using 3 mechanisms
ServletRequest.getRequestDispatcher("name.jsp"): can start from context root, doesnt have to
ServletContext.getRequestDispatcher("/name.jsp"): MUST start from context root
ServletContext.getNamedDispatcher("name"): retrieves a servlet based on name from web.xml
None of these 3 methods will throw an exception if the resource cannot be found, they all return null so you should check for that in your code before invoking include or forward


From these 3 methods you can invoke either include or forward methods.

Hope this helps
Thanks
Damien
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hi Damien,

Thanks for your reply i am little bit confuse regarding below code

------------------------------------------------
ServletRequest.getRequestDispatcher("name.jsp"): can start from context root, doesnt have to
-----------------------------------------------
is start from relative to the current servlet only i.e without / or it allow both context root as well as relative to the current servlet ?
Damien Gallagher
Ranch Hand

Joined: Dec 16, 2008
Posts: 38
Hi

With the ServletContext.getRequestDispatcher() it can start from the context root or relative to the current servlet
With the ServletContext version you MUST start from the servlet context.
This could be potential for an exam question. With ServletContext.getRequestDispatcher(), always look for the "\"

Thanks
Damien
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hi Thanks
------------
With the ServletContext.getRequestDispatcher() it can start from the context root or relative to the current servlet
------------------------
i think here ServletRequest instead ServletContext

if i am not wrong or i am getting wrong understand?
Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
Hi,


If you use ServletContext , it should be absolute path say It should start with "/".

If you use ServltetRequest, It should be relative you can use "/" else Container will pick from current directory.


Thanks
Praveen.
Poonam Agarwal
Ranch Hand

Joined: May 12, 2008
Posts: 323
Hi all,

I am little bit confused in the term of Context root and relative path please help me out to get it correctly.
Moreover i am reading 8th chapter of HFSJ, but i haven't read about the Filters mechanisam yet. Is it a part of exam??


Thanks <br />Poonam Agarwal SCWCD 86%
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hi Poonam,
Yes, Filters mechanisam is part of exam.

Relative Path: This path doesn't start with slash(/).it's based upon location of the servlet .So it's direct call Servlet on that location thats way it's not start with /.


Context Root : These pathes are based upon the context of the application,not the location of the servlet.So it's begin with forward slssh(/).

Hope it would be helpful for you..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between Filter vs/ RequestDispatcher