JSP Directive Tag and Scriptlet tag
In this JSP tutorial, you will learn about two types of Directive tag namely Include and Tag Lib and also Scriptlet tag used in Java Server Pages along with syntax, usage, example and explanation for each of the tag.
Include directive:
Include directive is a type of directive tag. If a programmer wants to include contents of a file inside another, then the Include directive is used. The file included can be either static ( HTML file) or dynamic (i.e., another tag file). The include directive is used to include the contents of other file in the current tag file.
Syntax of Include directive is:
<%@ include file = "xxx.html/xx.tagf"%>
In the above statement, include and file are keywords. This includes either .html (static file) or .tagf file (dynamic file) between " " in the include directive.
For instance:
<%@ include file = "include/exforsys.html"%>
Above example shows how to include a static resource called exforsys.html in an include directive. The above example includes the html from exforsys.html found in the include directory into the current JPS page.
For example:
The user includes a dynamic file in the include directive:
<%@ include file="exforsys.tagf" %>
The above example shows how to include a dynamic resource called exforsys.tagf in the include directive.
Tag Lib directive:
A user can customize actions from their tag file using the Tag Lib directive. The Tag Lib directive is a collection of custom tags.
The syntax of Tag Lib directive is:
<%@ taglib uri = "tag library URI" prefix = "tag Prefix" %>
In the above, taglib and uri are both keywords. The URI, which can be absolute or relative, uniquely identifies the tag library descriptor and is enclosed with " " in tag library URI. The tag prefix included inside " " is a string that will become the prefix to distinguish a custom action.
If the custom tag has a content body, then the format for using a custom tag in taglib directive is as follows:
<prefix:tagName>body</prefix:tagName>
If the custom tag does not have content body, then the format for using a custom tag in taglib directive is as follows:
<prefix:tagName/>
Scriptlet tag:
Scriptlet tag is a type tag used for inserting Java Code into JSP.
Notation of the Scriptlet tag is:
<% %>
To begin the Scriptlet tag, the user must add <% . Inside the Scriptlet tag, the user can add any valid Scriptlet, meaning any valid Java Code. User can write the code in between the Scriptlet tag accesses any variable or bean declared. The Scriptlet tag ends with the notation %>.
NOTE: there must be semicolon ; included at the end of each code inside the Scriptlet tag.
General syntax of Scriptlet Tag:
<%!
statement1;
statement2; //Valid Java Code
……….;
……….;
%> //end of Scriptlet tag
For instance
for (int j = 0; j <5; j++) { out.print(j); } %>
<%
In the above example, the Java code embeds inside the tag <% and %>.
NOTE: at the end of the statement, a semicolon ; is appended.