Tags Used in JSP
Various tags are used in JSP which help to use the functionalities if Java and HTML simultaneously. Java code is not required to be complete or self-contained within a single scriptlet block. It can straddle markup content, provided that the page as a whole is syntactically correct.
JSP pages use several delimiters for scripting functions. The most basic is, <% ... %> which encloses a JSP scriptlet. A scriptlet is a fragment of Java code that is run when the user requests the page. Other common delimiters include <%= ... %> for expressions, where the scriptlet and delimiters are replaced with the result of evaluating the expression, and directives, denoted with <%@ ... %>.
So, following are the types of tags commonly used:
• Directives: In the directives we can import packages, define error handling pages or the session information of the JSP page.
• Declarations: This tag is used for defining the functions and variables to be used in the JSP.
• Scriplets: In this tag we can insert any amount of valid java code and these codes are placed in _jspService method by the JSP engine.
• Expressions: We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream.
• Custom Tags:
Custom tags are user-defined JSP language elements that encapsulate recurring tasks. Custom tags are distributed in a tag library, which defines a set of related custom tags and contains the objects that implement the tags.
Syntax of custom tag is: <prefix:tag attr1="value" ... attrN="value" />
To use a custom tag in a JSP page, two things are to be kept in mind:
i. Declare the tag library containing the tag
ii. Make the tag library implementation available to the web application
• Expression language:
Simpler form for JSP Expressions came with JSTL 1.0, in the form of Expression Language. EL is used to set action attribute values from different sources at runtime. JSP Expression Language is also used without parameters but as actions directly in a webpage. JSP EL always starts with a "$" followed by a "{" and ends with a "}". The expression is specified inside the brackets. A set of implicit variables provide access to the requested data.
Example:
${2+3+4}
JSP EL supports literal numbers, the boolean value "true,false", "null", the strings enclosed in single and double quotes.
• Additional Tags:
The JSP syntax add additional tags, called JSP actions, to invoke built-in functionality. Additionally, the technology allows for the creation of custom JSP tag libraries that act as extensions to the standard JSP syntax. One such library is the JSTL, with support for common tasks such as iteration and conditionals (the equivalent of for and if statements in Java.)