File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Filter vs/ RequestDispatcher

 
vipin jain
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please any one explaine me what is main difference between Filter vs/ RequestDispatcher?
Every help would be appreciated.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vipin jain
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sandeep,

Thanks for reply....
Filter follow a specific chane is there any chane in RequestDispatcher?
 
Damien Gallagher
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
vipin jain
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic