running and deploying servlet with eclipse and tom

2019-02-28 22:44发布

问题:

  1. I created a test project based on Tomcat HelloWorld Servlet with Eclipse
  2. and tried to run it from Eclipse as is with Tomcat 7, which I have configured to run on 127.0.0.1 - but I get Page cannot be found at 127.0.0.1/helloworld/HelloWorld
  3. I also tried exporting as war file and deploying it to the (otherwise working) Tomcat server running as a Windows service - and deployed with the Tomcat Application Manager - manifest.mf and the classes are nicely copied to tomcat/webapps/helloworld, but trying to navigate to 127.0.0.1/helloworld/HelloWorld fails again, showing HTTP Status 404

From default @WebServlet to web.xml configuration

Next, in HelloWorld.java, I tried commenting out

//@WebServlet("/HelloWorld")

and then adding a web-application-specific web.xml configuration:

<?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"
    version="3.0">
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
</web-app>

But the results are still the same - no response!

Solutions?

  • Given the linked tutorial provides instructions for Tomcat 6, should I change something to make it work with Tomcat 7?
  • Specifically, is the default @WebServlet("/HelloWorld") added by Eclipse sufficient? What is needed for the annotation-based configuration of Servlet 3.0 to work (without web.xml)?
  • Or could it be that something is blocking any web app deployment at the global tomcat server level? I have changed the server configuration somewhat, and unfortunately i do not remember exactly what, except for making it serve on 127.0.0.1 rather than 127.0.0.1:8080

回答1:

Open the properties page of your dynamic web project, click on "web project settings" and use the context root paramter as the root path for your URL.



回答2:

Try putting a default Index.html page in the WebContent and see if the welcome page points to it. If yes then try entering the servlet name manually and see if that works. I get the same error but doing so fixes it.

http://localhost:8080/<Project_Name>/index.html

http://localhost:8080/<Project_Name>/servlet_name