JSP scriptlet tag also known as Scriptlets are nothing but java code enclosed within <% and %>
tags. The main purpose of scriptlet tag is to add java code into a JSP page. JSP container moves the statements enclosed in scriptlet tag to _jspService() method while generating servlet from JSP. The reason of copying this code to service method is: For each client’s request the _jspService()
method gets invoked, hence the code inside it executes for every request made by client.
Syntax of Scriptlet tag:
<% Executable java code %>
JSP to Servlet Conversion for Scriptlet
We already know that a JSP page doesn’t get executed directly, it gets converted into a Servlet first and then Servlet execution happens as normal. Also, I explained in the beginning of this tutorial that while translation from JSP to servlet, the java code is copied from scriptlet to _jspService() method. Let’s see how that happens.
Sample JSP code:
<H2>Sample JSP</H2> <% myMethod();%>
Note: Semicolon at the end of scriptlet.
Corresponding translated Servlet code for above JSP code:
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println("<H2>Sample JSP</H2>"); myMethod(); }
JSP Scriptlet Tag Example
In this example, we have a JSP page where bunch of tags are used. For now, just focus on the scriptlet tags <% %>
. Notice that inside these tags, we are comfortably writing java code. Remember to follow the java syntax such as semicolon after statements when working with scriptlets.
<%-- A jsp example to learn the JSP scripting elements--%>
<%String string1 ="JSP scriptlet";%>
<%!String string2 ="";%>
<html>
<head>
<title> JSP page: Welcome </title>
</head>
<body>
<h1>
<%--This is an Expression statement--%>
Welcome to <%=string1%>
</h1>
<%--same thing can be done in this way also--%>
<%if(localstring.equals("JSP scriptlet"))
out.println("Hi"+string2);
else
out.println("hello");
%>
</body>
</html>
In the above example there are many JSP elements present such as Expression tag, JSP comments, Declaration element etc. We will discuss them in next JSP tutorials but as of now you only focus on Scriptlet tag. The scriptlet tag present in the above example are as follows:
<%if(localstring.equals("JSP scriptlet")) out.println("Hi"+string2); else out.println("hello"); %>
The above code is a JSP scriptlet (notice starting <% and ending %> tags). As you can see that the code inside scriptlet tag is a pure java code so in order to execute java code in JSP we use scriptlets.
<%String string1 ="JSP scriptlet";%>
Like above set of statements this statement is a java initialization code which is enclosed within tags.
Apart from above two set of scriptlets there are many other scriptlet tags present in above example (notice if-else control flow logic). To use the if-else control flow statements of java, we have used scriptlet in above example. As this is the main advantage of using scriptlet so let’s make it more clear with the help of an example.
JSP Scriptlet tag example: use of if..else
Let’s say there is a variable num and you want to display “hi” on your webpage if it is greater than 5 else you want to display another message. The java code for this logic should look like this:
if (num > 5) { out.println("hi"); } else { out.println("num value should not be less than 6"); }
To write the similar code in JSP we need to use JSP scriptlets: Code would be like this –
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.0 translation //EN">
<HTML>
<HEAD>
<TITLE> MY JSP PAGE </TITLE>
</HEAD>
<BODY>
<% if (num > 5) { %>
<H3> hi </H3>
<%} else {%>
<h3> num value should not be less than 6 </h3>
<% } %>
</BODY>
</HTML>
Important Point to Note: Since the code inside scriptlet tag is a java code it must end with a semicolon(;).
Theodora says
how can i write a scriplet to to execute a loop 10 times