JSP Scripting Elements – Expressions, Scriptlets, Declarations

Placement-ready Online Courses: Your Passport to Excellence - Start Now

This article discusses the scripting elements and their use in JSP. It has a detailed description of the scriptlets, expressions, declarations in JSP. An additional element i.e. JSP comments have also been mentioned. This article tells about their importance in JSP code.

JSP Scripting Elements

JSP Scripting Elements

JSP scripting elements are one of the most vital elements of a JSP code. These <% %> tags contain JSP scripting elements. These tags are of the utmost importance as, at the time of translation, the JSP engine will consider anything inside these tags. Only this code will convert to Java code. Code other than this is plain or HTML text.

The scripting elements thus help to embed java code to the HTML, CSS, JavaScript code. There are three main subdivisions of scripting elements in JSP.

1. Expression Tags
2. Scriptlet Tags
3. Declaration Tags

They all provide different ways to include java code in a JSP file.

1. Scriptlets in JSP

JSP Scriptlets help in embedding Java in HTML for JSP code. <%___%> tags contain the Java code and are called as scriptlet tags. Unlike expressions, scriptlets have a set of statements in Java language. It allows writing java code in it. The Syntax of Scriptlet tag is as follows:

<% statement; statement ;….%>

The code inside a scriptlet tag goes to the _jspService() method of the generated servlet for processing the request. For each request sent, the _jsp service() method will invoke. If multiple scriptlets are present in the code, then they are appended to this service method in an ordered fashion.

For Example:

scriptlet.jsp
<html>
<head><title>Scriptlets</title>
</head>
<body>
<h3>--DataFlair--</h3>
<h3>Use of scriptlet in JSP</h3>
      	<% int a=3;
      	   int b=4;
      	   int c=5;
      	   out.println("a is: " +a+ "<br>" + "b is:" +b+ "<br>" + "c is:" +c+ "<br>");
      	   out.println("Multiplication gives: " + a*b*c + "<br>");
      	   out.println("Addition gives:" + (a+b+c));
      	%>
</body>
</html>

Explanation

The above code shows the use of a scriptlet tag that embeds the java code in it. The code inside the scriptlet tag will go _jspService() method and will become pure java code. The following is the output.

Output:

scriptlets in JSP

 

2. JSP Expressions

JSP Expression tags, as the name suggests, evaluates expressions. They incorporate arithmetic and logical expressions so that they can be evaluated. They form an easy means to access the value of a Java variable. The Expressions in JSP can be an access point for other expressions and merge that value in an HTML file. They can easily access data stored in applications.

Syntax of the expression is:

<%=expression/statement %>

This code will get written to the output stream of the response.

These expressions or statements can be any valid Java statement. This code gets converted to out.print( ) statement when an auto generated servlet gets formed. Use of expressions generates scriptless code.

Following are the examples of expressions in JSP.

a. expression.jsp

<html>
<head><title>Expressions</title>
</head>
<body>
<h3>--DataFlair--</h3>
<h3>Use of Expressions in JSP</h3>
<% String s1="expressions: done by kajal"; %>
<% out.print(s1); %> </br>
<%=s1.toUpperCase() %>
</body>
</html> 

b. expression2.jsp

<html>
<head><title>Expressions2</title>
</head>
<body>
<h3>--DataFlair--</h3>
<h3>Use of Expressions in JSP</h3>
<% int n1=10, n2=20; %>
<% out.print("Addition of numbers is ="); %>
<%=n1+n2 %>
</body>
</html>

Explanation

1. In this example, we can see the use of expression to get the result of the logical expression. The result is shown in the output below.
Output:

jsp expressions

 

2. Contrary to the first example, we can see the use of arithmetic in the JSP code. The output is below.
Output:

JSP Expressions

3. JSP Declarations

Declarations in JSP declare java language statements. <%!___%> tags contain declarations. They can declare classes/instances/inner classes/variables/methods.

Unlike the other two, this code doesn’t go to _jspService() method. This code rather goes to the source file that gets generated outside the _jspService method.

Syntax for declarations are:

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

Declarations have no access to implicit objects as they don’t appear in the service method. A declaration will remain outside the service method. If a method declared under declaration wants to access an object from scriptlet then it needs to pass the object as parameter.

Example of declaration in JSP:

Declaration.jsp
<html>
<head><title>Declaration</title>
</head>
<body>
<h3>--DataFlair--</h3>
<h3>Use of Declaration in JSP</h3>
      	<%! int num1=2, num2=3, n=0; %>
      	<% n=num1+num2+1;
    	out.println("The number after adding declared variables is " + n);
      	%>
</body>
</html>

Explanation:
In this code, we declared num1 and num2. Then we performed calculations on it. It will thus generate the following output.

Output:

JSP Declaration tags

Difference Between JSP Scriptlet tag and Declaration tag

JSP Scriptlet tagJSP Declaration tag
Only variables can be declared.Methods as well as variables can be declared.
The declaration inside this tag goes inside the _jspService() method.The declaration inside this tag goes to the generated source file outside the _jspService() method.

There is an additional element that is considered as a scripting element and that is Comments.

JSP Comments

Comments increase the understanding of the code. They will not be there in the output. Browser or the container ignores them. They are simply statements that can explain the code.

There are two types of comments in JSP. They are:

  • JSP code contains JSP comments. JSP container ignores these comments. They are hidden comments as they are not sent to the output stream.

The syntax of a JSP comment is as follows:

<%----Comments ignored by container----%>
  • Secondly, the HTML comments. They go in the XML or HTML code. Browser ignores these comments whereas a JSP container treats them as an HTML tag. They can be visible under view source.
<!----Comments ignored by browser----!>

For Example:

comments.jsp
<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:
We have used both HTML as well as JSP comments. Both the commented lines are not there in the output below:

Output:

There are some special constructs that we can use to insert comments in JSP, so that they can be treated specially. They are:

S.NO.JSP ConstructDescription
1.<%___%>This is a comment in JSP code.JSP engine ignores this comment.
2.<!____!>Browser ignores this comment as it is an HTML comment.
3.<\%This will represent start of a static literal
4.%\>This also shows static variables.
5.\’This represents Single quoted attributes that will use single quotes
6.\”This represents Double quoted attributes that will use double-quotes.

Conclusion

Inferring from the above article, we discussed the use of JSP scripting elements to embed java code in HTML, CSS, JavaScript. We learned about the subdivisions of scripting elements like scriptlet tag, expression tag, declarations. Then we also discussed an additional element called Comments. We saw their applications as well with the help of examples.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.

1 Response

  1. Mindmade Technologies says:

    WOW! Awesome article, Thanks for sharing this article. Keep posting

Leave a Reply

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