Where am I going wrong with this servlet in eclipse helios and tomcat 7 on mac?
I am trying to create a basic servlet with eclipse helios ee and tomcat 7. I can see that tomcat is working fine, because I can start the server from eclipse, point my browser at localhost:8080 and it comes up with the welcome page.
So I created a servlet with the dynamic web project wizard, but I get a 404 when trying to view it. I can also see that there is no web.xml file for it in the WEB-INF directory. Should eclipse be creating that automatically or is that something I have to do? In all the tutorials I've seen, it seems like web.xml gets created automatically?
Thanks for your help - sorry the question is a bit vague - I'm not really sure where to start looking for a solution..
Thanks!
EDIT: I've tried creating a web.xml file by hand. I can see that it's in the right place (I think) for the server to see it:
/Users/bw/Documents/workspace2/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/HelloWorld/WEB-INF/web.xml
and the file looks like this:
<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>
EDIT: Catalina log looks like:
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
EDIT:
Tried removing the app from the server and got this:
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开发者_如何学C, 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
It looks like the classes aren't getting copied from the build directory to the server's area?
EDIT:
AHA! Finally found the problem. Well, there may have been several things.
1) I tried deleting the server, and then creating a new one, and then adding the servlet. Seems like it may have got confused and not copied the necessary files across to the server area.
2) Eclipse was giving me the wrong url for viewing the servlet. I was looking at /hello and I needed to look at HelloWorld/hello - it looks like perhaps Eclipse doesn't include the context when it suggests the url for the servlet?
I'll post back when I have a clearer idea of what was going wrong..
Thanks all for your help!
There needs to be a URL mapped to the Servlet that you created. If you used the wizard it should have been created automatically though. Have you tried creating and entering it in a web.xml file?
Edit: Have you tried deleting your workspace or renaming it and trying with a new one? If that doesn't work then you can take a look at how to create web.xml files yourself.
Edit: If you were accessing it at the wrong URL then that would definitely cause the 404 error. The URLs in the web.xml file are mapped to example.com/project_name/servlet if you're running the project in eclipse.
You can even create custom 404 pages and show more or less information than is being shown right now or just make it look different.
Try disabling the "Serve modules without publishing" option your tomcat server editor and then restarting your server.
精彩评论