在eclipse helios和maccat上的tomcat 7中,我在哪里出错了这个servlet?

时间:2011-06-01 17:48:46

标签: java eclipse tomcat

我正在尝试使用eclipse helios ee和tomcat 7创建一个基本的servlet。我可以看到tomcat工作正常,因为我可以从eclipse启动服务器,将我的浏览器指向localhost:8080并且它提供了欢迎页面。

所以我使用动态Web项目向导创建了一个servlet,但是在尝试查看它时我得到了404。我还可以看到WEB-INF目录中没有它的web.xml文件。 eclipse应该自动创建还是我必须做的事情?在我看过的所有教程中,似乎自动创建了web.xml?

感谢您的帮助 - 抱歉这个问题有点模糊 - 我不确定从哪里开始寻找解决方案..

谢谢!

编辑: 我试过手动创建一个web.xml文件。我可以看到它在正确的位置(我认为)让服务器看到它:

/Users/bw/Documents/workspace2/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/HelloWorld/WEB-INF/web.xml

,文件如下所示:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>mytest.HelloWorld</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

编辑: Catalina日志看起来像:

Jun 1, 2011 11:37:56 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Jun 1, 2011 11:37:56 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWorld' did not find a matching property.
Jun 1, 2011 11:37:56 AM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 1, 2011 11:37:56 AM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jun 1, 2011 11:37:56 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1154 ms
Jun 1, 2011 11:37:56 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 1, 2011 11:37:56 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.14
Jun 1, 2011 11:37:57 AM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jun 1, 2011 11:37:57 AM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 1, 2011 11:37:57 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 583 ms

编辑:

尝试从服务器中删除应用并获得此信息:

Jun 1, 2011 12:03:28 PM org.apache.catalina.loader.WebappClassLoader modified
SEVERE:     Resource '/WEB-INF/classes/mytest/HelloWorld.class' is missing    Jun 1, 2011 12:03:28 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/HelloWorld] has started
Jun 1, 2011 12:03:29 PM org.apache.catalina.core.StandardContext resourcesStart
SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base /Users/bw/Documents/workspace2/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/HelloWorld does not exist or is not a readable directory
    at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)
    at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4836)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5016)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3854)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:424)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1205)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1391)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1401)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1401)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1380)
    at java.lang.Thread.run(Thread.java:637)
Jun 1, 2011 12:03:29 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error in resourceStart()
Jun 1, 2011 12:03:29 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Jun 1, 2011 12:03:29 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/HelloWorld] startup failed due to previous errors
Jun 1, 2011 12:03:29 PM org.apache.catalina.util.LifecycleBase stop
INFO: The stop() method was called on component [Pipeline[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]] after stop() had already been called. The second call will be ignored.
Jun 1, 2011 12:03:29 PM org.apache.catalina.util.LifecycleBase stop
INFO: The stop() method was called on component [WebappLoader[/HelloWorld]] after stop() had already been called. The second call will be ignored.
Jun 1, 2011 12:03:29 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/HelloWorld] is completed

看起来这些类没有从构建目录复制到服务器区域?

编辑:

AHA!终于找到了问题。好吧,可能有好几件事。

1)我尝试删除服务器,然后创建一个新服务器,然后添加servlet。似乎它可能已经混淆了,并没有将必要的文件复制到服务器区域。

2)Eclipse给了我错误的url来查看servlet。我正在查看/ hello,我需要查看HelloWorld / hello - 看起来Eclipse在建议servlet的URL时可能不包含上下文?

当我更清楚地知道出了什么问题时,我会发回信息。

感谢大家的帮助!

2 个答案:

答案 0 :(得分:2)

需要有一个映射到您创建的Servlet的URL。如果你使用了向导,它应该是自动创建的。您是否在web.xml文件中尝试过creating and entering it

编辑:您是否尝试删除工作区或重命名并尝试使用新工作区?如果这不起作用,那么你可以看看如何自己create web.xml files

编辑:如果您使用错误的URL访问它,那肯定会导致404错误。如果您在eclipse中运行项目,则web.xml文件中的URL将映射到example.com/project_name/servlet。

您甚至可以创建custom 404 pages并显示比现在显示的信息更多或更少的信息,或者只是让它看起来不同。

答案 1 :(得分:0)

尝试禁用tomcat服务器编辑器中的“不发布服务模块”选项,然后重新启动服务器。