Jsp include action tag

jsp:include action tag is used to include the content of another resource at the specific position into the current JSP page. Another resource can be servlet, jsp, or HTML file.

 

Syntax:

<jsp:include page="URL of another resource"/>

 

Note: In the case of jsp:include action tag contents are included during request processing.

 

The difference between include directive and include action.

               include directive               include action
  1. In case of include directive contents are included at the translation time.
  2. If the content of the included file is changed then changes will not reflect.
  3. Do not use RequestDispatcher to include the content.
  4. Can’t pass parameters.
  5. Syntax: <%@ include file=”URL of another resource” %>
  1. In the case of jsp:include contents are included at the runtime.
  2. If the content of the included file is changed then changes will reflect.
  3. The jsp:include action includes the contents using RequestDispatcher.
  4. Can pass parameters using jsp:param action tag.
  5. Syntax: <jsp:include page=”URL of another resource” />

 

Example:

welcome.jsp

<html>
    <head>
        <title>include action example</title>
    </head>
    <body> 
        <h3>Hello this is a include action example.</h3>
        <jsp:include page="home.jsp"/>
    </body>
</html>

 

home.jsp

<html>
    <head>
        <title>forward action example</title>
    </head>
    <body>
        <h4>This content is of that resource 
                                on which request is forwarded.</h4>
    </body>
</html>

 

web.xml

<web-app>
 
  <welcome-file-list>
          <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>	
 
</web-app>