Java Server Pages (JSP)

Anukrishna T
5 min readMay 21, 2023

--

Jakarta Server Pages (JSP; formerly JavaServer Pages) helps software developers to create dynamically generated web pages based on HTML, XML, or other document types.

A website is a webpage collection; each is formed in static or dynamic ways. A website works on the basis of a client-server paradigm where the client sends the request to the server and the server responds according to the request. This communication is done using the HTTP protocol.

What is Servlet?

Servlets are server-side Java programs that act as an interface between the client request and the application hosted on the server. It collects the information from the user through login forms or tables, creates dynamic webpages, and presents the result. Servlets can respond to any request, and they are commonly used to extend the applications hosted by web servers.

Servlet Life Cycle

  • Load Servlet Class — When a server starts up, the servlet container deploy and loads all the servlets.
  • Create Servlet Instance — Create an instance of Servlet.
  • Call the init() method Servlet.init() method is called by the Servlet container to notify that this instance is instantiated successfully and is about to be put into service.
  • Call the service() method — The servlet calls the service() method to process a client’s request and is invoked to inform the Servlet about the client's requests.
  • Call the destroy() method — A servlet is terminated by calling the destroy(). The destroy() method runs only once during the lifetime of a servlet.

We can create servlets in Java by following the way,

  • Create a Java class that extends the javax.servlet.http.HttpServlet class or implements the javax.servlet.Servlet interface.
  • Override the appropriate methods based on the HTTP request method you want to handle. For example, doGet() to handle GET requests and doPost() to handle POST requests.
  • Otherwise, use the service() method to implement the logic.
  • Configure the servlet mapping in the web.xml deployment descriptor file or use annotations if your servlet container supports them. This mapping associates a URL pattern with your servlet.

Java Server Pages (JSP)

Java Server Pages is an extensive collection of technologies, which are used to develop web pages. These web pages are developed by inserting Java codes into HTML pages with JSP tags. The collection can contain HTML, XML, or both with JSP actions and commands.

JSP contains static as well as dynamic data, where dynamic data can be JSP elements, whereas static data can be HTML, XML, SVG, or WYML files. Servlets generate dynamic content, interact with the client, and are maintained by Servlet engine containers.

Difference between JSP and Servlet

JSP Lifecycle

The following are the paths followed by a JSP,

  • Compilation- Whenever the browser asks for JSP, the engine first checks to see whether it needs to compile the page. If it is not compiled, then it goes for compilation.
  • Initialization- When a container loads or compiles a JSP it invokes the jspInit() method before servicing any requests.
  • Execution- This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed. Once it is loaded and initialized, then the engine will invoke the _jspService() method for executing the logic.
  • Cleanup- The last phase named the cleanup or destruction phase of the life cycle will remove JSP from use by a container with the help of the jspDestroy() method.

JSP Scripting Elements

As mentioned earlier, JSP are developed by inserting Java codes into HTML pages with JSP tags or scripting elements. There are four types of scripting elements:

Scriptlet tag — A scriptlet tag is used to execute Java source code in JSP. Syntax : <% java source code %>

Everything inside the scriptlet tag is considered as the code inside service() method in the servlet.

Expression tag — The code placed within the JSP expression tag is written to the output stream of the response. So you need not write out. print() to write data. Syntax : <%= statement %>

Declaration tag — The JSP declaration tag is used to declare fields and methods. The code written inside the JSP declaration tag is placed outside the service() method. Syntax: <%! field or method declaration %>

Directive Tag — The page directive defines attributes that apply to an entire JSP page. Syntax: <%@ page attribute=”value” %>

JSP Directives

JSP directives are the messages to the JSP container. They provide global information about an entire JSP page.

There are three types of directives:

  1. Page directive — It is used to provide instructions to a container that pertains to the current JSP page.
  2. Include directive — JSP include directive is used to include one file to another file. This included file can be HTML, JSP, text files, etc.
  3. Taglib directive — JSP taglib directive is used to define the tag library with taglib as the prefix, which we can use in JSP. More detail will be covered in the JSP Custom Tags section.

Advantages of JSP

  1. JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS-specific languages, so it is more powerful and easier to use.
  2. It is more convenient to write/modify regular HTML than to have plenty of println statements that generate the HTML.
  3. JSP allows the creation of reusable components through the use of custom tags and tag libraries. These components can be shared across multiple pages.
  4. JSP pages are compiled into servlets, which are then executed by the server. This compilation process improves performance compared to interpreted scripting languages.
  5. JSP is platform-independent and can be deployed on any server that supports the Java Servlet specification.

MVC in JSP

MVC stands for Model View and Controller which is a design pattern that separates the business logic, presentation logic, and data. The controller acts as an interface between View and Model and it intercepts all incoming requests. The model represents the state of the application or we can call it as data. It can also have business logic. View represents the presentation, that is the User Interface.

JSP and Servlets are often used together in web applications. Servlets handle the control and business logic, while JSP is used for rendering the dynamic content and presenting the data to the user. This combination promotes code reuse and maintainability.

--

--