Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

request.getRequestDispatcher() vs getServletContext().getRequestDispatcher()

 
Ranch Hand
Posts: 71
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gurus. Is there a different between both RequestDispatcher?



and



Which one should I use at any point of time? I observe that a new RequestDispatcher instance gets created for every time the method is invoked. Can someone care to explain please?
 
Creator of Enthuware JWS+ V6
Posts: 3348
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there a different between both RequestDispatcher?


There is a small difference:

From ServletRequest
  • method to call: getRequestDispatcher(String path)
  • the path parameter doesn't have to start with a "/"
  • without a "/" it is relative to directory containing the Servlet. It doesn't have to exist as a real directory (because of the logical fiction of the servlet mapping)
  • cannot surpass the web-application


  • From ServletContext
  • method to call: getRequestDispatcher(String path)
  • method to call: getNamedDispatcher(String name) - name should match a servlet-name
  • A servlet (identified with a servlet-name) without a servlet-mapping in the web.xml can be reached with the getNamedDispatcher()
  • the path parameter has to start with a "/", otherwise IllegalStateException
  • can surpass a web-application in the same server (JVM), using getContext() to obtain a foreign context. (note: Tomcat setting required in the servlet.xml: <Context crossContext="true"> )

  • Which one should I use at any point of time?


    You can use either one or a specific one when you want to surpass a web-application or when you want to use a relative path

    I observe that a new RequestDispatcher instance gets created for every time the method is invoked. Can someone care to explain please?


    It is not mandated by the specifications, so we (as programmers) don't know whether a RequestDispatcher object is created everytime and we should actually not care about it . The only thing we know is that the RequestDispatcher is an Interface and that the object implementing the RequestDispatcher interface can be obtained by calling getRequestDispatcher(String path)

    Regards,
    Frits
     
    Allan Cheong
    Ranch Hand
    Posts: 71
    Scala Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Got it. Thanks Frits!
     
    Grow a forest with seedballs and this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
      Bookmark Topic Watch Topic
    • New Topic