Jetty doesn't pick my spring application

I have a Spring MVC application where I use hibernate, freemarker. It is configured as a project with several maven. I am using IntelliJ ultimate.

The berth starts fine, but when I go to

http://localhost:8080/ 

It just displays the folders of my project, and I can view the source code in a browser!

Here is my setup now:

  final Server server = new Server(8080); ProtectionDomain domain = HttpServer.class.getProtectionDomain(); URL location = domain.getCodeSource().getLocation(); WebAppContext webAppContext = new WebAppContext(); webAppContext.setResourceBase(location.toExternalForm()); webAppContext.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml"); webAppContext.setContextPath("/"); webAppContext.setParentLoaderPriority(true); server.setHandler(webAppContext); server.start(); server.join(); 

My project layout is a multi-maven project (using intelli J), the layout is similar:

 /myapp/src/main/java/main.java (this contains the above code to start jetty) /myapp/src/main/webapp /myapp/src/main/webapp/assets /myapp/src/main/webapp/WEB-INF /myapp/src/main/webapp/WEB-INF/web-context.xml (spring config file) /myapp/src/main/webapp/WEB-INF/web.xml /myapp/src/main/webapp/WEB-INF/views/ (parent folder for my freemarker template files) /myapp/src/main/webapp/WEB-INF/views/home/index.ftl 

My web.xml:

 <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath*:log4j.properties</param-value> </context-param> <servlet> <servlet-name>myapp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/web-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myapp</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

When I run this in IntelliJ (11 ulimitate), I get the following output:

 2012-08-15 19:17:11,611 [main] INFO org.eclipse.jetty.server.Server - jetty-7.6.2.v20120308 2012-08-15 19:17:11,886 [main] INFO org.eclipse.jetty.webapp.StandardDescriptorProcessor - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet 2012-08-15 19:17:11,962 [main] INFO org.eclipse.jetty.server.handler.ContextHandler - started oejwWebAppContext{/,file:/Users/me/projects/myapp/myapp-web/target/classes/} 2012-08-15 19:17:12,021 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started SelectChannelConnector@0.0.0.0 :8080 

This obviously doesn't work, because when I run it using tomcat w / intelliJ, I get huge output for things like hibernate, spring, etc.

My pom.xml for the web module has:

 .. <packaging>war</packaging> .. 
+4
source share
4 answers

If you intend to run this during production, and you are already using maven to build your application, I suggest creating the actual war package first:

 mvn clean package 

Then, as soon as you create your webapp, create a new Jetty server that runs from the military archive (and not the source code).

As stated in the Jetty manual, this should be something like this:

  Server server = new Server(8080); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setWar("path_to_the_war_file/your_app.war"); server.setHandler(webapp); server.start(); server.join(); 
+4
source

you probably miss the context loader listener.

 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> 
+2
source

Instead of calling Jetty manually from main.java , refer to the Maven Jetty plugin in pom.xml and run the application by running mvn jetty:run

 <build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.4.v20120524</version> </plugin> </plugin> </build> 

If instead you should create a WAR executable with an embedded copy of Jetty, you need to approach the task like this:

  final Connector connector = new SelectChannelConnector(); connector.setPort(8080); final Server server = new Server(); server.addConnector(connector); server.setStopAtShutdown(true); final WebAppContext context = new WebAppContext(); context.setContextPath("/"); context.setServer(server); final ProtectionDomain protectionDomain = Main.class.getProtectionDomain(); final URL location = protectionDomain.getCodeSource().getLocation(); context.setWar(location.toExternalForm()); server.addHandler(context); server.start(); server.join(); 
0
source

You tried to set the defaultsDescriptor parameter

 webAppContext.setDefaultsDescriptor(JETTY_HOME+"/etc/webdefault.xml"); 

JETTY_HOME is the place where you installed the marina, you can find JETTY_HOME / etc / webdefault.xml containing the basic settings.

0
source

All Articles