Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Diff b/w getRequestDispatcher and getNamedRequestDispatcher  RSS feed

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the difference b/w the getRequestDispatcher and getNamedRequestDispatcher. can anyone explain with an example
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!