The <jsp:include>action tag includes other pages along with (<%@include file=heheader.jsp" %>).
While the include directive simply contains text from a specific source, the <jsp:include> action tag includes the processing results of the page. You can include HTML, JSP, Servlet, and etc.

Let us look at the examples for a better understanding.

 

<Example 1>

incldeForm.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>include Example</h1>

	<form method=post action="includeMain.jsp">
	Name : <input type="text" name="name"><p>
	<input type="submit" value="Submit">
	</form>	

	</body>
</html>

includeMain.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>includeMain.jsp</h1>

	<%
		request.setCharacterEncoding("euc-kr");
	%>

	<hr>

	<jsp:include page="includeSub.jsp" flush="false"/>
	includeMain.jsp's content

	</body>
</html>

includeSub.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<%
	String name = request.getParameter("name");
%>

 includeSub.jsp<p>
<b>Hey, <%=name%>!</b>
<hr>

So, in the browser, you will see includeMain.jsp page including includeSub.jsp page.

 

<Example 2>

incldeForm2.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h2>include Example2</h2>

	<form method=post action="includeMain2.jsp">
	Site Name : <input type="text" name="siteName1"><p>
				<input type="submit" value="Submit">
	</form>

	</body>
</html>

includeMain2.jsp

Here, <jsp:param> convey the "siteName" to this file from includeForm2.jsp.

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>
	
	<h2>includeMain2.jsp Page</h2>

	<%
		request.setCharacterEncoding("euc-kr");
		String siteName1 = request.getParameter("siteName1");
	%>

	<hr>
	<jsp:include page="includeSub2.jsp">
		<jsp:param name="siteName" value="<%=siteName1%>"/>
	</jsp:include>

	includeMain2.jsp Page Content<p>

	</body>
</html>

includeSub2.jsp

With the request object, the siteName is conveyed to the includeSub2.jsp.

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<%
		String siteName = request.getParameter("siteName");
	%>

	includeSub2.jsp Page<p>

	<b><%=siteName%></b>
	<hr>

	</body>
</html>

Both include directive tag and include action tag are similar in that they all include other pages, but the include directive tag contains only the text, whereas the include action tag contains the results of the page's processing.

 

Here is another example. 

 

<Example 3>

includer.jsp

<%@ page contentType = "text/html; charset=euc-kr" %>

<html>
	<head><title>include Directive</title></head>
	<body>

<%
    int number = 10;
%>

<%@ include file="includee.jspf" %>

Common Variable DATAFOLDER = "<%= dataFolder %>"

	</body>
</html>

includee.jspf

The 'f' in jspf means 'fraction'.

<%@ page contentType = "text/html; charset=euc-kr" %>

Number saved in includer.jsp page: <%= number %>

<p>

<%
    String dataFolder = "c:\\data";
%>

 

'Java > JSP' 카테고리의 다른 글

JSP) JSP and Oracle  (0) 2022.08.31
JSP) JavaBean  (0) 2022.08.31
JSP) Handling Exceptions  (0) 2022.08.30
JSP) Action tags - forward  (0) 2022.08.30
JSP) Scopes - Page / Request Scopes  (0) 2022.08.29

+ Recent posts