How include servlet output to jsp file
<p>In my web application I have a main page that contain some information. This page is created by servlet and corresponding jsp file. Almost all other pages in my web application must contain the same information as main page plus some addition information. I don't want dublicate code, so I want to use output of main servlet in other jsp files. Below is a simple example of what I try to accomplish.</p>

<p>This is <strong>web.xml</strong> file:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;servlet1&lt;/servlet-name&gt;
        &lt;servlet-class&gt;app.Servlet1&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;servlet1&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/servlet1&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt; 

    &lt;servlet&gt;
        &lt;servlet-name&gt;servlet2&lt;/servlet-name&gt;
        &lt;servlet-class&gt;app.Servlet2&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;servlet2&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/servlet2&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;     

&lt;/web-app&gt;
</code></pre>

<p>This is <strong>java</strong> files:</p>

<p><strong>servlet1.java</strong></p>

<pre><code>package app;


import java.io.IOException;
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 Servlet1 extends HttpServlet {        

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

        request.setAttribute("servletAttribute", 1);

        RequestDispatcher view = request.getRequestDispatcher("/servlet1.jsp");      
        view.forward(request, response);
    }                                                                               
}
</code></pre>

<p><strong>servlet2.java</strong></p>

<pre><code>package app;


import java.io.IOException;
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 Servlet2 extends HttpServlet {        

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

        request.setAttribute("servletAttribute", 2);

        RequestDispatcher view = request.getRequestDispatcher("/servlet2.jsp");      
        view.forward(request, response);
    }                                                                               
}
</code></pre>

<p>This is <strong>jsp</strong> files:</p>

<p><strong>servlet1.jsp</strong></p>

<pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

servlet1

&lt;%
    Integer servletAttribute = (Integer)request.getAttribute("servletAttribute");                   
    out.print("&lt;br&gt;servletAttribute:" + servletAttribute);
%&gt;   
</code></pre>

<p><strong>servlet2.jsp</strong></p>

<pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;

&lt;jsp:include page="/servlet1" /&gt;

servlet2

&lt;%
    Integer servletAttribute = (Integer)request.getAttribute("servletAttribute");                   
    out.print("&lt;br&gt;servletAttribute:" + servletAttribute);
%&gt;   
</code></pre>

<p>So servlet2.jsp must display output of servlet1. It display it, but it doesn/t display addition information from servlet2. And I get this error in Log file:</p>

<pre><code>org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [servlet2] in context with path [/WebApplication3] threw exception [java.lang.IllegalStateException: Exception occurred when flushing data] with root cause
java.io.IOException: Stream closed
</code></pre>

<p>As I understant this error appears because when servlet2.jsp call "/servlet1" servlet1 sent response to client and servlet2.jsp doesn't have session anymore.</p>

<p>So my question is - How can I fix my code to accomplish what I want? Is it possible to include output of some servlet to some jsp file? If it's possible, is it a good or bad practice?</p>