These include tag is used to include a JSP file in another page.For example, let's take two JSP pages, but both JSP pages output will be displayed on one browser.These include tag can be written in different types.
[c]
<!– First syntax –>
<s: include value=” Another jsp file name”/>
<!– Second syntax –>
<s:include value=” Another jsp file name”>
<param name=” key1″ value=”value1″/>
<param name=”key2″ value=”value2″/>
</s:include>
<!– Third syntax –>
<s:include value=” Another jsp file name”>
<param name=” key1″ >value1</param>
<param name=”key2″ >value2</param>
</s:include>
[/c]
Example of include tag:
1. In this example, let's create one Register form, so that if user submits the details, the second page should be displayed on the first page.
2. Create the directory structure.
3. Create the view pages and get the input values.
index.jsp
[java]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>
Hello World
</title>
</head>
<body>
<form action="include">
<s:textfield name="name" label="Enter Name" >
<input type="submit" value="submit">
</form>
</body>
</html>
[/java]
4. Create the action class and get the request from browser. Forward the response to result page.
MyActionClass.java
[java]
package com.splessons;
public class MyActionClass
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String execute()
{
return "success";
}
}
[/java]
5. Create the struts 2 configuration file and configure the action class in XML file by using
tag.
struts.xnl
[xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation //DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="include" class="com.splessons.MyActionClass">
<result name="success">
/success.jsp
</result>
</action>
</package>
</struts>
[/xml]
6. Print the result into browser through success page and include the index.jsp page using the tag.
success.jsp
[java]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<font color="green">
<h1>
Welcome to SPLessons
</h1>
</font>
<h2>
Hai
<s:property value="name">
This is include tag example
</h2>
</br>
<script type="text/javascript">
alert("Enter another person name");
</script>
<s:include value="index.jsp">
</body>
</html>
[/java]
web.xml
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>
struts2
</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>
struts2
</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
</web-app>
[/xml]
Output
7. Deploy the application into the server and run the application.
8. Enter the required field and click on submit button.
9. An alert message will be displayed, proceed by clicking OK button, then the output will be as shown in the below image.