Difference between getRequestDispatcher () of ServletContext and

Discussions

General J2EE: Difference between getRequestDispatcher () of ServletContext and

  1. What is the difference between getRequestDispatcher () of ServletContext and ServletRequest

    Please explain in easy to understand language and explain with a simple working example.

    Please explain from latest J2EE version.

    I want to understand when to use ServletContext and when to use ServletRequest's dispatcher call
  2. From Javadoc

    The servletRequest's getRequestDispatcher() can take a relative path while
    ServletContext's getRequestDispatcher() can not(can only take relative to the
    current context's root).

    For example
    with ServletContext both
    -> request.getRequestDispatcher("./jsp/jsppage.jsp") - evaluated relative to the path of the request
    -> request.getRequestDispatcher("/jsp/jsppage.jsp") - evaluated relative to the root
    are all valid
     
    with ServletContext only
    -> context.getRequestDispatcher("/jsp/jsppage.jsp") is valid
    but not context.getRequestDispatcher("./jsp/jsppage.jsp").
    that is it can not evaluate a path other than context root.

    As said in Javadoc servletContext's requestDispatcher must begin with a "/" and is interpreted as relative to the current context root.

    Hope this helps,
    Krishna
  3. Thats great[ Go to top ]

    thanks for the help Sravi