Saturday, September 10, 2022
HomeITWhat's JSP? Introduction to Jakarta Server Pages

What’s JSP? Introduction to Jakarta Server Pages


Jakarta Server Pages (previously JavaServer Pages) is a Java commonplace know-how that builders use to write down dynamic, data-driven internet pages for Java internet functions. JSP is constructed on high of the Java Servlet (aka Jakarta Servlet) specification and is among the Java internet applied sciences included for ongoing assist and upgrades in Jakarta EE.

JSP and servlets usually work collectively, particularly in older Java internet functions. From a coding perspective, the obvious distinction between JSP and servlets is that with servlets you write Java code after which embed client-side markup (like HTML) into that code. With JSP, you begin with the client-side script or markup, then embed JSP tags to attach your web page to the Java again finish.

Consider JSP as a solution to write markup with superpowers for interacting with the again finish. Normally, markup like HTML is shipped to the consumer the place it interacts with the back-end server through JavaScript. JSP pre-processes the HTML with particular instructions to entry and use server capabilities, then sends that compiled web page to the consumer.

JSP and JSF

JSP is intently associated to JSF, or Jakarta Server Faces (previously JavaServer Faces). JSF is a Java specification for constructing model-view-controller (MVC) internet functions. It’s the usual for Java internet frameworks like Eclipse Mojarra, MyFaces, and PrimeFaces. Whereas it is not unusual to see JSP used because the entrance finish for older JSF functions, Facelets is the popular view know-how for contemporary JSF implementations.

Do builders nonetheless use JSP?

Builders do nonetheless use JSP for some functions. It is a easier know-how than extra fashionable approaches like Jamstack, or a template engine like Thymeleaf, however typically easy is the way in which to go.

JSP is a core Java internet know-how. As builders, we are able to construct JSP pages comparatively rapidly and simply, they usually work together seamlessly with servlets in a servlet container like Tomcat. You would say that JSP is the Java ecosystem equal of PHP and ASP within the .NET world.

You’ll encounter JSP in older Java internet functions. Infrequently, you should still discover it helpful for constructing easy, dynamic Java internet pages. As a Java developer, you have to be accustomed to JSP and find out how to use it in your applications.

Writing JSP pages

A easy JSP web page consists of HTML markup embedded with JSP tags. JSP recordsdata have the .jsp extension. The JSP server (additionally known as a JSP container) is configured to direct an HTTP request to a selected JSP web page, which is then chargeable for rendering an HTTP response.

When the request arrives, the file is processed on the server and the HTML is rendered as the appliance view, an internet web page. The embedded JSP tags will probably be used to name server-side code and knowledge. The last word product of the JSP is vanilla HTML that the consumer browser can perceive as-is. The diagram in Determine 1 exhibits the interplay between HTML, JSP, and the online software server.

Diagram of a Jakarta Server Pages (JSP) page in a web application. IDG

Determine 1. Overview of a Jakarta Server Pages (JSP) web page in an internet software.

Itemizing 1 exhibits a easy JSP web page.

Itemizing 1. A easy JSP web page


<html>
  <physique>
    <p>${2 * 2} ought to equal 4</p>
  </physique>
</html>

Right here, you see a block of HTML that features a JSP expression, which is an instruction to the Java server written utilizing Expression Language (EL). Within the expression ${2 * 2}, the ${} is JSP syntax for interpolating code into HTML. The greenback signal with curly braces tells the JSP processor: “Maintain on for a second, we’re about to begin speaking about one thing particular you’ll want to handle.”  When executed, the JSP web page will output the outcomes of executing no matter is contained in the expression. On this case, the output would be the quantity 4.

JSP within the servlet container

JSP pages have to be deployed inside a servlet container. With the intention to deploy a Java internet software primarily based on JSP and servlets, you’ll package deal your .jsp recordsdata, Java code, and software metadata in a .conflict file, which is a straightforward .zip file with a traditional construction for internet functions.

As soon as you have loaded the JSP into your servlet container, will probably be compiled right into a servlet. JSPs and servlets share comparable traits, together with the power to entry and reply to request objects.

Tips on how to use JSP with Tomcat

We’ll use an instance software in Tomcat to get you began with Jakarta Server Pages. Should you do not have already got Tomcat put in, browse over to the Tomcat obtain web page and choose the Tomcat set up on your working system. As of this writing, Tomcat 10 is the present launch, suitable with Jakarta Servlet 6 and Jakarta Server Pages 3.1.

You possibly can set up Tomcat as a Home windows service, or run it from the command line with /bin/catalina.sh begin or /bin/catalina.bat. Both approach, begin up Tomcat, then go to localhost:8080 to see the Tomcat welcome web page proven in Determine 2.

jsp tomcat welcome page IDG

Determine 2. The Tomcat welcome web page.

Implicit objects in JSP

On the Tomcat welcome web page, click on the Examples hyperlink, then click on JSP Examples.

Subsequent, open the Implicit Objects Execute internet software. Determine 3 exhibits this instance software’s output. Take a minute to review the output. The weather it describes are at all times accessible by default inside a JSP web page.

Sample output for a JSP page. IDG

Determine 3. Pattern output for a JSP web page.

Notice that no code modifications are required between JSP 2.0 and JSP 3.0, which is the present replace for Jakarta EE. The Implicit Object instance makes use of JSP 2.0.

Request parameters

Implicit objects are built-in objects accessible through a JSP web page. When you find yourself growing an internet web page, you’ll use these objects to entry issues like request parameters, that are the information despatched over from the consumer browser when issuing an HTTP request.

Take into account the browser URL for the Implicit Objects software: http://localhost:8080/examples/jsp/jsp2/el/implicit-objects.jsp?foo=bar.

Notice that there’s nothing particular about this web page; we’re simply utilizing it to see how the URL parameter is accessed. The param is ?foo=bar, and you may see it mirrored within the output on the net web page, the place the desk exhibits EL Expression and the worth is bar. To check this out, change the URL to http://localhost:8080/examples/jsp/jsp2/el/implicit-objects.jsp?foo=zork, hit Enter, and you will see the change mirrored within the output.

This instance is a quite simple introduction to utilizing JSP tags to entry server-side request parameters. On this case, the JSP web page makes use of the built-in (implicit) object known as param to entry the online software’s request parameters. The param object is obtainable contained in the JSP expression syntax that you simply noticed in Itemizing 1. In that instance, we used an expression to do some math: ${2 * 2}, which resulted within the output of 4. On this case, the expression is used to entry an object and a discipline on that object: ${param.foo}. Subsequent, we’ll look extra intently on the Implicit Objects instance.

JSP in an internet software

On the Implicit Objects web page, click on the again arrow, adopted by the Supply hyperlink. This can lead you to the JSP code for the Implicit Objects internet software, which is proven in Itemizing 2.

Itemizing 2. JSP code for the Implicit Objects internet software


<%@web page contentType="textual content/html; charset=UTF-8" %>
<%@ taglib prefix="fn" uri="http://java.solar.com/jsp/jstl/capabilities" %>

<html>
  <head>
    <title>JSP 2.0 Expression Language - Implicit Objects</title>
  </head>
  <physique>
    <h1>JSP 2.0 Expression Language - Implicit Objects</h1>
    <hr>
    This instance illustrates a number of the implicit objects accessible
    within the Expression Language.  The next implicit objects are
    accessible (not all illustrated right here):
    <ul>
      <li>pageContext - the PageContext object</li>
      <li>pageScope - a Map that maps page-scoped attribute names to
          their values</li>
      <li>requestScope - a Map that maps request-scoped attribute names
          to their values</li>
      <li>sessionScope - a Map that maps session-scoped attribute names
          to their values</li>
      <li>applicationScope - a Map that maps application-scoped attribute
          names to their values</li>
      <li>param - a Map that maps parameter names to a single String
          parameter worth</li>
      <li>paramValues - a Map that maps parameter names to a String[] of
          all values for that parameter</li>
      <li>header - a Map that maps header names to a single String
          header worth</li>
      <li>headerValues - a Map that maps header names to a String[] of
          all values for that header</li>
      <li>initParam - a Map that maps context initialization parameter
          names to their String parameter worth</li>
      <li>cookie - a Map that maps cookie names to a single Cookie object.</li>
    </ul>

    <blockquote>
      <u><b>Change Parameter</b></u>
      <kind motion="implicit-objects.jsp" technique="GET">
          foo = <enter sort="textual content" identify="foo" worth="${fn:escapeXml(param["foo"])}">
          <enter sort="submit">
      </kind>
      <br>
      <code>
        <desk border="1">
          <thead>
            <td><b>EL Expression</b></td>
            <td><b>Outcome</b></td>
          </thead>
          <tr>
            <td>${param.foo}</td>
            <td>${fn:escapeXml(param["foo"])}&nbsp;</td>
          </tr>
          <tr>
            <td>${param["foo"]}</td>
            <td>${fn:escapeXml(param["foo"])}&nbsp;</td>
          </tr>
          <tr>
            <td>${header["host"]}</td>
            <td>${fn:escapeXml(header["host"])}&nbsp;</td>
          </tr>
          <tr>
            <td>${header["accept"]}</td>
            <td>${fn:escapeXml(header["accept"])}&nbsp;</td>
          </tr>
          <tr>
            <td>${header["user-agent"]}</td>
            <td>${fn:escapeXml(header["user-agent"])}&nbsp;</td>
          </tr>
        </desk>
      </code>
    </blockquote>
  </physique>
</html>

JSP capabilities

Should you’re accustomed to HTML, then the code in Itemizing 2 ought to be pretty understandable. You’ve the anticipated HTML <td> components, adopted by the ${ } JSP expression syntax launched in Itemizing 1. However discover the worth for param.foo:


<td>${fn:escapeXml(param["foo"])}&nbsp;</td>

Right here, the fn:escapeXML() is a JSP perform.

A JSP perform encapsulates a piece of reusable performance. On this case, the performance is to flee XML. JSP affords a wide range of capabilities, and you can too create capabilities your self. To make use of a perform, you import its library into your JSP web page, then name the perform.

In Itemizing 2, we embrace the escapeXML perform with the next line:


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

The syntax is fairly clear: it imports the required capabilities and assigns them a prefix (on this case “fn“) that can be utilized in all following expressions.

The Jakarta Customary Tag Library (JSTL)

The import line in Itemizing 2 calls taglib, which is brief for tag library. On this case, we have imported the Jakarta Customary Tag Library (JSTL). Tag libraries outline reusable bits of performance for JSP. JSTL is the usual tag library, containing a set of taglibs that ship with each servlet and JSP implementation, together with Tomcat.

The “capabilities” library is simply one of many taglibs included with JSTL. One other widespread taglib is the core library, which you import by calling:


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

Like "fn", the "c" designation is typical; you will note it throughout most JSP pages.

Securing JSP pages with JSTL

The next instance tag is from the JSTL core library:


<c:out worth = "${'<div>'}"/>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments