JSP Tags

Tags in JSP

JSP is a serverside technology to make content generation a simple appear. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content. Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development.

JSP contains the following tags

* Declaration tag
* Expression tag
* Directive Tag
* Scriptlet tag

Declaration tag ( <%! %> )

A declaration tag declares one or more variables or methods for use later in the JSP source file. A declaration tag must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration tag must be valid in the scripting language used in the JSP file.

This tag allows the developer to declare variables or methods. Before the declaration you must have
<%! At the end of the declaration, the developer must have %> Code placed in this tag must end in a semicolon ( ; ). Declarations do not generate output so are used with JSP expressions or scriptlets.

<%! private int counter = 0 ; private String get Account ( int accountNo) ; %>

Expression tag ( <%= %>)

An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file.

This tag allows the developer to embed any Java expression and is short for out.println(). A semicolon ( ; ) does not appear at the end of the code inside the tag. Example to show the current date and time

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

Directive tag ( <%@ directive … %>)

A JSP directive tag gives special information about the page to the JSP Engine.

There are three main types of directives:

* page – processing information for this page.
* Include – files to be included.
* Tag library – 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.

Scriptlet tag ( <% … %> )

A scriptlet tag can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language. We can declare variables or methods to use later in the file. Write expressions valid in the page scripting language. Use any of the JSP implicit objects or any object declared with a tag. You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet. Scriptlets are executed at request time, when the JSP engine processes the client request.

If the scriptlet produces output, the output is stored in the out object, from which you can display it. Between <% and %> tags, any valid Java code is called a Scriptlet. This code can access any variable or bean declared. For example, to print a variable.

<% String username = “visualbuilder” ; out.println ( username ) ; %>

No comments:

Post a Comment