Introduction to JSP

JSP stands for JavaServer Pages. JSP is one of the most powerful, easy-to-use and fundamental technology 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 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 client-server model, which a client (web browser) makes a request to the server and get a response accordingly. The protocol that the web server and web browser use to communicate is HTTP protocol, which works based on request/response model. The current version of 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 of 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 it out 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, in order to build Java web application using Java servlet, web developers have to understand Java programming language deeply, which not many 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 is in charge of translating the JSP page into a Servlet, which is 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’s 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>

As mentioned in the Servlet section, the JSP is translated into a Servlet in order to processing requests. For detailed information how the process works, check it out the understand JSP life cycle tutorial.

In this tutorial, we have briefly shown you how HTTP protocol works, what is Java Servlet, 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 detail in the subsequent tutorials.