Hello World with GlassFish 5.1 + Servlet + JSP

Overview

--Create a WAR file in Gradle containing Java Servlet and JSP --Install GlassFish 5.1.0 --Deploy the WAR file to GlassFish 5.1.0 --Operation check environment: Java 8 (AdoptOpenJDK 1.8.0_265) + GlassFish 5.1.0 (Java Servlet 4.0 + JavaServer Pages 2.3) + macOS Catalina + Gradle 6.5.1.

What is GlassFish 5.1?

GlassFish 5.1 is a Jakarta EE 8 and Java EE 8 compliant web application server. It supports Java Servlet 4.0 and JavaServer Pages 2.3.

Eclipse GlassFish

Eclipse GlassFish is a Jakarta EE compatible implementation sponsored by the Eclipse Foundation. Eclipse GlassFish 5.1 is also Java EE 8 Compatible.

Install Java 8

If Java 8 is not installed, install it with Homebrew etc. and set the environment variables JAVA_HOME and PATH.

$ brew tap AdoptOpenJDK/openjdk

$ brew cask install adoptopenjdk8

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

$ export PATH=${JAVA_HOME}/bin:${PATH}

Note that in Java 11 and Java 14, Glassfish 5.1.0 will not start with the following error.

$ ./glassfish5/bin/asadmin start-domain
Exception in thread "main" java.lang.NullPointerException
	at com.sun.enterprise.module.common_impl.AbstractModulesRegistryImpl.initializeServiceLocator(AbstractModulesRegistryImpl.java:128)
	at com.sun.enterprise.module.common_impl.AbstractModulesRegistryImpl.newServiceLocator(AbstractModulesRegistryImpl.java:120)
	at com.sun.enterprise.module.common_impl.AbstractModulesRegistryImpl.createServiceLocator(AbstractModulesRegistryImpl.java:194)
	at com.sun.enterprise.module.common_impl.AbstractModulesRegistryImpl.createServiceLocator(AbstractModulesRegistryImpl.java:200)
	at com.sun.enterprise.module.single.StaticModulesRegistry.createServiceLocator(StaticModulesRegistry.java:64)
	at com.sun.enterprise.admin.cli.CLIContainer.getServiceLocator(CLIContainer.java:193)
	at com.sun.enterprise.admin.cli.CLIContainer.getLocalCommand(CLIContainer.java:231)
	at com.sun.enterprise.admin.cli.CLICommand.getCommand(CLICommand.java:207)
	at com.sun.enterprise.admin.cli.AdminMain.executeCommand(AdminMain.java:347)
	at com.sun.enterprise.admin.cli.AdminMain.doMain(AdminMain.java:282)
	at org.glassfish.admin.cli.AsadminMain.main(AsadminMain.java:33)

Create a WAR file with Gradle containing Java Servlet and JSP

File list

├── build.gradle
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── MyServlet.java
        └── webapp
            ├── WEB-INF
            │   └── web.xml
            └── myjsp.jsp

build.gradle

plugins {
  id 'war'
}

repositories {
  mavenCentral()
}

dependencies {
  // Java Servlet 4.0 API
  // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
  providedCompile 'javax.servlet:javax.servlet-api:4.0.1'
}

// Java 8
sourceCompatibility = 1.8

// Application
version = '1.0'

src/main/java/com/example/MyServlet.java

package com.example;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {

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

src/main/webapp/myjsp.jsp

<%@ page contentType="text/html; charset=utf-8" %><html><body>
JSP: Hello JSP World!<br>
<%= pageContext.getServletContext().getServerInfo() %><br>
java.vm.name: <%= System.getProperty("java.vm.name") %><br>
java.vm.vendor: <%= System.getProperty("java.vm.vendor") %><br>
java.vm.version: <%= System.getProperty("java.vm.version") %><br>
</body></html>

src/main/webapp/WEB-INF/web.xml

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

<!-- Web Application Deployment Descriptor (Java Servlet 4.0) -->
<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">

  <servlet>
    <servlet-name>myjsp</servlet-name>
    <jsp-file>/myjsp.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>myjsp</servlet-name>
    <url-pattern>/myjsp</url-pattern>
  </servlet-mapping>

</web-app>

Create a WAR file

Create a WAR file with the Gradle build task.

$ gradle build

Make sure the WAR file is generated.

$ file build/libs/mywebapp-1.0.war 
build/libs/mywebapp-1.0.war: Zip archive data, at least v1.0 to extract

Install GlassFish 5.1.0

Install according to the official manual Installing Eclipse GlassFish Server 5 \ .1.

From Eclipse GlassFish \ | projects \ .eclipse \ .org glassfish-5.1.0.zip (Eclipse GlassFish 5.1.0, Full Profile) To download.

Extract the downloaded glassfish-5.1.0.zip to the installation directory.

$ unzip glassfish-5.1.0.zip

Simply download the zip file and unzip it to complete the installation.

Start GlassFish

Quick Start for Basic Features -Starting and Stopping the Default Domain Start according to.

$ ./glassfish5/bin/asadmin start-domain
Waiting for domain1 to start ......
Successfully started the domain : domain1
domain  Location: /Users/foo/glassfish5/glassfish/domains/domain1
Log File: /Users/foo/glassfish5/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.

Once started, you can manage the state of GlassFish at Administration Console http: // localhost: 4848 /.

Deploy the WAR file to GlassFish 5.1.0

It can be deployed with the asadmin deploy command. Details of the command can be found in the reference manual deploy.

$ ./glassfish5/bin/asadmin deploy --contextroot=mywebappcr --name=mywebappname /Users/foo/mywebapp/build/libs/mywebapp-1.0.war
Application deployed with name mywebappname.
Command deploy executed successfully.

The deployed WAR file is copied and expanded under the installation directory.

$ find . | grep mywebapp
./glassfish5/glassfish/domains/domain1/generated/xml/mywebappname
./glassfish5/glassfish/domains/domain1/generated/ejb/mywebappname
./glassfish5/glassfish/domains/domain1/generated/policy/mywebappname
./glassfish5/glassfish/domains/domain1/generated/policy/mywebappname/mywebappname
./glassfish5/glassfish/domains/domain1/generated/policy/mywebappname/mywebappname/granted.policy
./glassfish5/glassfish/domains/domain1/generated/jsp/mywebappname
./glassfish5/glassfish/domains/domain1/applications/mywebappname
./glassfish5/glassfish/domains/domain1/applications/mywebappname/myjsp.jsp
./glassfish5/glassfish/domains/domain1/applications/mywebappname/META-INF
./glassfish5/glassfish/domains/domain1/applications/mywebappname/META-INF/MANIFEST.MF
./glassfish5/glassfish/domains/domain1/applications/mywebappname/WEB-INF
./glassfish5/glassfish/domains/domain1/applications/mywebappname/WEB-INF/classes
./glassfish5/glassfish/domains/domain1/applications/mywebappname/WEB-INF/classes/com
./glassfish5/glassfish/domains/domain1/applications/mywebappname/WEB-INF/classes/com/example
./glassfish5/glassfish/domains/domain1/applications/mywebappname/WEB-INF/classes/com/example/MyServlet.class
./glassfish5/glassfish/domains/domain1/applications/mywebappname/WEB-INF/web.xml
./glassfish5/glassfish/domains/domain1/applications/__internal/mywebappname
./glassfish5/glassfish/domains/domain1/applications/__internal/mywebappname/mywebapp-1.0.war

Operation check

Check the operation of the deployed Web application.

(I'm a little worried that Servlet / 3.1 is output even though it should support Servlet 4.0)

$ curl --include http://localhost:8080/mywebappcr/myservlet
HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition  5.1.0 
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  5.1.0  Java/AdoptOpenJDK/1.8)
Content-Type: text/html;charset=utf-8
Content-Length: 118

<html><body>
Servlet: Hello Servlet World!<br>
GlassFish Server Open Source Edition  5.1.0 
</body></html>
$ curl --include http://localhost:8080/mywebappcr/myjsp
HTTP/1.1 200 OK
Server: GlassFish Server Open Source Edition  5.1.0 
X-Powered-By: JSP/2.3
Set-Cookie: JSESSIONID=0123456789abcdef0123456789ab; Path=/mywebappcr; HttpOnly
Content-Type: text/html;charset=utf-8
Content-Length: 230

<html><body>
JSP: Hello JSP World!<br>
GlassFish Server Open Source Edition  5.1.0 <br>
java.vm.name: OpenJDK 64-Bit Server VM<br>
java.vm.vendor: AdoptOpenJDK<br>
java.vm.version: 25.265-b01<br>
</body></html>

Undeploy the web application

You can remove the deployed web application with the asadmin undeploy command.

$ ./glassfish5/bin/asadmin undeploy mywebappname
Command undeploy executed successfully.

Stop GlassFish

You can stop it with the asadmin stop-domain command.

$ ./glassfish5/bin/asadmin stop-domain
Waiting for the domain to stop .
Command stop-domain executed successfully.

Reference material

Recommended Posts

Hello World with GlassFish 5.1 + Servlet + JSP
Hello World with Micronaut
Run JSP Hello World with Tomcat on Docker
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with VS Code!
Getting Started with JSP & Servlet
Hello World with Spring Boot
Hello World with SpringBoot / Gradle
Hello, World! With Asakusa Framework!
Hello World with Java Servlet and JSP (Easy web server startup with Maven + Jetty)
Hello world with Kotlin and JavaFX
Passing parameters from JSP with Servlet
(Intellij) Hello World with Spring Boot
Create PDF with itext7 ~ Hello World ~
"Hello world" for ImageJ with Eclipse
Hello World with GWT 2.8.2 and Maven
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 + CLI in 5 minutes
Hello world with Kotlin and Tornado FX
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Read "Hello world"
Java, Hello, world!
Java Hello World
Hello World with JavaFX 11 (OpenJFX) in Liberica JDK 11
Hello world! With Spring Boot (Marven + text editor)
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Show a simple Hello World with SpringBoot + IntelliJ
Try to display hello world with spring + gradle
Easy to display hello world with Rails + Docker
"Hello World" in Java
Hello World (REST API) with Apache Camel + Spring Boot 2
Java Learning (1)-Hello World
About [servlet] [JSP] [tomcat]
Read System.out.println ("hello, world")
Let's write Hello World
Hello world in node.js
Hello World in Java
Studying Java-Part 1-Hello World
Hello World on WebAssembly
Hello World (console app) with Apache Camel + Spring Boot 2
[Java] One type of alphabet prohibited With binding Hello, world! [Binding]
Hello World with Ruby extension library for the time being
Create a Hello World web app with Spring framework + Jetty
Until you create a Web application with Servlet / JSP (Part 1)
[Vue Rails] "Hello Vue!" Displayed with Vue + Rails
java hello world, compile, run
Java Servlet / JSP View drawing
JSP error display from Servlet
Java beginners read Hello World
Comparison of WEB application development with Rails and Java Servlet + JSP
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
Until you run Hello World of JavaFX with VS Code + Gradle
Try Hello World with the minimum configuration of Heroku Java spring-boot
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
[Introduction to JSP + Servlet] I played with it for a while ♬