Embedded Jetty Web Application Receiving 404 Error Not Found

I want to deploy Java Netbeans Webapp with an embedded Jetty server; the server itself is working, but I always get the following error:

enter image description here

I looked at heaps of examples on the Internet, set up and reconfigure my web.xml; although my configuration seems fine, I can't get it to work.

I must point out that when I launch the application from whithin Netbeans using the built-in Glassfish server, it works fine, which tells me that web.xml is probably well configured.

Can anyone help with this?

My code follows.

PS I know that he was asked about SO, but these examples also did not work for me.

Project Structure:

enter image description here

WebContext setup:

import org.eclipse.jetty.webapp.WebAppContext; public class AppContextBuilder { private WebAppContext webAppContext; public WebAppContext buildWebAppContext() { webAppContext = new WebAppContext(); webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml"); webAppContext.setResourceBase("src/main/webapp"); webAppContext.setContextPath("/Holmes"); return webAppContext; } } 

JettyServer.java:

 import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandlerCollection; public class JettyServer { private Server server; public JettyServer() { this(8585); } public JettyServer(Integer runningPort) { server = new Server(runningPort); } public void setHandler(ContextHandlerCollection contexts) { server.setHandler(contexts); } public void start() throws Exception { server.start(); } public void stop() throws Exception { server.stop(); server.join(); } public boolean isStarted() { return server.isStarted(); } public boolean isStopped() { return server.isStopped(); } } 

Deploy.java (main method) :

 import org.apache.log4j.Logger; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.handler.ContextHandlerCollection; /** * * @author Motty Waldner < motty@timeworksny.com > */ public class Deploy { private final static Logger log = Logger.getLogger(Deploy.class); static JettyServer jettyServer = new JettyServer(); public static void main(String[] args) throws Exception { // add hook to stop server upon service termination // (service calls System.exit(0) upon termination, // so it should work under normal circumstances) addShutdownHook(); ContextHandlerCollection contexts = new ContextHandlerCollection(); Handler[] handlers = new Handler[]{new AppContextBuilder().buildWebAppContext().getHandler()}; contexts.setHandlers(handlers); jettyServer = new JettyServer(); try { jettyServer.start(); } catch (Exception e) { log.error("Error Starting Jetty Server", e); } } private static void addShutdownHook() { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { jettyServer.stop(); log.info("Shutdown Hook is running: Jetty Server instance being stopped."); } catch (Exception e) { log.error("error", e); } log.info("Application Terminating ..."); } }); } } 

web.xml:

  <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Test App</display-name> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <session-config> <session-timeout>120</session-timeout> </session-config> <servlet-mapping> <servlet-name>StrutsController</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app> 

Thanks in advance for your help!

+7
java embedded-jetty
source share
4 answers

change the public class AppContextBuilder {

 private WebAppContext webAppContext; public WebAppContext buildWebAppContext() { webAppContext = new WebAppContext(); webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml"); webAppContext.setResourceBase("src/main/webapp"); webAppContext.setContextPath("/Holmes"); return webAppContext; } } 

to

 public class AppContextBuilder { private WebAppContext webAppContext; public WebAppContext buildWebAppContext() { webAppContext = new WebAppContext(); webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml"); webAppContext.setResourceBase(webAppContext); webAppContext.setContextPath("/Holmes"); return webAppContext; } } 

try it let me know

0
source share

Are stack trace logs available?

Without additional logs, I can only imagine, based on experience, that this could be caused by directory resolution, it worked fine in your IDE, as it can find the files you need without any additional context setting components, which may not be the case when You are performing a real deployment.

0
source share

Maybe the url pattern is wrong, try changing something like:

  <servlet-mapping> <servlet-name>StrutsController</servlet-name> <url-pattern>*</url-pattern> </servlet-mapping> 
0
source share

Edit

 Handler[] handlers = new Handler[]{new AppContextBuilder().buildWebAppContext().getHandler()}; 

to

 Handler[] handlers = new Handler[]{new AppContextBuilder().buildWebAppContext()}; 
-one
source share

All Articles