Engineering Full Stack Apps with Java and JavaScript
When a request is forwarded or included using the RequestDispatcher mechanism, the container may change the URI paths (request uri, context path, servlet path, path info and query string) in the request object to reflect new path.
This can be demonstrated using a simple example.
First, we will create an util class with a method to print the current values of all these URI path attributes and call it from a normal servlet, included servlet and forwarded servlet.
RDUtil
package com.javajee.rdmechanisms;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;public class RDUtil {
public static void printRequestURIDetails(HttpServletRequest request)
throws IOException {System.out.println("request.getRequestURI(): " + request.getRequestURI());
System.out.println("request.getContextPath(): " + request.getContextPath());
System.out.println("request.getServletPath(): " + request.getServletPath());
System.out.println("request.getPathInfo(): " + request.getPathInfo());
System.out.println("request.getQueryString(): " + request.getQueryString());
}
}
Now add the below line to FirstServlet, IncludedServlet and ForwardedServlet created during the RequestDispatcher Mechanism Demo, immediately after the initial printing:
RDUtil.printRequestURIDetails(request);
If you run the final complete code from the RequestDispatcher Mechanism Demo with the above line added immediately after the initial printing, you will get console output as:
FirstServlet.doGet
request.getRequestURI(): /ServletTraining/FirstServlet
request.getContextPath(): /ServletTraining
request.getServletPath(): /FirstServlet
request.getPathInfo(): null
request.getQueryString(): null
IncludedServlet.doGet
request.getRequestURI(): /ServletTraining/FirstServlet
request.getContextPath(): /ServletTraining
request.getServletPath(): /FirstServlet
request.getPathInfo(): null
request.getQueryString(): null
ForwardedServlet.doGet
request.getRequestURI(): /ServletTraining/ForwardedServlet
request.getContextPath(): /ServletTraining
request.getServletPath(): /ForwardedServlet
request.getPathInfo(): null
request.getQueryString(): null
As you can see, included servlet gives the same information as the parent servlet and forwarded servlet gives the details of itself.
To get an included Servlet's path details from within an included servlet, container sets following attributes:
javax.servlet.include.request_uri
javax.servlet.include.context_path
javax.servlet.include.servlet_path
javax.servlet.include.path_info
javax.servlet.include.query_string
To get the original Servlet's path details from within a forwarded servlet, container sets following attributes:
javax.servlet.forward.request_uri
javax.servlet.forward.context_path
javax.servlet.forward.servlet_path
javax.servlet.forward.path_info
javax.servlet.forward.query_string
Questions To Try
Please answer below questions and then try it out to see if your understandings were correct:
I have an initial servlet. It forwards to ForwardedServlet1 and ForwardedServlet1 again forwards to ForwardedServlet2. So what would be the values of above forward attributes inside ForwardedServlet2?
I have an initial servlet. It includes IncludedServlet1 and IncludedServlet1 again includes IncludedServlet2. So what would be the values of above include attributes inside IncludedServlet2?
I have an initial servlet. It forwards to ForwardedServlet and ForwardedServlet includes IncludedServlet. So what would be the values of above include attributes inside IncludedServlet?
I have an initial servlet. It includes IncludedServlet and IncludedServlet forwards to ForwardedServlet. So what would be the values of above forward attributes inside ForwardedServlet?
Comment with your answers below (only after trying).