JSP Getting Started
Prerequisites
Before writing your first JSP page, you need the following installed on your system:
- JDK 8+ — Download from oracle.com. Set
JAVA_HOMEenvironment variable. - Apache Tomcat 9+ — A popular open-source JSP/Servlet container. Download from tomcat.apache.org.
- IDE — Eclipse IDE for Enterprise Java (Eclipse EE) or IntelliJ IDEA Ultimate both have excellent JSP support.
Project Directory Structure
A standard JSP web application follows this directory structure:
MyWebApp/
├── index.jsp ← JSP pages (root level)
├── about.jsp
├── WEB-INF/
│ ├── web.xml ← Deployment descriptor
│ ├── classes/ ← Compiled .class files
│ │ └── com/example/
│ │ └── MyServlet.class
│ └── lib/ ← JAR dependencies
│ └── jstl.jar
└── resources/
├── css/
└── images/
The WEB-INF directory is protected — clients cannot access files inside it directly. Only the server can access them.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>My JSP Application</display-name>
<!-- Default welcome file -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Session timeout in minutes -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
Your First JSP Page
Create a file named index.jsp in the root of your web application:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to JSP</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.greeting { color: #2196F3; font-size: 24px; }
</style>
</head>
<body>
<h1 class="greeting">Hello, World!</h1>
<%-- JSP Comment: This won't appear in HTML source --%>
<%
// Java code in scriptlet
String user = request.getParameter("name");
if (user == null || user.isEmpty()) {
user = "Guest";
}
%>
<p>Welcome, <strong><%= user %></strong>!</p>
<p>Server time: <%= new Date() %></p>
<p>Server info: <%= application.getServerInfo() %></p>
<form method="get">
<input type="text" name="name" placeholder="Enter your name">
<button type="submit">Greet Me</button>
</form>
</body>
</html>
Deploying to Tomcat
There are two ways to deploy a JSP application to Tomcat:
- Direct Copy: Copy your web application folder into Tomcat's
webapps/directory. Tomcat auto-deploys it on startup. - WAR File: Package your application as a
.warfile and copy it towebapps/. Tomcat extracts and deploys it automatically. - Tomcat Manager: Use the Tomcat Manager web interface at
http://localhost:8080/managerto deploy WAR files through a browser. - IDE Integration: Eclipse and IntelliJ can deploy directly to a configured Tomcat server with one click.
After deploying, access your application at: http://localhost:8080/MyWebApp/index.jsp
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.