What is JSP in Java?

JSP stands for Java Server Pages, otherwise known as Jakarta Server Pages. It is a server-side technology used by web developers to create dynamic web pages that are based on HTML, XML, and other web document types.

JSP may be viewed as an extension or a high-level abstraction of Java servlets, and it has access to the entire API available in Java. JSP is developed using the Java programming language. In JSP, Java codes are embedded into HTML or XML codes using tags. JSP codes are compiled into executable Java Servlets by a program called JavaServer Pages compiler.

Features of JSP

Some features of JSP that make it stand out for the development of web pages are listed below.

  • JSP makes it easier to code with shorter code, by simply using tags and embedding Java codes in HTML and XML codes.

  • We can use implicit objects, predefined tags, expression language, and custom tags in JSP codes due to it being an extension of servlets.

  • JSP makes connecting, reading from, and writing to a database much easier.

  • Web pages written with JSP are browser and server independent.

  • JSP is dynamic, secure, and needs no re-compilation.

  • JSP is easy to maintain.

JSP syntax

Java codes are usually embedded in tags when writing a JSV program. The most basic form of the tag used for enclosing a JSP scriptleta fragment of Java code that runs when the user requests the page is:

 <% javaCode %>

When declaring a variable, we put an exclamation mark right after the opening percentage of the opening tag as below:

<%! String var = 'JSP is fun!'; %>

The result of evaluating an expression is rendered using the expression tag:

<%= expression %>

Comments are included in the program by using a double hyphen after the opening tag and before closing tag:

<%-- this line is commented out --%>

A typical example of a JSP program is shown below.

<p>Hello World! after the countdown from 3.</p>
<% for (int i=3; i>=0; i--) { %>
    <p>count <%= i %>...</p>
<% } %>
<p>Hello World!</p>

The result of the program is:

Hello World! after the countdown from 3.
count 3...
count 2...
count 1...
count 0...
Hello World!