Updated April 5, 2023
Introduction to JSP Include
The JSP has a different set of directives for the client or user messages to the JSP containers it provides the global level information’s through about the single or particular JSP pages. And also JSP directives are the special kind of instructions to the JSP containers to translate the JSP codes into the java servlet codes. In the JSP life cycle, translation is one of the phases for the transformation of the codes process based on the user or project requirements. The include is one of the JSP directives for including the data contents related to any kind of resources with different extensions like JSP, HTML, or any text files. The include tag includes the page resources into the translation phase at the required time.
Syntax:
The basic syntax of tags in the JSP web page files is as follows.
<html>
<body>
<%@include file="file name"%>
<%
---some logic codes-- %>
</body>
</html>
The above code is the basic syntax for the include directive in the html page which is also added the <% %> JSP tags which is helpful to include other files for navigating the web applications flow in the project.
How does JSP Include?
- The JSP includes directives also the set of messages or data to the JSP containers it provides the access like globally so we can view the page in the entire through the world in the browser, particularly in the specific web application. After the include directive entered in the page it moves to the translation phase for converting the codes from JSP into the servlet. We can include the HTML, PHP, XML,JSP, etc it includes any files with different extensions that are already created or yet to be created in the web project.
- They include also one of the action tags like added the different sets of extension files into the current JSP file or page. The included file resources also it can be any static page for JSP,HTML, or java servlet. We can also include the arguments in the particular values for the specific resources file its also be included in the include tag. Using <jsp:param…./> tag we can add the parameters or arguments in the file. If we use the include in the <%@include file=” %> header page whatever the changes we make to include in the files it is not be reflected in the application but if we use the include in the inside the page <jsp: include page=””/> that is the action time its will reflected the changes in the included files.
- While Accessing the JSP directives in the more number of times somewhere is not update the time when we use the tomcat application server it reflects the changes but the container won’t check the included files timestamp also not updated but include directive can include only static page contents, values are assigned in the property file it must be path and file names files exist otherwise file not found exception occurred at run time the files extension may be any types like .html,.jsp,.js, etc.I suppose we have a servlet back end code in our application it may not be included in the include header tag when we include the same servlet in the tag we get the exception like file not found error at the run time. But when we include the servlet in the action tag format <jsp:include=”/servletname”/> the include action tag accepts not only the static page contents it also supports dynamic web page contents. This inaction tag format it supports the requested time interval if it exceeds the server timeout error will be shown and also JSP page contents do not affect the main page of the web applications it includes and shown the output of the included JSP pages.
- When compared to the include tag as the directive as well as action scenarios in directive files include static format only and also files are changed randomly if we use common codes in the snippet that we can reuse the multiple pages. The include directive code is the reusability JSP page also translates the source codes at only once so it will be better to include the file resources as static.
Examples to Implement JSP Include
Below are the examples mentioned:
Example #1
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<%@include file="welcome.html"%>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Success</title>
</head>
<body>
<h2>Welcome To My Domain</h2>
</body>
</html>
Output:
Example #2
Code:
<html>
<body>
<h2>Sample</h2>
<jsp:include page="daate.jsp" />
<h2>Welcome To My Domain</h2>
</body>
</html>
<html>
<body>
<% out.print("Today date is:"+java.util.Calendar.getInstance().getTime()); %>
Welcome user: <%
String n1="siva";
out.print(n1); %>
</body>
</html>
Output:
Example #3
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Example</title>
</head>
<body style="max-width:660px; text-align: center; ">
<%@ include file="daate.jsp" %>
<h2>Demo</h2>
<p>
<marquee>
Welcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My Domain
Welcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My Domain
Welcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My Domain
Welcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My DomainWelcome To My Domain
Welcome To My DomainWelcome To My DomainWelcome To My Domain
Welcome To My DomainWelcome To My Domain
Welcome To My Domain</marquee>
</p>
<jsp:directive.include file="//WEB-INF//footer.jsp" />
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<title>Thank you</title>
</head>
<body>
<marquee>Thank you</marquee>
</body>
</html>
<html>
<body>
<% out.print("Today date is:"+java.util.Calendar.getInstance().getTime()); %>
Welcome user: <%
String n1="siva";
out.print(n1); %>
</body>
</html>
Output:
The above examples we will use the include tag in both actions and directive method in the JSP we can add the calendar function for more use.
Conclusion
The JSP include directive and action tags are two different types for translate the JSP codes into the JSP containers we can add all other extension files in the single web page for work in the navigation for web applications.
Recommended Articles
This is a guide to JSP Include. Here we discuss an introduction to JSP Include with appropriate syntax, working and examples to implement. You can also go through our other related articles to learn more –