Introduction to JSP
Learn what JavaServer Pages (JSP) is, how it lets you embed Java inside HTML, and see a first taste of JSP code.
Introduction
Before frameworks like Spring MVC and React existed, Java developers needed a simple way to generate web pages whose content changed based on data - a list of products, a logged-in user's name, the result of a database query. Writing raw HTML by hand could not do this because HTML is static. JavaServer Pages, or JSP, was Java's answer to this problem: a technology that lets you write mostly ordinary HTML and drop in small pieces of Java code exactly where the page needs to be dynamic.
JSP has been running in production systems for over two decades, and a huge number of enterprise Java applications - banking portals, insurance systems, internal company tools - are still built on it today. Learning JSP is not just a history lesson; it is a practical skill for anyone who will work with legacy Java web applications or wants to understand the foundation that later technologies like Spring MVC were built on top of.
- What JSP is and the problem it solves.
- How JSP mixes HTML and Java in a single file.
- What a JSP file looks like and how it is served.
- Where JSP sits in the Java web development ecosystem.
What is JSP?
JSP (JavaServer Pages) is a server-side technology that lets you create dynamically generated web pages using HTML, XML, or other document types combined with Java code. A JSP file has a .jsp extension and lives on a Java web server such as Apache Tomcat. When a browser requests a .jsp page, the server runs any embedded Java logic, produces plain HTML as output, and sends that HTML back to the browser. The browser never sees Java - it only ever receives the final HTML.
This matters because it means JSP pages behave like normal web pages from the browser's point of view, while giving the developer the full power of the Java language on the server to decide what that HTML should contain.
HTML with Embedded Java
The core idea of JSP is simple: you write a file that looks like an HTML page, and anywhere you need dynamic behavior, you insert a special tag that contains Java code. The server processes those tags and replaces them with the Java code's output before sending the page to the browser. Everything outside those special tags is passed through untouched, exactly as written.
| Tag Syntax | Purpose |
|---|---|
| <% ... %> | Scriptlet - a block of Java code |
| <%= ... %> | Expression - prints a Java value directly into the page |
| <%! ... %> | Declaration - declares a method or variable |
| ${ ... } | Expression Language (EL) - modern way to read data |
A JSP file is essentially an HTML template with "fill in the blank" sections written in Java. The static parts never change; the Java parts calculate what should go in the blanks each time the page is requested.
A Tiny JSP Example
Here is a minimal JSP page that greets a visitor and shows the current server time. Notice how ordinary HTML tags sit right next to Java code.
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Welcome</title></head><body> <h1>Hello, visitor!</h1> <p>The current server time is: <%= new java.util.Date() %></p></body></html>Click Run to see what this code prints.
The <%@ page %> line is a directive that configures the page (you will meet directives in a later lesson). The <%= ... %> expression runs the Java code new java.util.Date() and inserts the resulting text directly into the HTML output. Every time this page is requested, the timestamp is recalculated - that is what makes the page dynamic.
Where JSP Fits
JSP does not work alone. It runs inside a Java web server (also called a servlet container), such as Apache Tomcat, that understands how to translate and execute JSP files. Under the hood, JSP is actually built on top of another Java technology called Servlets - every JSP page is automatically converted into a Servlet before it runs. You will explore that relationship in detail in an upcoming lesson.
Server-Side
JSP code always runs on the server. The browser only ever receives plain HTML.
Built on Servlets
Every JSP page is translated into a Java Servlet behind the scenes by the container.
Runs in a Container
JSP requires a servlet container like Apache Tomcat to execute.
Still Widely Used
Many enterprise Java applications built over the last two decades still run on JSP.
Common Mistakes
- Thinking the browser executes Java - it does not; only the server does.
- Confusing a .jsp file with a .html file - a .jsp file is compiled and executed, not just served as-is.
- Assuming JSP is a standalone product - it requires a servlet container such as Tomcat to run.
- Believing JSP is obsolete and never used in real projects - it still powers many production systems today.
Best Practices
- Install and get comfortable with Apache Tomcat before writing your first JSP page.
- Remember that JSP output is always plain HTML - use browser "View Source" to see exactly what was sent.
- Start by reading existing JSP files in a legacy project to understand real-world patterns.
- Keep in mind that modern JSP style favors Expression Language and JSTL over raw Java scriptlets - you will learn why in a later lesson.
Frequently Asked Questions
No. A JSP file looks like HTML but can also contain Java code. The server processes that Java code and produces plain HTML, which is what actually reaches the browser.
Yes, at least the basics. JSP embeds real Java code, so understanding Java syntax, variables, and expressions will make learning JSP much easier.
Yes. While newer projects often use Spring MVC, Spring Boot, or single-page frontend frameworks, an enormous number of existing enterprise systems run on JSP, and maintaining or extending them is a common real-world job.
You need a servlet container, most commonly Apache Tomcat, which understands how to translate and execute .jsp files.
Key Takeaways
- JSP lets you embed Java code inside HTML to generate dynamic web pages.
- The server executes the Java code and sends only plain HTML to the browser.
- JSP files use special tags like <% %>, <%= %>, and ${ } to mark dynamic sections.
- JSP requires a servlet container such as Apache Tomcat to run.
- Every JSP page is translated into a Servlet behind the scenes.
Summary
JSP is a server-side Java technology that mixes HTML with embedded Java code to produce dynamic web pages. It runs inside a servlet container, translates each page into a Servlet, and sends only finished HTML to the browser. In the next lesson, you will look at the history of JSP and how it grew out of Java's early web development story alongside Servlets.