Introduction to JSP Scripting Elements
JSP scripting enables one to add JAVA code into JSP (Java server pages). The JAVA code can be inserted into these pages with the help of script tags. JAVA code can be written within these scripting tags. The server will determine the JAVA code using script tags as an indicator at the start and end of code. These scripting tags are called scripting elements. We will learn more about the functioning and use of these scripting elements in this topic with the help of examples.
Types of JSP Scripting Elements
JSP scripting tags can be categorized majorly into three categories:
1. Scriptlet Tag
This tag is simply to have JAVA code written under “<%” AND “%>”. So, the compiler understands the initial point from where the JAVA code implementation begins and ends. It is notified like below:
<% JAVA CODE TO BE WRITTEN HERE %>
Syntax:
<html>
<body>
<h1> syntax of JSP Scripting elements</h1>
<% This space is used to write JAVA CODE… %>
</body>
</html>
To understand the example, we need to understand the structure of JSP pages and how these are triggered. We need to have an index page (written in html language with an extension of htm/html) using which we will call the JSP page using <form action “”> tag. This JSP page should have a JAVA code embedded in the scriptlet tag.
Two pages should be created. One named “Indexpage.html”, this will be used as an entry page to other page names “JSPScriptletTag.jsp”. In “JSPScriptletTag.jsp” JAVA code is written under “<%..%>”.
Indexpage.HTML
Code:
<html>
<body>
<form action="JSPScriptletTag.jsp">
<input type="text" name="testname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
JSPScriptletTag.jsp
Code:
<html>
<body>
<%
String name=request.getParameter("testname");
out.println("You are viewing this name: "+name+" from the JAVA code embedded in the scriptlet tag of JSP pages..");
%>
</form>
</body>
</html>
The advantage of a scriptlet tag is that it is very handy to be used. If small enhancements are required in the JSP pages, then these can be added without putting much overload while loading. But in case long JAVA scripts need to be inserted then it is advised to use other types of tag as it may land up adding delay, overhead and hard to understand.
2. Declaration Tag
This tag is used t declare the method, functions, fields, data type properly. The code which is written in the declaration tag is placed out of service() method. The service() is the first method invoked when the servlet is auto-generated. The JAVA code is written under the tag “<%! .. %>”. It can be notified as:
<%! JAVA code is written here to demonstrate declaration tag… %>
Syntax:
<html>
<body>
<h1> syntax of JSP Declaration elements</h1>
<% This space is used to write JAVA CODE… %>
</body>
</html>
The below code is written to find out the square of any given number. The function is created in JAVA code named “square”
Code:
<html>
<body>
<%!
//below is the function definition to calculate square of any number. The number can be passed as a parameter to the function. The function will then perform an arithmetic operation to return a square of the number. The return type is also integer as per function prototype.
int square (int number) {
return number*number;
}
%>
<%= "Square of 3 is:"+square(3)+ "and this is calculated by JAVA code inscribed under JSP declaration scripting tag" %>
</body>
</html>
The major difference between declaration and scriptlet tag is that the code written under the scriptlet tag is under the scope of service() function triggered in auto-generated JSP page while this is not the case with declaration tag. Also, in scriptlet tags, we can not declare methods. The declaration is limited to data types and fields only. This is an added advantage in the declaration to have the functionality of adding method definition and declaration.
3. Expression Tag
This scripting tag is used to generate an output string that needs to be manipulated as per business requirements before appearing on the output screen. Hence we can put this tag in out, print() function. In this tag, the JAVA code is enclosed in “<%= .. %> ”. The only addition here is of “=” to scriptlet tag. It can be notified as:
<%= JAVA CODE can be written here.. %>
Syntax:
<html>
<body>
<h1 Demonstrating the example for explaining expression tag.. ></h1>
<%= "JAVA Code to be written here for expression tag" %>
</body>
</html>
In the below example we are going to see the same output as we got from the previous examples, but the only difference is of tag where we have enclosed JAVA code.
Indexpage.HTML
Code:
<html>
<body>
<form action=" JSPExpressionTag.jsp">
<input type="text" name="testname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
JSPExpressionTag.jsp
Code:
<html>
<body>
<%= "You can see this name: "+request.getParameter("testname")+" , It is appearing from the expression tag" %>
// Here we are writing code in expression tag to just get the output in the screen. There is no declaration like we had in scriptlet tag.
</body>
</html>
This tag is useful when we want to generate a short output statement with the help of the JAVA code. It is not recommended to have a bulky code written in this tag as it may increase overhead and reduce the code clarity at the time of maintenance.
Conclusion
The JSP scripting tags are very useful when we want to embed a JAVA code in the JSP pages. It reduces the effort to have a separate file and thus reduces overload, improving efficiency and propagating standard clean code writing. The scripting elements are easy to be used and does not require any knowledge as a prerequisite. Any JAVA developer can use this tag to start coding and put any logic. These are widely used to create interactive web designs.
Recommended Articles
This is a guide to JSP Scripting Elements. Here we discuss the Introduction and types of jsp scripting elements along with different examples and its code implementation. You may also have a look at the following articles to learn more –