Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » JSP » JSP Scripting Tags (Scripting Elements)

  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • Holi kyon manate hain in hindi? | Festival of colors in hindi
    Holi kyon manate hain in hindi? | Festival of colors in hindi Festival
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography
  • State the Universal law of gravitation | Law of Gravity, Formula
    State the Universal law of gravitation | Law of Gravity, Formula Science
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
    International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं? General Knowledge
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • America Independence Day : 4th July USA | USA Birthday
    America Independence Day : 4th July USA | USA Birthday General Knowledge
  • Vedaant Madhavan Biography in Hindi, Family, School, Age
    Vedaant Madhavan Biography in Hindi, Family, School, Age Biography

JSP Scripting Tags (Scripting Elements)

Posted on September 1, 2021October 20, 2021 By GeekCer Education No Comments on JSP Scripting Tags (Scripting Elements)
JSP Scripting Tags (Scripting Elements)

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
    • JSP Scriptlet Tag
      • Syntax of Scriptlet Tag
    • JSP Expression Tag
      • Syntax of Expression Tag
    • JSP Declaration Tags
      • Syntax of Declaration Tag
  • JSP Comments
    • Types of JSP Comments
      • Syntax of JSP Comment
      • Syntax of HTML Comment
      • Syntax of Java Comment
      • Example of Comments in JSP

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:

  1. JSP Comment
  2. HTML Comment
  3. 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..

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

JSP Tags:Comments, Web Application

Post navigation

Previous Post: Java Server Pages (JSP)
Next Post: JSP Directives

More Related Articles

JSP Implicit Objects JSP Implicit Objects JSP
Java Server Pages (JSP) Java Server Pages (JSP) JSP
HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important
JSP Directives JSP Directives JSP

Leave a Reply Cancel reply

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

  • JSP Home
  • JSP Scripting Tags
  • JSP Directives
  • JSP Implicit Objects
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved