Table of contents
1.
Introduction
1.1.
Scriptlet tag
1.2.
Expression tag
1.3.
Declaration tag
2.
What are scriptlets tag?
3.
Example of scriptlets tag
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Scriptlets in JSP

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

By using servlets, the technology that controls the content and appearance of web pages is called JSP. JSP stands for Jakarta Server Pages, which helps create dynamically generated web pages based on HTML, XML, etc. Some small programs specified in the web pages run on the web server before sending to the user who requested it to modify the web pages.

Three types of Scripting elements in JSP are-

The scripting elements allow you to enter java code within the jsp. There is a total of three types of scripting elements in jsp.

Scriptlet tag

Scriptlet tags are the tags used to insert pure java code inside JSP, and Scriptlets in JSP are the java codes inserted inside <% and %> tags.

<html>
<head>
<title> JSP page: Welcome Ninjas!</title>
</head>
<body>


<%--Scriptlet example--%>
<%! String localstring= "Coding Ninjas"; %>  
<%! String string2= "Coders"; %>  
<%if(localstring.equals("Coding Ninjas")){%>
Hi </br>
<%}
else {%>
hello Ninjas
<%} %>


<%if(localstring.equals("JSP scriptlet Example"))
out.println("Hi"+string2);
else
out.println("Hello Ninjas");
%>
</body>
</html>

Output-

Expression tag

The code included within the JSP expression tag is sent to the response's output stream. As a result, you are not required to write anything. To write data, use print(). Its primary purpose is to print the values of variables or methods.

<html>  
<body>  
<%= "welcome to Coding Ninjas" %>  
</body>  
</html> 

Output-

Declaration tag

For declaring fields and methods, we use the JSP declaration tag.

<html>  
<body>  
<%! int subscribers=5000; %>  
<%= "Number of subscribers are "+ subscribers%>  
</body>  
</html>  

Output-

Many JSP expression tag contains elements and the comment, Declaration element, etc., But now, we will focus on Scriptlets in JSP.

What are scriptlets tag?

Scriptlet tags are the tags used to insert pure java code inside JSP, and Scriptlets in JSP are the java codes inserted inside <% and %> tags.

Syntax-

<% java source code %>  

 

How does it invoke?

The code inside the scriptlet tags is passed to _jspSrvice() by the JSP container while generating servlet from JSP. Every time the client requests _jspService() (JSP service method) gets invoked, the code inside the scriptlet tags executes every client's request.

Now we will come back o the example discussed above, and below mention are the Scriptlets in JSP statements which are being used in the above example -

<%if(localstring.equals("JSP scriptlet Example"))
out.println("Hi"+string2);
else
out.println("Hello Ninjas");
%>
You can also try this code with Online Java Compiler
Run Code

If you notice in the above code that the code is in pure java language inside the tag, we use Scriptlets in JSP for executing the java code in JSP. We used Scriptlets in JSP for using if-else control flow statements of java.

Example of scriptlets tag

<html>  
<body>  
<% out.print("Coding Ninja"); %> <!-- scriptlet tag -->
</body>  
</html> 

Output-

Explanation: 

  • We use" at the beginning of the Syntax of JSP Scriptlet tag.
  • We write java code inside the scriptlet tags.
  • For printing anything on the console, we use System.out.println In java. But In JSP, to write something on console, we use only out.print because the out we're referring to is a variable in the effective method that wraps our JSP page, not System.out.
  • System.out writes to the servlet container's console but for the generated response, out is entirely a different class that writes to the output stream.

 

Example of JSP scriptlet tag that prints the user name

In this example we will create two files  first index.html, and another is main.jsp where the index.html file will take firstName from the user and main.jsp will prints firstName with the welcome message on the web page.

<html>  
<body>  
<form action="main.jsp">  
<input type="text" name="fname">  
<input type="submit" value="go"><br/>  
</form>  
</body>  
</html>  

Output-

main.jsp

<html>  
<body>  
<%  
String FirstName=request.getParameter("fname");  
out.print("Welcome "+FirstName +" !");  
%>  
</form>  
</body>  
</html> 

Output-

You can also read about the JSP Architecture and Lifecycle.

Frequently Asked Questions

 

1.Explain about JSP to Servlet transition for Scriptlet.

We know that JSPs don't get executed directly; JSP first gets converted into a servlet and gets executed usually. The java code gets copied from Scriptlet to _jspService() ( JSP service method ) while translating JSP to servelet.

Sample JSP code:

<H3> Sample JSP, Coding Ninja</H3>
<% myMethod();%>

Note: Semicolon should be at the end of scriptlet.

For above JSP code corresponding translated Servlet code is given below :

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, Coding Ninja</H2>");
myMethod();
}
You can also try this code with Online Java Compiler
Run Code

 

2.How can we use if-else using Scriptlet? Explain with example.

We can write if else in java language inside the scriptlet tags.

Suppose there is a variable number, and if it is greater than 4, you want to display "Hi Ninjas" on your webpage; otherwise, you wanna display a message "Num value should be greater than 4". 

if (number > 4)
{
out.println("Hi Ninjas");
}
else
{
out.println("Num value should be greater than 4");
}
You can also try this code with Online Java Compiler
Run Code

And for writing this code in JSP, we need to add this code inside scriptlet tags.

<HTML>
<HEAD>
<TITLE> MY JSP PAGE EXAMPLE</TITLE>
</HEAD>
<BODY>
<% int num=3;
 if (num > 4) { %>
<h3>Hi Ninjas</h3>
<%} else {%>
<h3>Num value should be greater than 4</h3>
<% } %>
</BODY>
</HTML>

Output-

Key Takeaways

In this blog, we learned about  Scriptlets in JSP. Don't come to a halt here. Check out our Top 8 Java Projects on GitHub | Coding Ninjas Blog. Can you also check out the JSP blogs? Check out more blogs here. More Blogs.

Recommended Readings:

Happy Learning!

Live masterclass