List the different tags available in JSP. Explain each of the tag with brief description and syntax.

  JSP SYNTAX

There are five main tags that all together form JSP syntax. These tags are: Declaration tags, Expression tags, Directive tags, Scriptlet tags, and Action tags.

1 Declaration tag (<%! %>)

This tag allows the developer to declare variables or methods. Before the declaration, we must have <%! and at the end of the declaration, we must have %>. Statements placed in this tag must end with a semicolon. Declarations do not generate output so are used with JSP expressions or scriptlets.

Example

<%!

private int counter = 0;

private String getAccount( int accountNo);

%>

%>)


2. Expression tag (<%=  %>)

This tag allows the developer to embed any Java expression. It is the short hand form of statement "out.println(" written in servlets. A semicolon should not appear at the end of the statements wriiten inside the tag. For example, to show the current date and time, we can write

Date <%= new java.util.Date() %>


3. Directive tag (<%@ directive %>)

It is a JSP directive that gives special information about the page to the JSP Engine. It usually has the following form: <%@ directive attribute="value" %>. There are three main types of directives:

Page Directive - processing information for this page.

Include Directive- files to be included.

Tag library Directive- tag library to be used in this page.

Directives do not produce any visible output when the page is requested but change the way the JSP Engine processes the page. For example, we can make session data unavailable to a page by setting a page directive (session) to false.

Page directive: This directive has 11 optional attributes that provide the JSP Engine with special processing information. Some of the attributes are:

<%@ page language="java" %> <%@ page extends = "com.taglib... %>

<%@ page import = "java.util.*" %>

Include Directive: It allows a JSP developer to include contents of a file inside another. File is included during translation phase. Examples are given below:

<%@ include file="include/privacy.html" %>

<%@ include file = "navigation.jsp" %>

The first example includes the privacy.html file residing in include directory into the JSP page and the second example includes navigation.jsp file in current directory into the JSP page

Tag Lib directive: A tag lib is a collection of custom tags that can be used by web developers in the current. It uses following syntax:

<%@ taglib uri = "tag library URI" prefix = "tag Prefix" %>

Example

<%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>

Scriptlet tag (<% ... %>)

In JSP, java code can be written inside the jsp page using the scriptlet tag. Code is written between <% and %> tags. This code can access any variable or bean declared. For example, to print a variable, we can write scriptlet as below:

<%

String username = "Suman"; out.println (username ) ;

%>


5. Action tag

The action tags are used to control the flow between pages and to use Java Bean. Some of the action tags supported by JSP are: jsp:forward, jsp:include, jsp:param etc. An example of action tag that forwards to another JSP page is given below:

<jsp: forward page="printdate.jsp" />

6. JSP Comment

JSP comments marks text or statements that the JSP container should ignore. A JSP comment is useful when we want to hide or "comment out" part of our JSP page. Following is the syntax of JSP comments:

<%-- This is JSP comment --%>

Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC