// Get the ServletContext object ServletContext context = getServletContext(); // Get the RequestDispatcher for the JSP page RequestDispatcher dispatcher = context.getRequestDispatcher("/index.jsp"); // Forward the request to the JSP page dispatcher.forward(request, response);
// Get the ServletContext object ServletContext context = getServletContext(); // Get the RequestDispatcher for the other servlet RequestDispatcher dispatcher = context.getRequestDispatcher("/otherServlet"); // Include the other servlet's response in the current response dispatcher.include(request, response);In this example, the getRequestDispatcher() method is called to get a RequestDispatcher for the "otherServlet" servlet. The include method is called on the dispatcher object to include the other servlet's response in the current response. This allows the output from the other servlet to be included in the output generated by the current servlet. Both examples use the javax.servlet package library.