The Wayback Machine - https://web.archive.org/web/20240920223613/https://www.geeksforgeeks.org/introduction-to-jsp/
Open In App

Introduction to JSP

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Java, JSP stands for Jakarta Server Pages( (JSP; formerly JavaServer Pages)). It is a server-side technology that is used for creating web applications. It is used to create dynamic web content. JSP consists of both HTML tags and JSP tags. In this, JSP tags are used to insert JAVA code into HTML pages. It is an advanced version of Servlet Technology i.e. a web-based technology that helps us to create dynamic and platform-independent web pages. In this, Java code can be inserted in HTML/ XML pages or both. JSP is first converted into a servlet by the JSP container before processing the client’s request. JSP has various features like JSP Expressions, JSP tags, JSP Expression Language, etc.

How is JSP more Advantageous than Servlet?

  • They are easy to maintain.
  • No recompilation or redeployment is required.
  • Less coding is required in JSP.
  • JSP has access to the entire API of JAVA.
  • JSP are extended version of Servlet.

Features of JSP

  • Coding in JSP is Easy: As it involves adding Java code to HTML/XML.
  • Easy to Use and Learn: It is straightforward and accessible for both Java and non-Java programmers.
  • It Does Not Require Advanced Knowledge of Java: Suitable for users with basic Java skills.
  • Reduction in the Length of Code: JSP uses action tags, custom tags, etc., to minimize code length.
  • Java Scriplets: Allows embedding Java code, variables, and expressions within JSP pages.
  • JSP Expression: Evaluates expressions and converts them to strings.
  • Declaration Tag: Used to declare variables and methods within JSP pages.
  • Implicit Objects: Provides built-in objects that reduce the length of code.
  • Make Interactive Websites: Facilitates the creation of dynamic web pages that interact with users in real-time.
  • Connection to Database is Easier: Simplifies connecting to databases and allows for easy data reading and writing.
  • Extension to Servlet: Inherits all features of servlets and includes implicit objects and custom tags.
  • Portable, Powerful, Flexible, and Easy to Maintain: Browser and server independent, making it versatile and easy to manage.
  • No Redeployment and No Re-Compilation: JSP is dynamic, secure, and platform-independent, so no need for redeployment or recompilation.
  • Create HTML Page from Where Request Will Be Sent: For example, try.html for sending requests to the server.
  • To Handle User Requests, Create .jsp File: For instance, new.jsp for handling requests.
  • Create Project Folder Structure: Organize files and resources into a project structure.
  • Create XML File: For configuration or data, e.g., my.xml.
  • Create WAR File: Package the application into a WAR (Web Application Archive) file for deployment.
  • Start Tomcat: Ensure the servlet container (Tomcat) is running.
  • Run Application: Deploy and test the JSP application.
  • It is Capable of Handling Exceptions: JSP can manage errors and exceptions effectively.
  • Difficult to Debug for Errors: Debugging JSP pages can be challenging due to their mixed nature of HTML and Java code.
  • First-Time Access Leads to Wastage of Time: Initial access can be slower as JSP pages need to be compiled.
  • Output is HTML Which Lacks Features: JSP generates HTML, which may not support advanced features.

Creating a simple JSP Page

hello.JSP :

JSP simply puts Java inside HTML pages. You can take any existing HTML page and change its extension to “.jsp” instead of “.html”. In fact, this is the perfect exercise for your first JSP.

Take the HTML file you used in the previous exercise. change its extension from “.html” to  “jsp”. Now load the new file, with the “.jsp” extension, in your browser.

You will see the same output, but it will take longer! But only the first time. If you reload it again, it will load normally.

What is happening behind the scenes is that your JSP is being turned into a Java file, compiled, and loaded. This compilation only happens once, so after the first load, the file doesn’t take long to load anymore. (But every time  you change the JSP file, it will be re-compiled again.)

Of course, it is not very useful to just write HTML pages with a .jsp extension! We now proceed to see what makes JSP so useful.

Adding dynamic content via expressions:

As we saw in the previous section, any HTML file can be turned into a JSP file by changing its extension to .jsp . Of course , what makes JSP useful is the ability to embed Java. Put the following text in a file. jsp extension (let us call it hello.jsp) , place it in your JSP directory, and view it in a browser.

<HTML>
<BODY>
Hello! The time is now <%= new java.util.Date() %>
</BODY>
</HTML>

Notice that each time you reload the page in the browser, it comes up with the current time. The character sequence.

<%= and %> enclose Java expressions, which are evaluated at run time.

This is what makes it possible to use JSP to generate dynamic HTML pages that change in response to user actions or vary from user to user.

Explain JSP Elements:

We will learn about the various elements available in JSP with suitable examples. In JSP elements can be divided into 4 different types.

These are:

  •     Expression
  •     Scriplets
  •     Directives
  •     Declarations

Expression:

We can use this tag to output any data on the generated page. These data are automatically converted to string and printed on the output stream.

Syntax:

JSP Expressions are : <%="Anything" %>

NOTE : JSP Expressions start with Syntax of JSP Scriptles are with <%=and ends with %>.  Between these, you can put anything that will convert to the String and that will be displayed.

Example:

<%="HelloWorld!" %>   

Scriplets:

In this tag we can insert any amount of valid java code and these codes are placed in the _jsp Service method by the JSP engine.

Syntax:

<%//java codes%>

NOTE : JSP Scriptlets begins with <% and ends %> . We can embed any amount of Java code in the JSP Scriptlets. JSP Engine places these codes in the _jspService() method.

Variables available to the JSP Scriptlets are:

  •  Request
  •  Response
  •  Session
  •  Out

Directives:

A JSP “directive” starts with <%@ characters. In the directives, we can import  packages , and define error-handling pages or the session information of the JSP page.

Syntax:

<%@directive attribute="value"% >  
  •  page
  •  include
  •  taglib    

Declarations :

This tag is used for defining the functions and variables to be used in the JSP.

Syntax:

<%!
//java codes
%>

NOTE : JSP Declaratives begins with <%! and ends %> with We can embed any amount of java code in the JSP Declaratives. Variables and functions defined in the declaratives are class-level and can be used anywhere on the JSP page. 

Example :

<%@page import="java.util.*"%>
<HTML>
<BODY>
<%!
Date theDate = new Date(); // Corrected the unwanted space in the declaration
Date getDate() {
System.out.println("In getDate() method");
return theDate;
}
%>
Hello! The time is now <%=getDate()%>
</BODY>
</HTML>

Example of a JSP Web Page:

 <HTML>
<HEAD>
<TITLE>A Web Page</TITLE>
</HEAD>
<BODY>
<%out.println("Hello there!");%>
</BODY>
</HTML>

Run a Simple JSP Page:

  • Step 1: Save the JSP file using “.jsp” extension (ex- “hello.jsp”)
  • Step 2: Start the server
  • Step 3: Place your application inside a folder
  • Step 4: To execute the JSP script, simply start tomcat server and use a browser to browse an URL of the JSP page i.e. http://localhost:portnumber/YourApplicationContextRoot/jspfile then you will see the jsp file is being compiled.


Previous Article
Next Article

Similar Reads

Environment setup for JSP
The environment setup for JSP mainly consists of 3 steps: Setting up the JDK. Setting up the webserver (Tomcat). Starting tomcat server. All the steps are discussed in details below: Setting up Java Development Kit : Step 1: This step involves downloading JDK from Download JDK. Step 2: Setting up the PATH environment variable appropriately. For win
2 min read
Life cycle of JSP
A Java Server Page life cycle is defined as the process that started with its creation which later translated to a servlet and afterward servlet lifecycle comes into play. This is how the process goes on until its destruction. Following steps are involved in the JSP life cycle: Translation of JSP page to ServletCompilation of JSP page(Compilation o
2 min read
Difference between Servlet and JSP
Brief Introduction: Servlet technology is used to create a web application. A servlet is a Java class that is used to extend the capabilities of servers that host applications accessed by means of a request-response model. Servlets are mainly used to extend the applications hosted by web services. JSP is used to create web applications just like Se
3 min read
JSP | Implicit Objects - request and response
Perquisite: Introduction To JSPJSP stands for Java Server Pages, and it's a server side technology. It's used for creating web applications and dynamic web content. The main property of JSP is that we can insert our java code inside our HTML page using JSP tag. JSP provides you platform-independent pages. C/C++ Code &lt;%@ page language = &quot;jav
4 min read
Difference between JSP and ASP
JSP stands for Java Server Pages, a server-side scripting language which helps developers to create dynamic web pages based on HTML, XML or other types. It was created by Sun Micro systems. It's also similar to the ASP and PHP but it uses Java programming languages and it has the full access to Java APIs as well as databases. ASP stands for Active
2 min read
JSP Access Model
JSP is java based technology which is used to simplify the development of the dynamic webpage. JSP is used to separate the dynamic content of the web page from its content with the help of HTML and JSP tags. There are two JSP Model Architecture: JSP Model 1 Architecture: JSP Model1 Architecture or JSP Centric architecture contains JavaBeans or EJB
3 min read
How to display search result of another page on same page using ajax in JSP?
In this example, we are creating a search bar to display the result on the same page in using ajax in JSP. AJAX stands for Asynchronous JavaScript and XML. Where Ajax is mainly used for Displaying another web page content on the same web page without refreshment of the page. You can read more about ajax technology here. Approach: We use the JQuery
2 min read
Classes of JSP API
JSP API is a set of classes and interfaces that can be used to make a JSP page. These classes and interfaces are contained in the javax servlet.jsp packages. A portion of the classes characterized in the javax. servlet.jsp packages are: Error DataJSP WriterPage Context The Error Data Class: The error information class characterizes error data for e
4 min read
Difference Between JSP and PHP
JSP was an implies of giving a comparable programming fashion to PHP and ASP. It is based on Java Servlets and requires a Servlet holder server like Tomcat to supply the backend preparation required to change over the JSP to a servlet that can yield HTML. In differentiating PHP can run on its possess as a CGI motor, or as a coordinates module in nu
4 min read
What is JSP Filters?
JSP(Java Server Pages) is a server-side technology used to create web applications in Java. To manipulate the server's response and to intercept the requests from a client, JSP has its proprietary filters that are used with the help of a java class. At the same time, the filters are used to authenticate the credentials of the user and to perform da
4 min read
Difference between JavaScript and JSP
JavaScript is a lightweight and object-oriented scripting language used to create dynamic HTML pages with interactive effects within a webpage. It is an interpreted scripting language and its code is run in a web browser. It is also called a browser’s language and can be used for client-side developments as well as server-side developments. It was
3 min read
JSP Architecture
JSP architecture gives a high-level view of the working of JSP. JSP architecture is a 3 tier architecture. It has a Client, Web Server, and Database. The client is the web browser or application on the user side. Web Server uses a JSP Engine i.e; a container that processes JSP. For example, Apache Tomcat has a built-in JSP Engine. JSP Engine interc
3 min read
Creating JSP in Eclipse IDE with Tomcat Server
Eclipse IDE is an open-source Integrated Development Environment that is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. JSP stands for java server pages. It is a server side technology been used for creating web a
3 min read
JSP Session - Implicit Object
In JSP, the session is the most regularly used implicit object of type HttpSession. It is mainly used to approach all data of the user until the user session is active. Methods used in session Implicit Object are as follows: Method 1: isNew(): This method is used to check either the session is new or not. Boolean value (true or false) is returned b
3 min read
JSP Config - Implicit Objects
JSP Config is an implicit object which is used to transmit the configuration details to the JSP page. In JSP, Config is an instance of type ServletConfig. This implicit object is used to acquire an initialization parameter for a certain JSP page. For each JSP page, the config object is generated through the web container. JSP's config object carrie
2 min read
JSP PageContext - Implicit Objects
PageContext extends JspContext to contribute helpful context details while JSP technology is applied in a Servlet environment. A PageContext is an instance that gives access to all the namespaces related to a JSP page, gives access to some page attributes and a layer over the application details. Implicit objects are connected to the pageContext co
4 min read
JSP Application - Implicit Objects
In JSP, application is an implicit object of type ServletContext. This is an instance of javax.servlet.ServletContext. It is generated onetime by the web container when web application or web project is deployed on server. This object is used to acquire the initialization parameter from the configuration file (web.xml). It is used for allotting the
3 min read
What is JSP Page?
A JSP page is a normal web page with JSP elements for generating the parts of the web page that differ for each request. A simple JSP web page that contains the JSP elements and template text. Everything on the page that is not a JSP element is called Template text. Template text can be any text, i.e., HTML, XML, WML or even plain text.JSP has no d
3 min read
Custom Tags in JSP
Custom tags are user-defined action tags that can be used within Java Server Pages. A tag handler is associated with each tag to implement the operations. Therefore, it separates the business logic from JSP and helps avoid the use of scriptlet tags. The scriptlet tag embeds java code inside the JSP page itself rendering the page difficult to unders
8 min read
Custom Tags with Body in JSP
A custom tag can be defined with its body. Body of the tag can be skipped or evaluated as per the requirement. Syntax of custom tags along with its body is given below: &lt;prefix : suffix&gt; body -content &lt;/prefix : suffix&gt; To create custom tags with the body, the given steps need to be followed. 1. Create tag handler: Implement one of the
6 min read
JSP | Declaration Tag
Declaration tag is one of the scripting elements in JSP.This Tag is used for declare the variables. Along with this, Declaration Tag can also declare method and classes. Jsp initializer scans the code and find the declaration tag and initializes all the variables, methods, and classes. JSP container keeps this code outside of the service method (_J
2 min read
JSP | ScriptletTag
Java Server Page (JSP) is a technology for controlling the content or appearance of Web pages through the use of servlets. Small programs that are specified in the Web page and run on the Web server to modify the Web page before it is sent to the user who requested it. There are total three Scripting Element in JSP Scriptlet tagExpression tagDeclar
2 min read
JSTL | JSP Standard Tag Library
Introduction: Initially, Web designers used scriptlets in JSP pages to generate dynamic content. This resulted in readability issues and also made it difficult to maintain the JSP page. Custom tags were introduced to overcome the problems faced in using scriptlets. Although custom tags proved to be a better choice than scriptlets they had certain l
4 min read
JSP Full form
JSP stands for Jakarta Server Pages (formerly JavaServer Pages). It is a programming tool that is used on the Application Server Side to create dynamic web pages. JSP basically used to support Platform–Independent and Dynamic methods to build web-dependent applications based on HTML, XML, SOAP, etc. JSP allows developers to embed Java code as well
3 min read
Expression Language in JSP
Expression Language was introduced in JSP 2.0, it is a simplified language that is used to access and manipulate data within JSP pages also it provides a convenient way to access the data stored in objects like requests, sessions, applications, JavaBeans, etc. We know there are n number of objects that store data, and if we want to retrieve that da
4 min read
JSP - Custom URI in Custom Tag
JavaServer Pages (JSP) provide a dynamic environment for developing various online applications. Setting the custom Uniform Resource Identifiers (URIs) for custom tags opens up the way for management and organization. While the default pairing of JSP custom tags with URIs based on Java package structures lays the groundwork. Analysis of a few Aspec
6 min read
JSP - File Downloading
In this article, we will learn how to download files from the server/file directories using JSP. The JSP code has a response header indicating that text is being returned that should be downloaded rather than displayed. We will set the content type header to "APPLICATION/OCTET-STREAM", and it will indicate that the content is binary data. The FileI
3 min read
JSP - HTTP Status Codes
When the Client makes any requests to the server, the Status Codes are issued by the server as a response to the client's request. So, in an application, we have the client and the server architecture. The server is the part that holds the particular web service or an API. The client is the actor who sends that HTTP request to the server. So, whene
4 min read
JSP - Form Processing
In JavaServer Pages, Form processing is the fundamental aspect of web development, and it can allow users to interact with websites by submitting data. It can handle form submissions involving extracting the data from the HTTP requests and generating the appropriate responses. Form Processing in JSPForm processing in the JavaServer Pages involves r
8 min read
JSP - Server Response
In JavaServer Pages development, the server response refers to the data sent back to the client from the server after receiving and processing the client request, and the server response typically includes the dynamically generated by the HTML, XML, JSON, or other based on the JSP file logic and data. Key TerminologiesServer: The server is a comput
7 min read
Article Tags :