Introduction to JSP

JSP stands for JavaServer Pages. JSP is one of the most powerful, easy-to-use, and fundamental technologies for building Java web applications. JSP combines HTML, XML, Java Servlet, and JavaBeans technologies into one highly productive technology that allows web developers to develop reliable, high-performance, and platform-independent web applications and dynamic websites.

There are two important technologies that you need to understand before you can dive into JSP:

  • First, JSP replies on the HTTP protocol to provide client-server communication over the network.
  • Second, JSP is built on top of Java servlet technology to simplify the development tasks for web developers. We will give you an overview of these technologies in the next sections.

HTTP Protocol

JSP web application uses a client-server model, in which a client (web browser) makes a request to the server and gets a response accordingly.

The protocol that the web server and web browser use to communicate is the HTTP protocol, which works based on the request/response model.

The current version of the HTTP protocol at the time of writing is 1.1, which provides the HTTP commands: HEAD, GET, PUT, POST, DELETE, TRACE, OPTIONS, and CONNECT.

As a web developer, you mostly work with the GET and POST commands because the clients (web browsers) use these commands to make requests.

The following picture demonstrates client-server communication using HTTP protocol:

HTTP Model
HTTP Model

HTTP protocol is stateless. It means that no information or state is carried over between subsequent requests from a client.  To overcome the stateless HTTP protocol, the server uses sessions to keep information between two subsequent requests of the same client. For detailed information on how JSP handles the session, check out the JSP session tutorial.

Java Servlet

Before JSP, Java Servlet technology was used to develop Java web applications. Java servlet is a Java class that extends the functionality of a web server. Java Servlet is responsible for handling incoming requests and dynamically generating responses.

Java servlet is powerful. However, to build a Java web application using Java servlet, web developers must understand Java programming language deeply, which few early web developers could.

To make it easier, JSP was designed to overcome the difficulty while still providing the full power of Java servlet. JSP was built on top of Java Servlet. Behind the scenes, JSP is translated into a server before the web server can process it.

In the web application server, a web container or servlet container translates the JSP page into a Servlet, a Java source code file that implements the javax.servlet.http.HttpServlet class by default. The servlet class is compiled into a Java class file, which can be used to process web client requests.

What is JSP

JSP is a text file that contains HTML code-mixing with JSP elements. The JSP elements provide the logic for generating dynamic content based on requests.

The following is the preview of a simple JSP page. You don’t need to understand everything in the code, just take a look to get the idea.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            int x = 10;
            out.println(x + 10);
            String msg = "Just an introduction to JSP";
            out.println(msg);
        %>
    </body>
</html>Code language: HTML, XML (xml)

As mentioned in the Servlet section, the JSP is translated into a Servlet to process requests. Check out the understand JSP life cycle tutorial for detailed information on how the process works.

In this tutorial, we have briefly shown you how HTTP protocol works, what Java Servlet is, and how JSP was built on top of Java Servlet technology. With this short introduction, we hope you get the big picture of JSP before going into the details in the subsequent tutorials.