1. This mechanism is useful to include the response of other resources in the current response.
  2. The servlet who got the request initially is responsible to provide total response.
  3. include

  4. Include mechanism is best suitable for including banner information like logo’s ,copy right info etc.

Example:

  1. Create FirstServlet
  2. Create SecondServlet
  3. Configure FirstServlet & SecondServlet in web.xml
  4. Deploy & Run the application

Create FirstServlet

package com.java2learn;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out=response.getWriter();
		out.println("Inside FirstServlet</br>");
		RequestDispatcher rd = request.getRequestDispatcher("/test2");
		rd.include(request, response);
		out.println("FirstServlet Again");
	}
}

Create SecondServlet

package com.java2learn;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("Inside SecondServlet</br>");
	}
}

Configure FirstServlet & SecondServlet in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ServletDemo</display-name>

	<servlet>
		<description></description>
		<display-name>FirstServlet</display-name>
		<servlet-name>FirstServlet</servlet-name>
		<servlet-class>com.java2learn.FirstServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>FirstServlet</servlet-name>
		<url-pattern>/FirstServlet</url-pattern>
	</servlet-mapping>
	<servlet>
		<description></description>
		<display-name>SecondServlet</display-name>
		<servlet-name>SecondServlet</servlet-name>
		<servlet-class>com.java2learn.SecondServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SecondServlet</servlet-name>
		<url-pattern>/test2</url-pattern>
	</servlet-mapping>
</web-app>

Deploy & Run the application

includeOutput

Package Structure:
PackageStructure

Download Project: ServletDemo

Attributes added by Web container while including in request scope.

Attribute Equivalent servlet request method
javax.servlet.forward.request_uri getRequestURI()
javax.servlet.forward.context_path getContextPath()
javax.servlet.forward.servlet_path getServletPath()
javax.servlet.forward.path_info getPathInfo()
javax.servlet.forward.query_string getQueryString()