Getting Started with JSP | Generated by AI

Home 2025.10

JavaServer Pages (JSP) is a server-side technology used to create dynamic web content in Java applications. It allows you to embed Java code directly into HTML pages, making it easier to generate dynamic responses based on user input or data. JSP is part of the Jakarta EE (formerly Java EE) platform and works well with servlets in web applications.

Below is a step-by-step guide to get started with JSP, from setup to deployment. This assumes basic knowledge of Java and HTML.

1. Prerequisites

2. Set Up Your Environment

  1. Install Tomcat:
    • Extract the Tomcat archive to a directory (e.g., C:\tomcat on Windows or /opt/tomcat on Linux).
    • Start Tomcat by running bin/startup.bat (Windows) or bin/startup.sh (Unix). Access http://localhost:8080 in your browser to verify it’s running.
  2. Create a Project Structure:
    • In Tomcat’s webapps folder, create a new directory for your app (e.g., my-jsp-app).
    • Inside it, create:
      • WEB-INF/web.xml (deployment descriptor, optional in JSP 2.2+ but good for config).
      • A root folder for JSP files (e.g., index.jsp).

    Basic web.xml example:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
             https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
             version="5.0">
        <display-name>My JSP App</display-name>
    </web-app>
    

3. Write Your First JSP Page

JSP files have a .jsp extension and combine HTML with Java code using scriptlets (<% %>), expressions (<%= %>), and declarations (<%! %>). For modern best practices, use JSP Expression Language (EL) and JSTL (JavaServer Pages Standard Tag Library) to avoid raw scriptlets.

Example: Create index.jsp in your app’s root:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  <!-- For JSTL, if used -->

<html>
<head>
    <title>Hello JSP</title>
</head>
<body>
    <h1>Welcome to JSP!</h1>
    
    <!-- Scriptlet: Java code -->
    <% 
        String name = request.getParameter("name") != null ? request.getParameter("name") : "World";
        java.util.Date now = new java.util.Date();
    %>
    
    <!-- Expression: Output value -->
    <p>Hello, <%= name %>! The time is <%= now %>.</p>
    
    <!-- Using EL (Expression Language) for cleaner output -->
    <p>Your name via EL: ${param.name}</p>
    
    <!-- JSTL example: Loop over a list -->
    <c:set var="fruits" value="$Apple" />
    <ul>
        <c:forEach var="fruit" items="${fruits}">
            <li>${fruit}</li>
        </c:forEach>
    </ul>
</body>
</html>

4. Deploy and Run

  1. Place your app folder (e.g., my-jsp-app) in Tomcat’s webapps directory.
  2. Restart Tomcat.
  3. Access in browser: http://localhost:8080/my-jsp-app/index.jsp.
  4. Test with query params: http://localhost:8080/my-jsp-app/index.jsp?name=Grok to see dynamic output.

5. Common Features and Best Practices

6. Advanced Topics

For hands-on practice, start with a simple “Hello World” and gradually add features. If you’re building a full web app, explore Jakarta EE tutorials.

References


Back

x-ai/grok-4-fast

Donate