可执行war中的嵌入式jetty无法找到web.xml中指定的servlet

时间:2014-01-05 06:24:15

标签: servlets war embedded-jetty

我有一个测试项目,它在可执行的战争中使用嵌入式jetty(版本9)从web.xml部署描述符运行网站。 Jetty无法在类路径上找到我的servlet类,我不知道为什么。

这是堆栈跟踪:

java.lang.ClassNotFoundException: org.dyndns.awj.web.servlet.MainServlet.class
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:436)
        at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:389)
        at org.eclipse.jetty.util.Loader.loadClass(Loader.java:86)
        at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:125)
        at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:294)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:809)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1346)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:743)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:491)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
        at org.eclipse.jetty.server.Server.start(Server.java:355)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
        at org.eclipse.jetty.server.Server.doStart(Server.java:324)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at JettyApp.main(JettyApp.java:45)
2014-01-04 23:41:32.968:WARN:/:main: unavailable
javax.servlet.UnavailableException: org.dyndns.awj.web.servlet.MainServlet.class
        at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:132)
        at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:294)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:809)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1346)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:743)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:491)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
        at org.eclipse.jetty.server.Server.start(Server.java:355)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
        at org.eclipse.jetty.server.Server.doStart(Server.java:324)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at JettyApp.main(JettyApp.java:45)
2014-01-04 23:41:32.972:WARN:oejuc.AbstractLifeCycle:main: FAILED andrew@abc35b6d==org.dyndns.awj.web.servlet.MainServlet.class,-1,false: javax.servlet.UnavailableException: org.dyndns.awj.web.servlet.Ma
inServlet.class
javax.servlet.UnavailableException: org.dyndns.awj.web.servlet.MainServlet.class
        at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:132)
        at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:294)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:809)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1346)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:743)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:491)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
        at org.eclipse.jetty.server.Server.start(Server.java:355)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
        at org.eclipse.jetty.server.Server.doStart(Server.java:324)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at JettyApp.main(JettyApp.java:45)
2014-01-04 23:41:32.973:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@c666ef0{/,file:/tmp/jetty-0.0.0.0-8080-website.war-_-any-1780354419120987955.dir/webapp/,STARTING}{fi
le:/home/aj/Documents/website.war}
javax.servlet.UnavailableException: org.dyndns.awj.web.servlet.MainServlet.class
        at org.eclipse.jetty.servlet.Holder.doStart(Holder.java:132)
        at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:294)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:809)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1346)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:743)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:491)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
        at org.eclipse.jetty.server.Server.start(Server.java:355)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
        at org.eclipse.jetty.server.Server.doStart(Server.java:324)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
        at JettyApp.main(JettyApp.java:45)
2014-01-04 23:41:32.999:INFO:oejs.ServerConnector:main: Started ServerConnector@4896ceb3{HTTP/1.1}{0.0.0.0:8080}

这个项目使用maven来构建战争,pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.dyndns.awj.web</groupId>
  <artifactId>website</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>website</name>
  <description>My Test Website</description>

  <repositories>
    <repository>
        <id>Maven Central</id>
        <url>http://repo1.maven.org/maven2/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-jsp</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-annotations</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-plus</artifactId>
        <version>9.1.0.RC2</version>
    </dependency>
    <dependency> 
        <groupId>javax.faces</groupId> 
        <artifactId>javax.faces-api</artifactId> 
        <version>2.2</version> 
    </dependency> 
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <executions>
              <execution>
                  <id>main-class-placement</id>
                  <phase>prepare-package</phase>
                  <configuration>
                      <tasks>
                          <move todir="${project.build.directory}/${project.artifactId}-${project.version}/">
                              <fileset dir="${project.build.directory}/classes/">
                                  <include name="JettyApp.class" />
                              </fileset>
                          </move>
                      </tasks>
                  </configuration>
                  <goals>
                      <goal>run</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
           <warName>${project.artifactId}</warName>
           <archive>
               <manifest>
                   <mainClass>JettyApp</mainClass>
               </manifest>
           </archive>
           <failOnMissingWebXml>true</failOnMissingWebXml>
           <warSourceDirectory>webapps</warSourceDirectory>
       </configuration>
       <executions>
           <execution>
               <id>default-war</id>
               <phase>package</phase>
               <goals>
                   <goal>war</goal>
               </goals>
           </execution>
       </executions>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.3</version>
         <executions>
             <execution>
                 <id>jetty-classpath</id>
                 <phase>prepare-package</phase>
                 <goals>
                     <goal>unpack-dependencies</goal>
                 </goals>
                 <configuration>
                     <includeGroupIds>org.eclipse.jetty,javax.servlet,org.objectweb.asm</includeGroupIds>
                     <excludeArtifactIds>jsp-api,jstl</excludeArtifactIds>
                     <excludes>META-INF/ECLIPSEF.*</excludes>
                     <outputDirectory>
                         ${project.build.directory}/${project.artifactId}-${project.version}
                     </outputDirectory>
                 </configuration>
             </execution>
         </executions>
     </plugin>
    </plugins>
  </build>
</project>

JettyApp.java如下所示:

import java.net.URL;
import java.security.ProtectionDomain;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyApp {
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        ProtectionDomain domain = JettyApp.class.getProtectionDomain();
        URL location = domain.getCodeSource().getLocation();
        webapp.setWar(location.toExternalForm());
        server.setHandler(webapp);

        server.start();
        server.join();
    }
}

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>website</display-name>
  <servlet>
    <servlet-name>andrew</servlet-name>
    <servlet-class>org.dyndns.awj.web.servlet.MainServlet.class</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>andrew</servlet-name>
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

servlet的源代码,MainServlet,位于WEB-INF / classes /中的war内:

package org.dyndns.awj.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MainServlet
 */
public class MainServlet extends HttpServlet {
    private Logger logger = Logger.getLogger(MainServlet.class.getCanonicalName());
    /**
     * 
     */
    private static final long serialVersionUID = 3075545586281194740L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getPathInfo();
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("<html><head><title>Website!</title><body>aoeu</body></html>");
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

0 个答案:

没有答案