如何使用 Eclipse 为 Jetty 编写一个简单的 Servlet?

Posted

技术标签:

【中文标题】如何使用 Eclipse 为 Jetty 编写一个简单的 Servlet?【英文标题】:How do I write a simple Servlet for Jetty using Eclipse? 【发布时间】:2012-02-21 22:26:42 【问题描述】:

我下载了 Eclipse for Java EE 和 Jetty 8.1,并希望实现一个非常简单的 Servlet。

当我使用 java -jar start.jar 启动时,Jetty 工作正常。

为了开发 Servlet,我这样做了:

    在 Eclipse 中,我选择了“新建动态 Web 项目”。

    我在 Eclipse 的“构建路径”中添加了jetty\lib\servlet-api-3.0.jar

    在那个项目中,“New Servlet”,生成了一个 Servlet,我添加了一些代码:

    /**
     * Servlet implementation class MyServlet
     */
    @WebServlet("/MyServlet")
    public class MyServlet extends HttpServlet 
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, 
                    HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, 
                    HttpServletResponse response)
            throws ServletException, IOException 
            PrintWriter out = response.getWriter();
            out.println("<html><body><h1>My Servlet</h1></body></html>");
        
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, 
                    HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, 
                    HttpServletResponse response)
            throws ServletException, IOException 
            // TODO Auto-generated method stub
        
    
    
    

    我在WebContent\WEB-INF\web.xml 中创建了一个文件web.xml,内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <web-app 
    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"
    version="2.4">
    <servlet>
        <servlet-name>My Servlet</servlet-name>
    <servlet-class>MyServlet.class</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>My Servlet</servlet-name>
        <url-pattern>/Servlet</url-pattern>
    </servlet-mapping>
    

    我将build\classes中编译好的.class文件复制到WebContent\WEB-INF\classes

    现在我选择将项目作为.war-文件导出到jetty\webapps\MySite.war

    我以java -jar start.jar 启动Jetty 并访问localhost:8080/MySite/Servlet,但现在我收到此消息:

    Servlet Not Initialized
    
    javax.servlet.UnavailableException: Servlet Not Initialized
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:546)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:485)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:483)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:412)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
    at org.eclipse.jetty.server.Server.handle(Server.java:351)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:451)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:916)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:647)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:233)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:76)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:615)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
    at java.lang.Thread.run(Thread.java:722)
    

如果我访问localhost:8080/MySite/,我会看到两个文件夹WEB-INF\META-INF\,它们的大小都是0 bytes

所以它看起来不像我在 Eclipse 中的 .war-export 工作。

如何使用 Eclipse 为 Jetty 导出简单的 Servlet?


我现在已经为 Eclipse 安装了 Jetty WTP plugin,但我无法使用它运行这个 servlet:

2012-01-30 10:06:50.322:INFO:oejs.Server:jetty-8.1.0.RC4
2012-01-30 10:06:50.353:INFO:oejdp.ScanningAppProvider:Deployment monitor C:\Users\Jonas\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps at interval 1
2012-01-30 10:06:50.353:INFO:oejdp.ScanningAppProvider:Deployment monitor C:\Users\Jonas\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\contexts at interval 1
2012-01-30 10:06:50.353:INFO:oejd.DeploymentManager:Deployable added: C:\Users\Jonas\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\contexts\MySite.xml
2012-01-30 10:06:50.650:WARN:oejuc.AbstractLifeCycle:FAILED o.e.j.w.WebAppContext/MySite,file:/C:/Users/Jonas/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/MySite/,C:\Users\Jonas\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0/wtpwebapps/MySite: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
    at org.eclipse.jetty.annotations.AnnotationConfiguration.createAnnotationParser(AnnotationConfiguration.java:111)
    at org.eclipse.jetty.annotations.AnnotationConfiguration.configure(AnnotationConfiguration.java:73)
    at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:428)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1208)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:454)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:36)
    at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:183)
    at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:491)
    at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:138)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:142)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:53)
    at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:604)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:535)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:398)
    at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:332)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:118)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:552)
    at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:227)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.util.component.AggregateLifeCycle.doStart(AggregateLifeCycle.java:58)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:53)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:91)
    at org.eclipse.jetty.server.Server.doStart(Server.java:263)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1214)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:457)
    at org.eclipse.jetty.start.Main.start(Main.java:600)
    at org.eclipse.jetty.start.Main.main(Main.java:82)
Caused by: 
java.lang.ClassNotFoundException: org.objectweb.asm.ClassVisitor
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.eclipse.jetty.annotations.AnnotationConfiguration.createAnnotationParser(AnnotationConfiguration.java:111)
    at org.eclipse.jetty.annotations.AnnotationConfiguration.configure(AnnotationConfiguration.java:73)
    at org.eclipse.jetty.webapp.WebAppContext.configure(WebAppContext.java:428)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1208)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:454)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:36)
    at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:183)
    at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:491)
    at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:138)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:142)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:53)
    at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:604)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:535)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:398)
    at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:332)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:118)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:552)
    at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:227)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.util.component.AggregateLifeCycle.doStart(AggregateLifeCycle.java:58)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:53)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:91)
    at org.eclipse.jetty.server.Server.doStart(Server.java:263)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1214)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:457)
    at org.eclipse.jetty.start.Main.start(Main.java:600)
    at org.eclipse.jetty.start.Main.main(Main.java:82)

【问题讨论】:

【参考方案1】:

首先,你的 web.xml 是错误的。

servlet-class 元素包含 servlet 的完全限定类名。

从 web.xml 架构中,您在第 923 行使用了 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd。因此,第 9 行的 servlet-class 元素的内容应该是 MyServlet,而不是 MyServlet.class

其次,只使用一种方式注册一个servlet、过滤器或监听器以使其清晰。如果您更喜欢使用注解将它们注册到 servlet 容器。您最好删除 web.xml 中的 servlet 和 servlet-mapping 元素。否则,您最好删除 java 源代码中的注释。如果你想使用 web.xml,你应该将架构版本从 2.4 更新到 3.0

最后,您的第二个问题来自 Jetty WTP Eclipse 插件,可以尝试以下方法解决:jetty run error for websockets project

【讨论】:

我将 servlet-class 元素从 MyServlet.class 更改为 my.package.MyServlet 并注释掉了 @WebServlet-annotation,现在它可以工作了。谢谢。【参考方案2】:

您访问服务器的方式可能是错误的。 如果我们观察到你正在部署的战争是MySite.war 你使用的网址是localhost:8080\MyServlet\Servlet

试试localhost:8080\MySite\Servlet

【讨论】:

您也可以在 eclipse 中添加一个插件,通过 eclipse 本身连接到您的 Jetty 服务器。尝试添加新服务器。然后您可以直接在eclipse中运行您的应用程序,无需导出到war然后单独发布到Jetty 对不起,这是我的错字,我访问的是localhost:8080\MySite\Servlet。 URI ``localhost:8080\MyServlet\Servlet` 返回404 Not Found。我会看看插件,但它也应该可以导出战争文件。

以上是关于如何使用 Eclipse 为 Jetty 编写一个简单的 Servlet?的主要内容,如果未能解决你的问题,请参考以下文章

Jetty9安装部署

如何从 Eclipse 中导出 Jetty 启动配置 [重复]

如何让 eclipse-jetty 插件扫描和识别 websocket 类?

如何在Eclipse中正确安装Jetty插件并初步使用(图文详解)

如何使用jetty

eclips如何安装jetty插件