Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetty 12.0.x resource Servlet #11933

Merged
merged 21 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/jetty-asciidoctor-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.eclipse.jetty.documentation</groupId>
<artifactId>documentation</artifactId>
<version>12.0.10-SNAPSHOT</version>
<version>12.0.12-SNAPSHOT</version>
</parent>
<artifactId>jetty-asciidoctor-extensions</artifactId>
<packaging>jar</packaging>
Expand Down
8 changes: 6 additions & 2 deletions documentation/jetty-documentation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<parent>
<groupId>org.eclipse.jetty.documentation</groupId>
<artifactId>documentation</artifactId>
<version>12.0.10-SNAPSHOT</version>
<version>12.0.12-SNAPSHOT</version>
</parent>
<artifactId>jetty-documentation</artifactId>
<packaging>pom</packaging>
<packaging>jar</packaging>
<name>Documentation :: Guides</name>

<properties>
Expand Down Expand Up @@ -58,6 +58,10 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-infinispan-embedded-query</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,10 @@ Below you can find an example of how to setup `DefaultServlet`:
----
include::../../{doc_code}/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=defaultServlet]
----

If you wish to serve resources from a non-default location, then the `ResourceServlet` should be used instead of the `DefaultServlet`. Below you can find an example of how to setup both a `DefaultServlet` and a `ResourceServlet`:

[source,java,indent=0]
----
include::../../{doc_code}/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=resourceServlet]
----
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.jetty.client.ContentResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.ee10.servlet.DefaultServlet;
import org.eclipse.jetty.ee10.servlet.ResourceServlet;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
Expand Down Expand Up @@ -1220,11 +1221,32 @@ public void defaultServlet()
// Add the DefaultServlet to serve static content.
ServletHolder servletHolder = context.addServlet(DefaultServlet.class, "/");
// Configure the DefaultServlet with init-parameters.
servletHolder.setInitParameter("resourceBase", "/path/to/static/resources/");
servletHolder.setInitParameter("maxCacheSize", "8388608");
servletHolder.setInitParameter("dirAllowed", "true");
servletHolder.setAsyncSupported(true);
// end::defaultServlet[]
}

public void resourceServlet()
{
// tag::resourceServlet[]
// Create a ServletContextHandler with contextPath.
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/app");

// Add the ResourceServlet to serve static content from a specific location.
ServletHolder servletHolder = context.addServlet(ResourceServlet.class, "/static/*");
// Configure the ResourceServlet with init-parameters.
servletHolder.setInitParameter("baseResource", "/absolute/path/to/static/resources/");
servletHolder.setInitParameter("pathInfoOnly", "true");
servletHolder.setAsyncSupported(true);

// Add the DefaultServlet to serve static content from the resource base
ServletHolder defaultHolder = context.addServlet(DefaultServlet.class, "/");
defaultHolder.setAsyncSupported(true);
// end::resourceServlet[]
}

public void serverGzipHandler() throws Exception
{
// tag::serverGzipHandler[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ public void defaultServlet()
// Add the DefaultServlet to serve static content.
ServletHolder servletHolder = context.addServlet(DefaultServlet.class, "/");
// Configure the DefaultServlet with init-parameters.
servletHolder.setInitParameter("resourceBase", "/path/to/static/resources/");
servletHolder.setInitParameter("baseResource", "/path/to/static/resources/");
servletHolder.setAsyncSupported(true);
// end::defaultServlet[]
}
Expand Down
2 changes: 2 additions & 0 deletions documentation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@

<modules>
<module>jetty</module>
<module>jetty-asciidoctor-extensions</module>
<module>jetty-documentation</module>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also the jetty-asciidoctor-extensions module that's not activated and stuck at version 12.0.10-SNAPSHOT. Shouldn't it be enabled too?

</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,12 @@ public Request getWrapped()
{
return _request;
}

@Override
public String toString()
{
return "%s@%x{%s}".formatted(getClass().getSimpleName(), hashCode(), getWrapped());
}
}

@SuppressWarnings("unchecked")
Expand Down
Loading
Loading