
Scripting tags are important concept in JSP. You can insert java code inside the JSP by using Scripting tags.
The most important thing you should know before using Scripting tags, putting small amounts of java code into JSP page is fine, but using long and complicated java code blocks in JSP page is hard to maintain, hard to debug, hard to reuse.
JSP scripting tags are very useful when we want to add JAVA code to JSP pages. This reduces the effort of creating separate files for the Java code and the JSP code. Scripting Elements is easy to use and does not require any knowledge as a prerequisite.
In this article, we will discuss about the scripting tags in details with syntax and sample examples. At the end of this article you will get the idea about all the scripting elements.
Table of Contents
Types of Scripting Tags
The following types of scripting tags are available in JSP:
- Declaration Tag
- Expression Tag
- Scriptlet Tag
JSP Scriptlet Tag
You can write the java statements, method declaration, variables declaration, expression and so on inside the Scriptlet tag. A scriptlet tag starts with <% and ends with %>.
Syntax of Scriptlet Tag
<% Java Source Code Here %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.println("<h1>Example of Scriptlet Tag in Jsp</h1>");
%>
</body>
</html>
JSP Expression Tag
If you need to display the output of the data on the JSP page. The expression tag helps in such a scenario. This tag evaluates to a single Java expression and displays the output.
When you put the data inside an expression tag, it gets printed on the output stream and automatically converts the data into a string. In other words, you can use the expression tags to display the value of variables and methods without writing the out.println().
Syntax of Expression Tag
<%= Java statement %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Addition: <%= 10 + 20 %>
Date: <%= new java.util.Date() %>
</body>
</html>
JSP Declaration Tags
If you want to declare variables or methods inside JSP page then you can use JSP declaration tag. Most importantly, you must declare the variable or method before using it in the JSP page.
Syntax of Declaration Tag
<%! Variable or method declaration %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%!
int x = 20;
int y = 50;
int sum = x + y;
%>
<%= sum %>
</body>
</html>
JSP Comments
Comments are very useful part for developer which helps developer to read the code for better understanding. It guides the developer about what is working in the block of code, or what the method is doing if you add comments to the method.
So if you are developer then you should add comment to explain basic logic and working so that other developers can understand code or block of code very easily.
Types of JSP Comments
There are three types of comments available for a JSP page:
- JSP Comment
- HTML Comment
- Java Comment
Syntax of JSP Comment
Using JSP comment you can mark JSP code as comment. In other words, the web container ignores the JSP code which are inside the comment tag.
<%-- Comment description --%>
Syntax of HTML Comment
HTML comment tag is common for HTML, XML and JSP.
<!-- Comment description -->
Syntax of Java Comment
We can use java comment inside the scriptlet tags because we can insert java code in scriptlet tags.
// Single line comment /* Multiline Comment */
Example of Comments in JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%-- JSP comment --%>
<!-- HTML comment -->
<%
// out.println("<h1>Single Line comment</h1>");
/*
Multiline Comment
*/
%>
</body>
</html>
In conclusion, you have learned about JSP scripting tags and JSP native comments which are useful for JSP developer and also it is the most asked question in interview..