Syntax of Tags in JSP

What is the Syntax of tags in JSP?

Explanation

JSP Tags or Action Elements represent dynamic actions that occur at runtime. JSP Tags are used to write text into a file, send an email, retreive data from a database and many more. Tags are grouped into libraries called tag libraries. Tag libraries are specified in a jsp file by a uri.

Following is the syntax for tags used in JSP.

Syntax:


<prefix:action_name attr1="value1" attr2="value2">
action_body
<prefix:action_name>

Following is the syntax of a JSP tag without a body.

Syntax:


<prefix:action_name attr1="value1" attr2="value2" />

In the above syntax the closing tag is with a "/" slash at the end as it does not have a body. Usually the "prefix" attribute refers to the library the action belongs to and it enables action in different library to have the same name. The "uri" attribute identifies the tag library used for the action.

Example :


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<p>Tag Library</p>
<c:out value="${1+2}"/>
Result :

Tag Library
3

In the above example "c" is the prefix which refers the tag library "core" and "out" is the action. We use the JSTL tag c:out to display the result of adding two numbers. Here ${1+2} in an expression and is described in JSP EL.

Ask Questions

Ask Question