Before writing your first JSP page, you need the following installed on your system:
JAVA_HOME environment variable.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>
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>
There are two ways to deploy a JSP application to Tomcat:
webapps/ directory. Tomcat auto-deploys it on startup..war file and copy it to webapps/. Tomcat extracts and deploys it automatically.http://localhost:8080/manager to deploy WAR files through a browser.After deploying, access your application at: http://localhost:8080/MyWebApp/index.jsp
Explore 500+ free tutorials across 20+ languages and frameworks.