Skip to content

Commit

Permalink
Adding ee8 webapp examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 1, 2024
1 parent f1e898a commit 2e351a2
Show file tree
Hide file tree
Showing 24 changed files with 457 additions and 53 deletions.
62 changes: 62 additions & 0 deletions embedded/ee10-webapp-context/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.examples.embedded</groupId>
<artifactId>jetty-embedded-examples</artifactId>
<version>12.0.x</version>
</parent>
<artifactId>ee10-webapp-context</artifactId>
<version>12.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 WebApp Context</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.examples.webapps</groupId>
<artifactId>hello-servlet-5</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-wars</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<includeArtifactIds>hello-servlet-3</includeArtifactIds>
<includeScope>runtime</includeScope>
<includeTypes>war</includeTypes>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteReleases>true</overWriteReleases>
<stripVersion>true</stripVersion>
<outputDirectory>${project.build.directory}/webapps</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package examples;

import java.io.IOException;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import java.net.URI;
import java.net.URL;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;

public class WebAppContextFromClasspath
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;

public class WebAppContextFromFileSystem
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);

Path warPath = Paths.get("../../webapps/hello/target/hello.war").toAbsolutePath().normalize();
Path warPath = Paths.get("target/webapps/hello-servlet-5.war").toAbsolutePath().normalize();
if (!Files.isRegularFile(warPath))
{
System.err.println("Unable to find " + warPath + ". Please build the entire project once first (`mvn clean install` from top of repo)");
Expand Down
68 changes: 68 additions & 0 deletions embedded/ee8-webapp-context/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.examples.embedded</groupId>
<artifactId>jetty-embedded-examples</artifactId>
<version>12.0.x</version>
</parent>
<artifactId>ee8-webapp-context</artifactId>
<version>12.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE8 WebApp Context</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.examples.webapps</groupId>
<artifactId>hello-servlet-3</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-wars</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<includeArtifactIds>hello-servlet-3</includeArtifactIds>
<includeScope>runtime</includeScope>
<includeTypes>war</includeTypes>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteReleases>true</overWriteReleases>
<stripVersion>true</stripVersion>
<outputDirectory>${project.build.directory}/webapps</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet
{
private String msg;

@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);

msg = config.getInitParameter("message");
if (msg == null)
{
msg = "User";
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
response.getWriter().printf("%s%n", msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples;

import java.net.URI;
import java.net.URL;

import org.eclipse.jetty.ee8.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;

public class WebAppContextFromClasspath
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);

// Figure out what path to serve content from
ClassLoader cl = WebAppContextFromClasspath.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
// designed to look for directories (we resolve the directory later)
URL f = cl.getResource("hello-webapp/hello.html");
if (f == null)
{
throw new RuntimeException("Unable to find resource directory");
}

// Resolve file to directory
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("WebRoot is " + webRootUri);

WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(webRootUri.toASCIIString());
webapp.setParentLoaderPriority(true);

server.setHandler(webapp);

server.start();
server.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.jetty.ee8.webapp.WebAppContext;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.InetAccessHandler;

public class WebAppContextFromFileSystem
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);

Path warPath = Paths.get("target/webapps/hello-servlet-3.war").toAbsolutePath().normalize();
if (!Files.isRegularFile(warPath))
{
System.err.println("Unable to find " + warPath + ". Please build the entire project once first (`mvn clean install` from top of repo)");
System.exit(-1);
}

System.out.println("WAR File is " + warPath);

InetAccessHandler inetAccessHandler = new InetAccessHandler();
// allow only http clients from localhost IPv4 or IPv6
inetAccessHandler.include("127.0.0.1", "::1");
server.setHandler(inetAccessHandler);

Handler.Sequence handlers = new Handler.Sequence();
inetAccessHandler.setHandler(handlers);

WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(warPath.toUri().toASCIIString());

handlers.addHandler(webapp);

// we should now have a handler tree like ...
// server.handler = InetAccessHandler
// wrapping -> Handler.Sequence
// contains -> WebAppContext

server.start();
server.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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_3_1.xsd"
version="3.1">
<display-name>hello webapp</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>examples.HelloServlet</servlet-class>
<init-param>
<param-name>message</param-name>
<param-value>Hello from web.xml</param-value>
</init-param>
</servlet>

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

<welcome-file-list>
<welcome-file>hello.html</welcome-file>
</welcome-file-list>
</web-app>
File renamed without changes.
3 changes: 2 additions & 1 deletion embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<module>compressed-encoding</module>
<module>connectors</module>
<module>deploying</module>
<module>ee8-webapp-context</module>
<module>ee10-webapp-context</module>
<module>error-handling</module>
<module>file-server</module>
<module>file-upload</module>
Expand All @@ -30,7 +32,6 @@
<module>servlet-security</module>
<module>simple-server</module>
<module>virtual-hosts</module>
<module>webapp-context</module>
<module>websocket-jakarta-api</module>
<module>websocket-jetty-api</module>
<module>xml</module>
Expand Down
Loading

0 comments on commit 2e351a2

Please sign in to comment.