Hello World with Java Servlet and JSP (Easy web server startup with Maven + Jetty)

Overview

--Run Java Servlet and JSP in local environment --Use Jetty for Servlet container --Launch Jetty with Maven plugin

environment

Source code

Source code list

--pom.xml: Build config file for Maven --MyServlet.java: Java Servlet source code --index.jsp: JSP source code --web.xml: Web application configuration file

├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── MyServlet.java
        └── webapp
            ├── WEB-INF
            │   └── web.xml
            └── index.jsp

pom.xml: Build config file for Maven

--Add javax.servlet-api to dependency to use Java Servlet API 3.1. --Add jetty-maven-plugin so that you can easily start Jetty from Maven and check the operation.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>my-servlet</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>my-servlet</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <jetty.version>9.4.19.v20190610</jetty.version>
  </properties>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- https://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html -->
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty.version}</version>
      </plugin>
    </plugins>
  </build>

</project>

MyServlet.java: Java Servlet source code

package com.example;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/html; charset=utf-8");
    PrintWriter out = resp.getWriter();
    out.println("<html><body>Hello, Servlet World!</body></html>");
    out.flush();
    out.close();
  }
}

index.jsp: JSP source code

<html><body>Hello JSP World!</body></html>

web.xml: web application configuration file

--Mapping the Servlet of MyServlet.java so that it can be accessed with / my-servlet / *.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>My Servlet Web Application</display-name>

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/my-servlet/*</url-pattern>
  </servlet-mapping>
</web-app>

Launch Jetty to access your web application

You can start Jetty with mvn jetty: run.

$ mvn jetty:run

When you access http: // localhost: 8080 / my-servlet /, the content output by MyServlet.java is returned.

$ curl http://localhost:8080/my-servlet/
<html><body>Hello, Servlet World!</body></html>

When you access http: // localhost: 8080 /, the content written in index.jsp is returned.

$ curl http://localhost:8080/
<html><body>Hello JSP World!</body></html>

Reference material

Recommended Posts

Hello World with Java Servlet and JSP (Easy web server startup with Maven + Jetty)
Hello World with GlassFish 5.1 + Servlet + JSP
Hello World with GWT 2.8.2 and Maven
Comparison of WEB application development with Rails and Java Servlet + JSP
[Updated December 2018] Tips for embedded web server and servlet created with Jetty9
Create a Hello World web app with Spring framework + Jetty
Hello world with Kotlin and JavaFX
Hello World with Docker and C
Hello world in Java and Gradle
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Hello World with Eclipse + Spring Boot + Maven
Hello world with Java template engine Thymeleaf
Java development with Codenvy: Hello World! Run
Hello world with Kotlin and Tornado FX
Minimal Java environment construction and Hello World
Deploy Java web app to Azure with maven
Build Java environment and output hello world [Beginner]
Run JSP Hello World with Tomcat on Docker
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Easy to make LINE BOT with Java Servlet
Easy to display hello world with Rails + Docker
Java, Hello, world!
Java Hello World
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
"Hello World" in Java
Java Learning (1)-Hello World
Hello World in Java
Hello World with Micronaut
Web application development memo with MVN, Tomcat, JSP / Servlet with VScode
[Java] One type of alphabet prohibited With binding Hello, world! [Binding]
Until you create a Web application with Servlet / JSP (Part 1)
Reasons to use Servlet and JSP separately in Java development
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
Of the three Java embedded web servers, Tomcat, Jetty, and Undertow, which one worked with GraalVM?