Table of contents
1.
Introduction
2.
JSP Syntax
2.1.
Declaration Tag
2.2.
Java Scriptlets     
2.3.
JSP Expression    
2.4.
Java Comments    
3.
Environment Setup for JSP
4.
Creating a JSP Page
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

JSP Syntax and First App

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

Introduction

JSP is a server-side scripting language that combines HTML, XML, java servlets into one highly scalable technology and develops a reliable, platform-independent dynamic web application. It is an extension of servlet technology; hence, it inherits all the servlet features and some advanced modifications, making it the perfect choice for developers to develop a web application.

JSP Syntax

Declaration Tag

This tag is used to declare classes, methods, and variables. The code written inside the declaration tag is placed outside the service() method of the auto-generated servlet. Hence, it doesn't get memory at each request.

Syntax: 

<html>  
<body>  
<%! int var=17; %>  
</body>  
</html> 

 

Example:

<html>  
<body>  
<%!   
int square(int n){  
return n*n;  
}  
%>  
<%= "Square of 3 is: "+ square(3) %>  
</body>  
</html> 


 Output:

 

Java Scriptlets     

It enables us to add java code, variables, and expressions.

 Syntax:

<% Java Code %>

 

 Example:

<html>  
<body>  
<% out.print("Welcome to Coding Ninjas"); %>  
</body>  
</html>

     
 Output:


 

JSP Expression    

It evaluates and helps in converting the expression into strings.
 

 Syntax:     

<% c= a + b %>,  <%= “JSP Servlet ” %>

     

 Example:

<html>  
<body>  
<%= "Learn JSP" %>  
</body>  
</html>  

 

  Output:

 

 

Java Comments    

It helps in adding comments in the code.

      
 Syntax:

<% - - Comment  - -%>

      
 Example:

<html>
   <body>
      <h2>Comments</h2>
      <%-- This comment will not be visible in the page source --%>
   </body>
</html>

 

 Output:

 

Let us go through a couple of examples which will help us understand using what we have learnt so far from the JSP Syntax section

Example 1: 

<html>
<head>
<meta content="text/html">
<title>Hello World JSP</title>
</head>
<body>
    <%= "Hello World!" %>
</body>
</html>

 

Output:

 

Example 2:

<html> 
   <head><title>A Comment Test</title></head>
   <body>
      <p>Today date is: <%= (new java.util.Date()).toLocaleString()%></p>
   </body> 
</html> 

 

Output:

 

Environment Setup for JSP

Step 1: Download the Java Software Development Toolkit (SDK) from this Link.

Step 2: After downloading the Java SDK, follow the instructions of the installation package. After the installation, some folders will be created on your hard drive. 

Step 3: Once installed, set the path of a particular directory where Java (C:\Program Files\Java\jdk-17.0.1) is stored along with the environment variable.

Step 4: Install Eclipse IDE from the Link.

Step 5: Now, to create your first JSP file, follow the below steps:
 

  • Create a dynamic web project. Go to File Menu -> New -> dynamic web project.

 

(Note: If you don’t find the dynamic web project option in the menu bar, please refer to this link.)

 

  • Choose the name of the JSP file and press finish to create the file.
  • After creating the file, add the content to the JSP file.

 

 

Step 6: To run the created JSP file on the newly installed eclipse IDE, first, download the Tomcat apache server in your system. 

Step 7: Install Apache Tomcat from the given Link. Unzip the downloaded file and store it in a folder that can be accessed easily later.

Step 8: Go to Windows (menu bar) -> preferences. You’ll see a dialog box given below.

 

 

Step 9: Select Runtime environment in the server. Now, go to add, and you will see a dialog box given below. Select the latest version of Apache Tomcat and the checkbox to create a new local server. Click next.

(Note: If you don’t see any option for Apache tomcat, it’s probably because you are missing a few adaptors. Refer to this link for further information.)

 

 

Step 10: Browse the folder where you have stored the downloaded Apache Tomcat. Select Finish and restart the Eclipse IDE.

 

 

Step 11: That’s all you have successfully configured ApacheTomcat in your eclipse IDE, and now you can run your JSP file in it.

Creating a JSP Page

A JSP page has an HTML body incorporated with Java code. Given JSP page includes the declaration, comments, scriptlets, tags.

<%@ 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>JSP Example Coding Ninjas</title>
</head>
<body>


<%-- This is a JSP example with scriplets, comments , expressions --%>
<% out.println("This is JSP Example Coding Ninjas"); %>
<br/>
<% out.println("The number is "); %>
<%! int num12 = 20; int num32 = 22; %>
<%= num12*num32 %>
<br/>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</body>
</html>

 

Output:
 

You can also read about the JSP Architecture and Lifecycle.

Must Read Apache Server

FAQs

  1. What is a session in JSP?
    In JSP, the session is regularly used implicit object of type HttpSession. It is mainly used to approach all user’s data until the user session is active. 
    Three methods that are mainly used in session implicit object are as follows: isNew(), getId(), getAttributeName()
     
  2. Which tag is used for bean development in JSP?
    The jsp:useBean, jsp:getProperty, and jsp:setProperty tags are used for bean development.
     
  3. What is a JSP cookie?
    Cookies are textual information stored by the server on the client-side. They are used to recognize a user on the webserver. Cookies are sent from the server to the client. The client’s local machine contains pieces developed of information called cookies. Further, all these cookies are sent to the server for all the subsequent requests made by the client so that the server can either identify a user or use it for other purposes.

Key Takeaways

We discussed a basic introduction to JSP in this blog, including JSP syntax, its features and the steps to install on your system. JSP is a server-side scripting language used for creating dynamic web pages using java in HTML. JSP comes with several advanced features that help create and maintain a highly scalable web application.

Related Articles:

If you are a beginner and interested in learning other fields related to the web, you can follow our guided path to understand the core subjects of computers and get a good grip on DSA concepts. 

Live masterclass