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

fix: Scan for WebServlet annotation #125

Open
wants to merge 7 commits into
base: 12.0.x
Choose a base branch
from
Open

Conversation

mat3e
Copy link

@mat3e mat3e commented Jul 11, 2024

Following this example I couldn't make @WebServlet annotation work. I reproduced the issue with test and fixed it following:

Also, seems web.xml is not used at all (it points to Servlets 3.1) and there were some minor issues, like missing @Override I fixed.

Copy link
Contributor

@joakime joakime left a comment

Choose a reason for hiding this comment

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

The addition of jetty-ee10-annotation dependency, and the test case, should be the only things this PR needs.

{
HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
HttpRequest request = HttpRequest.newBuilder()
var client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
Copy link
Contributor

Choose a reason for hiding this comment

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

No, keep the variable, don't fall back to var.
The use of var makes for bad and hard to follow examples. (the whole point of this repo)

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">
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't delete this, upgrade it to ee6 instead.

@@ -51,6 +49,7 @@ public static Server newServer(int port)
context.setBaseResource(baseResource);
context.setContextPath("/");
context.setWelcomeFiles(new String[]{"index.html", "welcome.html"});
context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/");
Copy link
Contributor

Choose a reason for hiding this comment

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

Set the pattern properly, don't include 100% of the container.
As on Maven, this even includes all of the maven deps, and the maven plugins too.

In fact, the default pattern in ee10 should work out of the box.

Copy link
Author

@mat3e mat3e Jul 11, 2024

Choose a reason for hiding this comment

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

What should be the pattern? When I removed this line (using default?), test was failing. I changed to .*/classes/* now.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are two patterns that impact how the webapp scanning operates.

  1. The Container level (org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern) which scans for things that are not part of the webapp.
    By default, it will not scan the container, unless this pattern is defined.
  2. The WebApp level (org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern) which scans for things that belong specifically to the webapp.
    By default, this will scan all artifacts on the webapp (like WEB-INF/lib/ and classes/) unless the pattern is defined.

For the container, you might need to support servlet, and jsp, and taglib.

For servlet only ...

webappContext.setAttribute(
    "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
    ".*/jakarta.servlet-api-[^/]*\\.jar$");

For servlet, with JSP, and even Taglib support ...

webappContext.setAttribute(
    "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
    ".*/jakarta.servlet-api-[^/]*\\.jar$|.*/jakarta.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");

Add more jar patterns for other jars that you might need from the container (which shouldn't be much).
Point is, don't open everything on the container for scanning, that's just wrong way to use the pattern, wrong for performance as you scan everything, wrong for security as you never want to open everything to the webapp, etc ...

Copy link
Contributor

@joakime joakime Jul 11, 2024

Choose a reason for hiding this comment

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

The overwhelming majority of embedded use cases never scan, as that's an anti-pattern for embedded use cases (which focus on absolute control, and startup performance).

They instead define the webapp configuration entirely in code for quick startup.

This is the reason why those patterns are the way they are by default.

@joakime
Copy link
Contributor

joakime commented Jul 16, 2024

There are build errors with this latest code push.

Run mvn spotless:apply once and repush please.

@mat3e
Copy link
Author

mat3e commented Jul 20, 2024

I applied Spotless, but when it comes to ContainerIncludeJarPattern, ".*/classes/*" was the narrowest I found to pass my test expecting TestServlet to be registered 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants