Java server Pages Tags

JSP Tags -: This is called the scripting tags.The scripting elements provide the facility to insert code into JSP. There are many type Java Server Pages tags available these are the

  1. Declaration Tags
  2. Expression Tags
  3. JSP Scriptlet Tag
  4. JSP Comment tags

Declaration Tags-: In this declare variables, methods and classes in the declaration tag of JSP. If any variable, method or class are declared before we can use it. the syntax of declaration tag is two way like

Syntax for JSP Declaration :
<% declaration %>
OR
<jsp: declaration>
declaration </jsp: declaration>

For example of variable declaration of JSP

<html>
<head>
<title>how to use Declaration Tag in Variable Declaration</title>
</head>
<body>

<%! int x = 1; %>
<jsp:declaration>
int y = 2;
</jsp:declaration>

<% 
out.print("Value of x : " + x + "<br />"); 
out.print("Value of y : " + y);
%>

</body>
</html>

Output of this program

  • value of x : 1
  • value of y : 2

How to method declare -: Here Is The Example for Method Declaration

<html>
<head>
<title>How to Method Declaration</title>
</head>
<body>

<%! 
String ConcatStr(String str1, String str2){
	return str1 + str2;
}
%>
<% out.print(ConcatStr("Hello", " netnic")); %>

</body>
</html>

Output of this program

Hello netnic

Write a example of class declaration in jsp

<html>
<head>
<title> Class Declaration</title>
</head>
<body>

<%! 
public class student{
	public String myMethod(){
		return "Hello netnic";
	}
}
%>
<% 
student x = new student();
out.print(x.myMethod());
%>

</body>
</html>

Output of this program

Hello netnic

Expression Tag -:This tags are used to print the expression. The arguments that are given on the out.print () method are given in the argument tag Expression tag. The out.print () method is not required when the expression tag is used. Expression Tag is used to print variable, method and class.

In the Expression Tag, semi-colon (;) is not given at the end of the expression.

Syntax of expression of tags

<%= expression %> or <jsp: expression> expression </jsp: expression >

For example of expression tags


<html>
<head>
<title>Expression Tag</title>
</head>
<body>

<%! int x = 1, y = 2; %>

<%= 
"Value of x : " + x + "<br />" + 
"Value of y : " + y 
%>
<jsp:expression>
"Value of x : " + x +  
"Value of y : " + y
</jsp:expression>

</body>
</html>

output of this program

Value of x : 1
Value of y : 2 Value of x : 1Value of y : 2

JSP Scriptlet Tag -:The code of java is given in the JSP Scriptlet Tag. The code of this java may contain declarations and expressions of variables, methods or classes.

The syntax of JSP Scriptlet tags

<% java code %> or <jsp: scriptlet> java code </jsp: scriptlet>

<html>
<head>
<title>Scriptlet Tag </title>
</head>
<body>

<% int x = 1, y = 2; %>

<%
out.print("Value of x : " + x + "<br />");
%>
<jsp:scriptlet>
out.print("Value of y : " + y);  
</jsp:scriptlet>

</body>
</html>

Output of this program

Value of x : 1
Value of y : 2

JSP Comment tags-: jsp comment When given, the text provided by JSP Engine is ignored. JSP Comment is used to understand jsp code. The syntax of comments tags as like

<%– comment –%>

For example of jsp comment tags

<html>
<head>
<title>example of comment Tag</title>
</head>
<body>

<% out.print("jsp comment has been added here"); %>

<%-- This is a JSP Comment --%>

</body>
</html>

Output of this program

jsp comment has been added here

Leave a Comment