What is JSP? Introduction to Jakarta Server Pages

One of the original Java web technologies, JSP is still widely used with servlets and JSTL. Here's how to use Jakarta Server Pages to build dynamic web pages that connect to the Java back end.

1 2 Page 2
Page 2 of 2

This tag outputs the <div> tag with the XML already escaped. The function is important because outputting content directly to a web page via ${variable} opens the door to script injection attacks. This simple function is used to protect web pages from such attacks.

The core library also includes various tags for iteration and flow control (like "if ... else" handling).

Calling taglibs in JSP pages

Now that you've got a handle on JSP basics, let's make a change to the example application. To start, locate the Implicit Objects application in your Tomcat installation. The path is: apache-tomcat-8.5.33/webapps/examples/jsp/jsp2/el.

Open this file and locate the functions include:


<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Just below this line, add a new line:


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Now, hit Return and add another new line:


<c:out value = "${'This is a test of the JSTL Core Library'}"/>

Reload the page at http://localhost:8080/examples/jsp/jsp2/el/implicit-objects.jsp?foo=bar.

You should see your updates reflected in the output. In this example, we are just using the tag library to output text, something we could also do with a JSP expression. Tags open up a range of further capabilities with the same syntax. 

Layout of a JSP web application

Tomcat follows the standard layout for Java servlet and JSP pages. The general structure is a folder with the name of the application, which is deployed inside Tomcat's /webapps directory.

In the Implicit Objects application, the application is /examples. Opening that folder reveals three important children: the /WEB-INF directory, the index.html file, and a set of other directories.

The WEB-INF directory contains metadata that informs Tomcat how to run the application.

The remaining content, including index.html, is directly accessible, just as it would be in a typical web server. Likewise, the implicit-objects.jsp is available to view, and is automatically handled as a JSP page (not an HTML page) by the server.

JSP's request-response architecture

In a typical servlet/JSP application, servlets receive requests and interact with server-side components and services (such as the database) to fulfill them. Once handled, the request result is forwarded to a JSP page, which presents the response based on the data.

A servlet uses the following syntax when sending a request response to a JSP page:


request.getRequestDispatcher("path/to/jsp.jsp").forward(req,res);

Whatever data the servlet has inserted into the various JSP scopes will be accessible to JSP as objects, like the param object you saw earlier. A scope is simply a range of life for a given object. Request and response objects are request scoped, meaning they live for the duration of the request. Other scopes include page scope (living as long as the JSP page does) and session scope (terminating when the user ends the given session).

MVC in JSP and servlet applications

The interaction between servlets and JSP pages follows the classic MVC pattern: the controller object (a servlet) prepares the model object (an item or user object) and sends it to the view (the JSP page) for rendering. MVC's clean separation of concerns has made it a popular and long-lived approach to building software. Struts is one of the oldest and most well-known frameworks implementing MVC with JSP and servlets. Spring MVC also includes built-in support for JSP.

Using Spring MVC is probably the most common approach to JSP development within a larger framework, since it gives you access to the entire Spring ecosystem. A good way to accelerate the process is use a JSP Spring Boot template.

Conclusion

JSP is a well-known and versatile technology for developing Java web application views. Combined with servlets, JSP pages are very powerful and provide access to the entire range of Java capabilities. While Jakarta Server Pages may not be your first choice for developing a modern Java web application, you will encounter JSP pages in legacy applications. It's also a good choice for simpler applications, where you want to quickly connect your HTML front end to server-side Java code. The JSTL tags are capable of handling most web application requirements, and third-party libraries extend that functionality for specific technologies and use cases.

Copyright © 2022 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2