JSP Syntax and Semantics

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

This JSP Tutorial deals with the writing structure and the use of it by the JSP containers. It describes the main elements of a JSP page, namely scripting elements, directives, and actions. It also discusses additional elements like comments, implicit objects, custom tags. So let’s start with JSP Syntax and Semantics.

JSP Syntax and Semantics

JSP Syntax and Semantics

As JSP deals with the writing of codes, a proper way to write that code becomes important. Thus we need to understand the syntax and semantics of a JSP code. To understand it better, we first need to know what syntax and semantics itself mean.

Syntax – Syntax is the coding structure to represent elements.

Semantics – It is the meaning of that element to the container i.e., what happens when that element is used.
Thus we will discuss various elements that form the basis of JSP Syntax and Semantics

Components of a JSP Page

1) JSP elements
2) Fixed template data
3) Or a combination of both

                                               JSP elements

JSP containers will recognize these instructions. They tell about the generation of the code and how it needs to be operated. They have specific start and end tags for their recognition and identification by the web container.

                     Fixed template data

As this data is static or fixed, the compiler will not recognize it. This data won’t change so that when the HTML file  generates at the end, this data remains intact.

JSP Elements

There are 3 most important elements in JSP

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

1. JSP scripting elements
1.1 Expressions
1.2 Scriptlets
1.3 Declarations

2. Directives
2.1page
2.2include
2.3taglib

3. Actions

Above mentioned elements form the most part of JSP code. In addition to this, various other elements are there like:

  • Implicit objects
  • JSP comments
  • JSP custom tags

1. JSP Scripting Elements

1.1 Expressions

They are a simple means for accessing the value of a Java variable. They can be an access point to other expressions and merging that value with HTML as well.

Syntax of expressions is:

<%=expression/statement%>

This code gets written to the output stream of the response. Now, this expression or statement can be any valid Java statement. This code will convert to out.print() statement when an auto generated servlet forms until and unless the expression is convertible to string format.

For Example:

<html>
<head><title>Expression</title>
 
</head>
<body>
<h6>--DataFlair--</h6>
 
<% String s1="kajal"; %>
<% out.print(s1); %>
<% String s2=s1.toUpperCase(); %>
<% out.println( "  "+s2); %>
 
</body>
</html>

Explanation: Each line in the code is an expression. In this code we convert lower case to upper case.

Output:

jsp expressions

1.2 Scriptlets

Scriptlets embed Java code in HTML for JSP code. It is like an expression, the only difference is that these are a set of statements written in Java language. It allows writing java code in it and is written between <%—-%> are called scriptlet tags. Syntax of Scriptlet tag is as follows:

<% statement; [statement;….]%>

Everything that is inside a scriptlet goes to the _jspService() of the servlet code for processing the request. In case a code has multiple scriptlets then they will append in the _jspService in an ordered fashion.

For Example:

<html>
<head><title>Scriptlet</title>
 
</head>
<body>
<h6>--DataFlair--</h6>
 
<% int a=1;
       int b=2;
       int c=3;
            out.println("The number is " + a*b*c);
%>
</body>
</html>

Explanation: In this code, we multiply three numbers and print the result in scriptlet tags.

Output:

jsp scriptlets

 

 

1.3 Declarations

Declarations too have java language statements. Yet there lies a major difference. This code doesn’t go to _jspService(). This code incorporates into the source file that gets generated outside the _jspService method. Syntax is:

<%! statement,[statement,….] %>

Important points to note about declaration are that:

  • They have no access to implicit objects(discussed further).
  • They declare class/instance/variable/methods/inner classes.
  • Declaration is inside the servlet class but remains outside the service method. As said earlier this code doesn’t go to _jspService() method.
  • If a method that is inside declaration wants to use requested object from scriptlet then it needs to pass the object as parameter.

Example of declaration in JSP:

<html>
<head><title>Declaration</title>
 
</head>
<body>
<h6>--DataFlair--</h6>
<%! int num=1, n=0; %>
<% n=num+1;
        out.println("The number is " + n);
%>
</body>
</html>

Explanation: In this code, we have used declaration at code line 6, where we have declared the variables.

Output:

jsp scriptlets

2. Directives

It is a type of instruction to the web container at the time of translation. Directives tell what code needs to be generated. It affects the compilation of the JSP page done by the container. Syntax:

<%@ directive name [attribute=”value”attribute=”value”…..]%>

Note: They <%@……%> should lie in the same physical file.

Classification of Directives under 3 categories is as follows:

2.1 Page Directive

It defines the attributes for the JSP page as a whole.
Syntax:

<%@ page[attribute=”value”attribute=”value”]%>

The attributes of the page directive are:

  • language
  • extends
  • import
  • session
  • buffer
  • autoflush
  • isThreadsafe
  • info
  • isErrorpage
  • errorpage
  • contentType

For Example:

<%@ page language=”java” contentType=”text/.html”>

Explanation:
This code implies that coding of the page has been done in Java where the contentType of output is text or an HTML response. We won’t get an output here as directives are an offset to the JSP code only.

2.2 Include Directive

It works similarly to the #include of the C preprocessor directory. It’s work is to merge the contents of another file into .jsp input stream at the translation phase. Syntax:

<%@ include file=”filename”%>

For Example:

<%@ include file=”header.html”>
<%@ include file=”sortmethod”>

Explanation: The “filename” can be the absolute name or the relative path name as shown in the example. There is no body coding here, only the use of include tag is shown so output is not there.

2.3 Taglib Directive

With the use of tag library this directive makes the custom tags available in the current page. Syntax can be shown as:

<%@ taglib uri=”taglib URI”prefix=”tagPrefix”%>

The attributes of this directive are:

  • tagLibrary URI
  • tagPrefix

**The JSP directives section contains a detailed description of this topic.

3. Actions

Actions also known as standard actions unlike others are written in XML. These are also known as high level JSP elements. They create, modify or use other objects. Syntax:

<tagname attr=”value”:….>….body…….</tagname>

Proper nesting is important here. For e.g. <A><B>……….</B></A> is right whereas <A><B>………</A></B> is wrong.
Important tags in actions are namely:

  • <jsp:usebean>
  • <jsp:setProperty>
  • <jsp:getProperty>
  • <jsp:include>
  • <jsp:forward>
  • <jsp:param>
  • <jsp:plugin>

**The article JSP Actions contains a detailed description of this topic.
Having discussed the three essential elements of the JSP page, let’s discuss the periphery elements of a JSP page.

JSP Comments

Comments contain statements and texts but are ignored. They simply describe the code so that the usability and understanding increases.
There are two types of comments in a JSP page. They are:
Firstly, JSPcomments that are written in the JSP code. JSP container ignores these comments. Syntax:

<%----Hidden JSP comment----%>

Secondly the HTML comments that are written in the XML or HTML code. Browser ignores these comments. Syntax:

<!----included in generated HTML----!>

Example:

<html>
<head><title>Comments</title>
<!---This is an HTML comment--->
<h6>--DataFlair--</h6>
</head>
<body>
 
<%---This is a JSP comment---%>
<% int a=1;
   	int b=2;
  	out.println("The addition of a and b is:" + a+b);
%>
</body>
</html>

Explanation: In the generated output you can compare, the code and result. JSP and HTML comments get ignored by the JSP container and browser respectively.

Output:

jsp scriptlets

Implicit Objects

As we know that only scriptlet, expression and HTML data goes to _jspService() and these variables are implicitly available to all except declaration. Thus they are implicitly available; they don’t need to be declared.
Various implicit variables are request, response, pageContext, session, application, out, config, page, exception. They can also be created using tag libraries.

Custom Tags

These tags can be created so that the functionality of JSP can be extended. They allow the encapsulation of functionality. This is thus made available to non-expert page authors. There is a separate article to discuss custom tags in detail.

Conclusion

This article discusses the basic syntax and semantics in JSP. We have learned about different elements of a JSP code. They are scripting elements, directives, and actions. Scripting elements consist of the main coding part under the scriptlet tag. It also consists of declarations and expressions. We discussed another important part i.e., directives that tells about the attributes of the page, includes files necessary and gives advantages of custom tags. Other important parts discussed were comments, implicit objects, tag extensions.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

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