--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.
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 is a Jakarta EE compatible implementation sponsored by the Eclipse Foundation. Eclipse GlassFish 5.1 is also Java EE 8 Compatible.
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)
├── 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 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 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.
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 /.
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
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>
You can remove the deployed web application with the asadmin undeploy command.
$ ./glassfish5/bin/asadmin undeploy mywebappname
Command undeploy executed successfully.
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.
Recommended Posts