-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/10.0.x' into 11.0.x
- Loading branch information
Showing
17 changed files
with
876 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip | ||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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>11.0.x</version> | ||
</parent> | ||
<artifactId>servlet-server</artifactId> | ||
<version>11.0.x</version> | ||
<packaging>jar</packaging> | ||
<name>Jetty Examples :: Jetty 11.0.x :: Embedded :: Servlet Server</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</groupId> | ||
<artifactId>jetty-webapp</artifactId> | ||
<version>${jetty.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-client</artifactId> | ||
<version>${jetty.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.jetty.toolchain</groupId> | ||
<artifactId>jetty-test-helper</artifactId> | ||
<version>${jetty-test-helper.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
118 changes: 118 additions & 0 deletions
118
embedded/servlet-server/src/main/java/examples/EmbedMe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// ======================================================================== | ||
// 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.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.util.resource.Resource; | ||
import org.eclipse.jetty.webapp.WebAppContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class EmbedMe | ||
{ | ||
private static final Logger LOG = LoggerFactory.getLogger(EmbedMe.class); | ||
|
||
public static void main(String[] args) throws Exception | ||
{ | ||
int port = 8080; | ||
Server server = newServer(port); | ||
server.start(); | ||
server.join(); | ||
} | ||
|
||
public static Server newServer(int port) | ||
{ | ||
Server server = new Server(port); | ||
|
||
WebAppContext context = new WebAppContext(); | ||
Resource baseResource = findBaseResource(context); | ||
LOG.info("Using BaseResource: {}", baseResource); | ||
context.setBaseResource(baseResource); | ||
context.setContextPath("/"); | ||
context.setWelcomeFiles(new String[]{"index.html", "welcome.html"}); | ||
context.setParentLoaderPriority(true); | ||
server.setHandler(context); | ||
return server; | ||
} | ||
|
||
private static Resource findBaseResource(WebAppContext context) | ||
{ | ||
try | ||
{ | ||
// Look for resource in classpath (this is the best choice when working with a jar/war archive) | ||
ClassLoader classLoader = context.getClass().getClassLoader(); | ||
URL webXml = classLoader.getResource("/WEB-INF/web.xml"); | ||
if (webXml != null) | ||
{ | ||
URI uri = webXml.toURI().resolve("../..").normalize(); | ||
LOG.info("Found WebResourceBase (Using ClassLoader reference) {}", uri); | ||
return Resource.newResource(uri); | ||
} | ||
} | ||
catch (URISyntaxException e) | ||
{ | ||
throw new RuntimeException("Bad ClassPath reference for: WEB-INF", e); | ||
} | ||
catch (MalformedURLException e) | ||
{ | ||
throw new RuntimeException("Bad ClassPath reference for: WEB-INF", e); | ||
} | ||
|
||
// Look for resource in common file system paths | ||
try | ||
{ | ||
Path pwd = Paths.get(System.getProperty("user.dir")).toAbsolutePath(); | ||
Path targetDir = pwd.resolve("target"); | ||
if (Files.isDirectory(targetDir)) | ||
{ | ||
try (Stream<Path> listing = Files.list(targetDir)) | ||
{ | ||
Path embeddedServletServerDir = listing | ||
.filter(Files::isDirectory) | ||
.filter((path) -> path.getFileName().toString().startsWith("embedded-servlet-server-")) | ||
.findFirst() | ||
.orElse(null); | ||
if (embeddedServletServerDir != null) | ||
{ | ||
LOG.info("Found WebResourceBase (Using /target/ Path) {}", embeddedServletServerDir); | ||
return Resource.newResource(embeddedServletServerDir); | ||
} | ||
} | ||
} | ||
|
||
// Try the source path next | ||
Path srcWebapp = pwd.resolve("src/main/webapp/"); | ||
if (Files.exists(srcWebapp)) | ||
{ | ||
LOG.info("WebResourceBase (Using /src/main/webapp/ Path) {}", srcWebapp); | ||
return Resource.newResource(srcWebapp); | ||
} | ||
} | ||
catch (Throwable t) | ||
{ | ||
throw new RuntimeException("Unable to find web resource in file system", t); | ||
} | ||
|
||
throw new RuntimeException("Unable to find web resource ref"); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
embedded/servlet-server/src/main/java/examples/TestServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// ======================================================================== | ||
// 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.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@SuppressWarnings("serial") | ||
@WebServlet(urlPatterns = {"/test"}) | ||
public class TestServlet extends HttpServlet | ||
{ | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | ||
{ | ||
request.getRequestDispatcher("/WEB-INF/html/index.html").forward(request, response); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
embedded/servlet-server/src/main/java/examples/TimeServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// ======================================================================== | ||
// 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 java.util.Date; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@SuppressWarnings("serial") | ||
@WebServlet(urlPatterns = {"/time"}) | ||
public class TimeServlet extends HttpServlet | ||
{ | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
response.setContentType("text/plain"); | ||
response.getWriter().write(new Date().toString()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
embedded/servlet-server/src/main/webapp/WEB-INF/html/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<html> | ||
<head> | ||
<title>Content from WEB-INF/html</title> | ||
</head> | ||
<body> | ||
<h1>Content from WEB-INF/html</h1> | ||
<p> | ||
This content from <code>{war}/WEB-INF/html/index.html</code> | ||
</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?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>Demo of Servlet 3.1</display-name> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<html> | ||
<head> | ||
<title>Welcome File</title> | ||
</head> | ||
<body> | ||
<h1>Welcome</h1> | ||
<p> | ||
This is the <code>src/main/webapp/welcome.html</code> | ||
</p> | ||
<h2>Other servlets</h2> | ||
<ul> | ||
<li><a href="/test">/test</a> - a simple servlet that just returns content from <code>{war}/WEB-INF/html/</code> directory | ||
</li> | ||
<li><a href="/time">/time</a> - a simple servlet that just shows the server time</li> | ||
</ul> | ||
</body> | ||
</html> |
Oops, something went wrong.