kids encyclopedia robot

Jakarta Server Pages facts for kids

Kids Encyclopedia Facts
Quick facts for kids
JSP
Filename extension
.jsp, .jspx, .jspf
Internet media type
application/jsp
Developed by Eclipse Foundation
Initial release 1999; 26 years ago (1999)
Latest release
3.1
April 31, 2022; 3 years ago (2022-04-31)
Type of format Dynamic web page
Standard JSR 245
Open format? Yes

Jakarta Server Pages (JSP), also known as JavaServer Pages, is a set of tools that helps software developers build web pages that change. These are called dynamic web pages. Think of a website that shows you different content each time you visit, like a news site with the latest headlines or a shopping site with personalized recommendations. JSP helps create these kinds of pages using languages like HTML and XML.

JSP was first released in 1999. It's similar to other web technologies like PHP, but it uses the Java programming language. To make JSP pages work, you need a special web server program, like Apache Tomcat. This program helps run the Java code on the server.

How JSP Works

JSP Model 2
How JSP pages work together in a web application.

Imagine a JSP page as a blueprint for a web page. When someone asks to see a JSP page, the server doesn't just send the blueprint directly. Instead, the JSP page first gets turned into a special Java program called a servlet. This happens behind the scenes.

Once a JSP page becomes a servlet, the server remembers it. This means the next time someone asks for the same page, the server can use the already-made servlet, which makes things faster.

JSP pages can work on their own, but they are often used as part of a bigger system. In this system, JSP handles what the user sees (like the design and content of the web page). Other parts of the system handle the data (like information from a database) and the logic (like what happens when you click a button). This way, different parts of the website work together smoothly.

JSP lets developers mix Java code with regular web content, like HTML. The server then runs this combined code to create the final web page. The code runs inside a special program called a Java virtual machine (JVM). This JVM helps the code work on different types of computers without needing to be rewritten.

JSP Code: What It Looks Like

JSP pages use special symbols to show where Java code or commands are. These symbols are like secret codes that tell the server what to do.

Directives, Scriptlets, and Expressions

One common symbol is `<% ... %>`. This is called a scriptlet. It holds small pieces of Java code that run when someone asks for the web page.

Another symbol is `<%= ... %>`. This is for expressions. It's used when you want to show the result of a Java calculation directly on the web page. The server replaces this symbol and the code inside it with the actual answer.

There are also directives, which look like `<%@ ... %>`. These give instructions to the server about the page itself, like what language it uses.

Java code doesn't have to be all in one block. You can spread it out across your HTML. For example, you can start a Java `if` statement in one scriptlet, add some HTML, and then finish the `if` statement in another scriptlet. This lets you control which parts of your HTML show up based on your Java code.

Example Code

Here's an example of how you might count to three using JSP:

<p>Counting to three:</p>
<% for (int i=1; i<4; i++) { %>
    <p>This number is <%= i %>.</p>
<% } %>
<p>OK.</p>

When a user sees this page in their web browser, it will look like this:

Counting to three:

This number is 1.

This number is 2.

This number is 3.

OK.

Notice how the Java code makes the sentence "This number is..." appear three times, with a different number each time.

Standard JSP Tags

JSP also has special tags that look a bit like HTML tags but start with `jsp:`. These tags help you do common tasks without writing a lot of Java code.

The useBean Tag

The `jsp:useBean` tag is used to create or find a special Java object called a JavaBean. JavaBeans are like containers for data and actions. They help keep your code organized.

This tag has a few important settings:

  • `id`: This is the name you give to your JavaBean so you can use it later.
  • `class`: This tells the server where to find the JavaBean's code.
  • `scope`: This tells the server how long the JavaBean should be available. There are four main types of scope:

* `page`: The JavaBean is only available on the current web page. * `request`: The JavaBean is available for all parts of the current request (like when you click a link and the server prepares a page). * `session`: The JavaBean is available for a user's entire visit to the website (their "session"). * `application`: The JavaBean is available for everyone using the web application, all the time.

The getProperty and setProperty Tags

Once you have a JavaBean, you can use `jsp:getProperty` to get information from it, and `jsp:setProperty` to put information into it.

Both of these tags use the `name` setting to say which JavaBean they are working with. This `name` must match the `id` you gave the JavaBean with the `jsp:useBean` tag.

Expression Language (EL)

JSP also has something called the Expression Language (EL). It's a simpler way to get data from Java objects.

EL uses a dollar sign and curly braces: `${ ... }`. Inside the braces, you write a short command to get the data you need.

Example EL

If you have a JavaBean named `javabean` and it has a piece of information called `variable`, you can show that information like this: The value of `variable` in the object `javabean` is `${javabean.variable}`.

More Tags: JSTL

Developers can also create their own custom JSP tags. One very popular set of tags is called the Jakarta Standard Tag Library (JSTL).

Jakarta Standard Tag Library

JSTL helps with common tasks in JSP pages, like repeating a section of code (like a "for loop") or showing something only if a certain condition is true (like an "if statement").

The most used part of JSTL is the "core library." You usually tell your JSP page you're using it by adding a special line at the top and giving it a short name, like "c".

JSP Compiler

A JSP compiler is a program that reads your JSP page and turns it into a Java Servlet. This usually happens automatically the first time someone visits your JSP page. Sometimes, developers will "precompile" pages before they are even used. This can make the website run faster.

During development, the server often checks if you've changed your JSP file very often. But on a live website, it checks less often to save computer power.

Why Some Developers Have Concerns

Some experts believe that mixing too much Java code directly into JSP pages can make the code messy and hard to manage. They suggest that the Java code that handles the "thinking" (like calculations or getting data) should be kept separate in Java Servlets. The JSP page should then focus only on showing the "display" part (the HTML). This helps keep things organized.

Early on, some people felt JSP wasn't the best solution, but it has improved a lot over time. Today, there are also other ways to build dynamic web pages, but JSP remains an important technology.

See also

Kids robot.svg In Spanish: JavaServer Pages para niños

Programs that run Servlets and JSPs

  • Apache Tomcat
  • Apache TomEE
  • Jetty (web server)
  • GlassFish
  • Oracle iPlanet Web Server
  • WebSphere Application Server

Other ways to make web pages with Java

  • Adobe ColdFusion
  • Lucee
  • FreeMarker
  • JHTML
  • Thymeleaf
kids search engine
Jakarta Server Pages Facts for Kids. Kiddle Encyclopedia.