Your First JSP Page
Create a minimal .jsp file, deploy it to Tomcat, and view it running live in your browser for the first time.
Introduction
With Tomcat installed, it is time to write and run an actual JSP page. This lesson walks through the full cycle: creating a project folder, writing a simple .jsp file, placing it where Tomcat can find it, and loading it in a browser - the exact same workflow used in real JSP development.
Creating the Project Folder
Inside Tomcat's webapps directory, create a new folder for your project. This folder name becomes part of the URL used to access it.
tomcat/└── webapps/ └── firstapp/ └── hello.jspNote that there is no WEB-INF folder required yet - a JSP-only application with no Servlets or configuration can technically run without one, though real projects almost always include it (as you saw in the previous lesson).
Writing hello.jsp
Create a file named hello.jsp inside the firstapp folder with the following content.
<%@ page contentType="text/html;charset=UTF-8" language="java" %><!DOCTYPE html><html><head> <title>My First JSP Page</title></head><body> <h1>Hello, JSP!</h1> <p>This page was generated by a servlet container.</p></body></html>This is deliberately simple - there is no embedded Java logic yet, only the page directive from Lesson 1. Even a JSP file with zero dynamic content is still valid and will be translated and served correctly by Tomcat.
Deploying to Tomcat
Because the file already lives inside webapps/firstapp/, it is technically already deployed - this is the "exploded directory" style of deployment from the previous lesson. All that remains is to make sure Tomcat is running.
cd tomcat/bin
# Windowsstartup.bat
# macOS / Linux./startup.shYou do not need to restart Tomcat after adding a new .jsp file to an existing, already-deployed web application. Tomcat checks for and translates new/changed .jsp files automatically the first time each one is requested.
Viewing It in the Browser
With Tomcat running, open a browser and navigate to the following URL, matching the folder name and file name exactly.
http://localhost:8080/firstapp/hello.jspClick Run to see what this code prints.
If you view the page source in your browser (right-click, "View Page Source"), you will see plain HTML - no trace of the <%@ page %> directive. That confirms the server processed the JSP file and sent back only the finished markup, exactly as described in Lesson 1.
Making It Slightly Dynamic
Now add one small piece of embedded Java to prove the page is actually being executed on each request, not just served as a static file.
<%@ page contentType="text/html;charset=UTF-8" language="java" %><!DOCTYPE html><html><head> <title>My First JSP Page</title></head><body> <h1>Hello, JSP!</h1> <p>This page was generated by a servlet container.</p> <p>Page generated at: <%= new java.util.Date() %></p></body></html>Save the file, refresh the browser (no restart needed), and reload the page a few times.
Click Run to see what this code prints.
The changing timestamp confirms the Java expression is being re-evaluated on every single request - the page is genuinely dynamic, not a cached static file.
Common Mistakes
- Placing hello.jsp directly inside webapps/ instead of inside its own application subfolder.
- Typing the wrong URL path - it must match webapps/<folder>/<file>.jsp exactly, including case sensitivity on some operating systems.
- Forgetting to start Tomcat before testing in the browser, resulting in a "connection refused" error.
- Editing the file but forgetting to save it before refreshing the browser.
Best Practices
- Always check "View Page Source" early on to build the habit of confirming what actually reaches the browser.
- Keep the browser and your code editor side by side so you can refresh instantly after each save.
- Watch the Tomcat console/log output when a page fails to load - JSP compilation errors are reported there in detail.
- Start with the simplest possible JSP file and add complexity one small piece at a time, as done in this lesson.
Frequently Asked Questions
JSP files must be requested through a servlet container over HTTP (like http://localhost:8080/...). Opening the raw file in a browser (file://...) just shows the unprocessed source text, since there is no server to translate it.
No. Tomcat automatically translates the JSP file into a Servlet, compiles it into Java bytecode, and caches the compiled class - all behind the scenes, the first time the page is requested.
Tomcat will show a translation or compilation error page in the browser, and full details will also appear in the Tomcat logs folder, which is the best place to diagnose the exact line causing the problem.
Yes, Tomcat's port is configurable in conf/server.xml. If you change it, remember to update the URL you use in the browser to match.
Key Takeaways
- A JSP application lives in its own folder under Tomcat's webapps directory.
- JSP files are accessed over HTTP through the browser, never opened directly as local files.
- Tomcat automatically translates and compiles .jsp files without manual steps.
- View Page Source confirms that only final HTML - never raw JSP syntax - reaches the browser.
- A changing value on each refresh (like a timestamp) proves a JSP page is truly dynamic.
Summary
You have now written, deployed, and run your first JSP page end to end, and confirmed it executes fresh on every request. Next, you will go one level deeper and learn exactly what happens behind the scenes during the JSP lifecycle - from translation to compilation to serving requests.