2

I´m trying to implement a Jetty Server inside a Swing / JavaFX application. There is a similar project with Tomcat: https://www.beyondjava.net/how-to-wrap-bootsfaces-or-jsf-in-general-as-a-native-desktop-application

Now I´m trying to change this to Jetty. This is in my main code (instead tomcat)

public void run() throws Exception {
    Server server = new Server(8080);

    Path basePath = new File("src/main/webapp/").toPath().toRealPath();

    WebAppContext context = new WebAppContext();
    context.setContextPath("/");

    // Configuring from Development Base
    context.setBaseResource(new PathResource(basePath.resolve("src/main/webapp")));
    // Add webapp compiled classes & resources (copied into place from
    // src/main/resources)
    Path classesPath = basePath.resolve("target/webapp/WEB-INF/classes");
    context.setExtraClasspath(classesPath.toAbsolutePath().toString());
    server.setDumpAfterStart(true);

    server.setHandler(context);

    server.start();
}

In the pom.xml I´ve added this:

<dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.48.v20220622</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.4.48.v20220622</version>
        </dependency>

If I start the Main function the Webapp (index.xhtml) is not showing. Ijust got:

enter image description here

Here the structure: enter image description here

Any idea what is wrong?

Here the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <!--param-value>Production</param-value -->
        <param-value>Development</param-value>
    </context-param>
    <!-- The BootsFaces_THEME context-param controls the loading of the Themes -->


    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!-- Map following files to the JSF servlet -->
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

The faces-config.xml is currently empty

vished2000
  • 164
  • 1
  • 11

1 Answers1

2

Note: Jetty 9.4.x is now at End of Community Support. I would recommend upgrading to Jetty 10.x or Jetty 11.x as soon as you are able to. (Pay attention to namespace when making your decision)

See https://github.com/eclipse/jetty.project/issues/7958

The first thing that jumps out at me is that there is a problem with your baseResource reference.

Path basePath = new File("src/main/webapp/").toPath().toRealPath();

context.setBaseResource(new PathResource(basePath.resolve("src/main/webapp")));

That will result in a baseResource of src/main/webapp/src/main/webapp.

Your console / logs would show this as well.

Change it to ...

context.setBaseResource(new PathResource(basePath));

The next thing I notice is that you have JSP files, to enable JSP support requires some extra steps.

If using Jetty 10+ you'll need the following dependencies.

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>apache-jsp</artifactId>
  <version>10.0.11</version>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-annotations</artifactId>
  <version>10.0.11</version>
</dependency>

If using Jetty 9 or older, you'll also need to setup a whole mess of initialization manually with Server Configuration, webapp Configuration, a few properties to tell jetty where to scan for implementations in the server or webapp classloaders, manually add a few ServletContainerInitializers to enable JSP properly, etc. (it's easier to just upgrade to Jetty 10)

https://github.com/jetty-project/embedded-jetty-jsp

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • If I change that to your approach, I´m gettng: HTTP ERROR 503 Service Unavailable and in the logs some other errors: SCHWERWIEGEND: Unable to obtain InjectionProvider from init time FacesContext. Does this container implement the Mojarra Injection SPI? Jul 09, 2022 1:35:04 PM javax.faces.FactoryFinderInstance getFactory SCHWERWIEGEND: Die Anwendung wurde bei Systemstart nicht einwandfrei initialisiert, Factory konnte nicht gefunden werden: javax.faces.context.FacesContextFactory. – vished2000 Jul 09 '22 at 11:35
  • Thnak you.... I´ve updated to 10.0.11.... and now I´m getting this error after I start the application inside the SWT app: "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." – vished2000 Jul 09 '22 at 12:46
  • That means you need to use `https`, not `http`. – Joakim Erdfelt Jul 09 '22 at 12:51
  • how can I change that? I mean I´m using localhost? – vished2000 Jul 09 '22 at 12:52
  • Joakim, I´m using JSF not JSP... – vished2000 Jul 11 '22 at 09:46
  • ok, I fixed that Issue with https... it´s a bug in elipse... but still I got 503 error – vished2000 Jul 11 '22 at 11:03