JSP

JSP Projects

JSP Project

What are JSP tags and how they used?
Previous Home Next

 

There are five types of tags used in JSP. Declaration tag ,Expression tag ,Directive tag,Scriplet tag ,Action tag.These are as listed below......
1)Expression tag
2)Scriptlet tag
3)Declaration tag
4)Scriplet tag
5)Action tag

1)JSP declaration tag:

The JSP declaration tag is used to declare fields and methods.

The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet.

So it doesn't get memory at each request.

Syntax of JSP declaration tag:

The syntax of the declaration tag is as follows:

<%! field or method declaration %> 

Example: JSP declaration tag that declares field:

In this example we explain JSP declaration tag, we are declaring the field and printing the value of the declared field using the jsp expression tag. 

index.jsp

<html>

<body>

<%! int data=100; %>

<%= "Value of the variable is:"+data %>

</body>

</html>
Example:JSP declaration tag that declares method:

In this example we explain JSP declaration tag, we are defining the method which returns the cube of given number and calling this method from the jsp expression tag. But we can also use jsp scriptlet tag to call the declared method.

index.jsp

<html>

<body>

<%!

int cube(int n){

return n*n*n*;

}

%>

<%= "Cube of 4 is:"+cube(4) %>

</body>

</html> 
2)Expression Tag in JSP:
Expression tag is used to display output of any data on the generated page. The data placed in Expression tag prints on the output stream and automatically converts data into string. The Expression tag can contain any Java expression used for printing output equivalent to out.println().Thus, an expression tag contains a scripting language expression which is evaluated, automatically converts data to a String and the outputs are displayed.

Notation of Expression tag is shown below:
<%= %>
Expression tag must begin with <%= Inside Expression tag, the user embeds any Java expression. Expression tag ends with the notation %>.

NOTE: Expression should not contain a semicolon between codes, as with Declaration tag.

General syntax of Expression Tag:

<%! //start of declaration tag

statement //Java Expression

%> //end of declaration tag

For example:

<%!

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

%>
3.Directive tags
Directive tags gives information about the page to the JSP engine.Using directive tags user can import packages. 
Notation for directive tag <%@      % >
The jsp directives are messages that tells the web container how to translate a JSP page into the corresponding servlet.
There are three types of directives:
a)page directive
b)include directive
c)taglib directive




 
4)Scriptlet tag in JSP:
The scriptlet tag is used to write java class statements inside the jspService() method. In other words, writing java code in a Java Server Page is done through scriptlet tag. A scriptlet tag starts with <% and ends with %>
Example: insert the following codes between the <body> tags.
<%

// This scriptlet declares and initializes "date"

System.out.println( "Evaluating date now" );

java.util.Date date = new java.util.Date(); %>

The time is now:

<% // The following scriptlets generate HTML output

out.println( String.valueOf( date ));

out.println( "<br>Your machine's address is: " );

out.println( request.getRemoteHost()); %>

5)JSP Action Tags:
There are many JSP action tags or elements. Each JSP action tag is used to perform some specific tasks.
The action tags are used to control the flow between pages and to use Java Bean. The Jsp action tags are given below.

JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
jsp:useBean creates or locates bean object.
jsp:include includes another resource.
jsp:setProperty sets the value of property in bean object.
jsp:getProperty prints the value of property of the bean.
jsp:plugin embeds another components such as applet.
jsp:param sets the parameter value. It is used in forward and include mostly.
jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.

note:The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean development. So we will see these tags in bean developement.Here we select only important JSP action tags.

jsp:forward action tag:

The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or another resource.

Syntax of jsp:forward action tag without parameter

<jsp:forward page="relativeURL | <%= expression %>" />

Syntax of jsp:forward action tag with parameter

<jsp:forward page="relativeURL | <%= expression %>">

<jsp:param name="parametername" value="parametervalue | <%=expression%>" />

</jsp:forward>

Example: jsp:forward action tag without parameter

In this example, we are simply forwarding the request to the printdate.jsp file.

index.jsp 

<html>

<body>

<h2>this is index page</h2>

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

</body>

</html>

printdate.jsp

<html>

<body>

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>

</body>

</html>

Example: jsp:forward action tag with parameter

In this example, we are forwarding the request to the printdate.jsp file with parameter and printdate.jsp file prints the parameter value with date and time.

index.jsp
 

<html>

<body>

<h2>this is index page</h2>

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

<jsp:param name="name" value="ycd.com" />

</jsp:forward>

</body>

</html>

printdate.jsp

<html>

<body>

<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>

<%= request.getParameter("name") %>

</body>

</html>


 Examples
Declaration tag Example
<%! // Start declaration tag
private int example = 0 ;//define variales
private int test = 5 ;
%> // end the decllaration tag

Expression tag Example
<%!                 

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


%>

Previous Home Next