JSP Syntax Elements

JSP has the below formats used as syntax:


Scriplet Tag:

A scriplet is nothing but JAVA statements which contain methods declarations and variables initialization.

Syntax: 

<% code %>

Sample Program:

<title>Sample JSP</title>
</head>
<body>
<H1>Sample JSP</H1>
<% out.println("Sample JSP code."); %>
</body>
</html>

Output:

Scriptlet

Expression Tag:

This is a tag where expression is evaluated and converted to a String and the expression is shown as output of a JSP file.

Syntax: 

<%= code %>

Sample Program:

<title>Display Today's date</title>
</head>
<body>
<%
java.util.Date today = new java.util.Date();
String text = "Today's date is: " + today.toString();
%>
<%= text %>
</body>
</html>

Output:

Expression

Declaration Tag:

This statement tells the servlet compiler about the variables, methods and general setup of the JSP.

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: 

<%! code %>

Sample Program:

<html>
<head>
<title>Sample JSP</title>
</head>
<body>
<h2>Sample Declaration</h2>
<%!int a=50; %>
<%="Value of a is : " +a %>
</body>
</html>

Output:

Declaration

How to comment in a JSP File:

A comment is a statement that tells the complier to ignore. A comment is used to add some information about the code or change in the code or to hide code from compiler.

<html>
<head><title>Comment</title></head>
<body>
<h2>Comments</h2>
<%--A sample comment--%>
</body>
</html>

Posted on July 12, 2014 in Java Server Pages

Share the Story

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top