• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Diff b/w getRequestDispatcher and getNamedRequestDispatcher

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is the difference b/w the getRequestDispatcher and getNamedRequestDispatcher. can anyone explain with an example
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read their descriptions in the API?
ServletContext.getRequestDispatcher(resource) is used to load a resource from the web application. If you have a JSP called /context/my.jsp and you want to include the JSP, you use getContext().getRequestDispatcher("/my.jsp");
ServletContext.getNamedDispatcher(name) returns a resource by its registered name. You can't access Servlets using the previous manner, since they get mapped to the context, they don't actually exist as files. You need to give them a name (see the API) then you can access them by name.
Think of a RequestDispatcher as loading a file into the container and making it available. To include a JSP as executed code, you need to load the file inside something that will execute it in the container for you. The RequestDispatcher does this. This is like asking "You know that file there, I want to run it in the container please".
Servlets (and some JSPs as well) don't exist on the file path. You need to reach into the container and ask the container to return one by name. A bit like asking "Hey, you know com.javaranch.davo.MyServlet as 'DisplayThingy', can you pass that to me please since I can't access it otherwise".
The RequestDispatcher returned behaves the same in either case, these methods represent ways to get the different resources inside the container.
Hope this helps,
Dave.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic