Search code examples
eclipseapachetomcateclipse-wtp

Integrating tomcat and eclipse but fails to test servlet?


I followed couple of links and i'm still having problem in deploying servlet page.

Reference Link: http://www.ibm.com/developerworks/opensource/library/os-eclipse-tomcat/index.html

Task Accomplished: 1. Able to integrate tomcat Apache 7.x with Eclipse [ Add the server ] 2. created JSP page to check for current date and time [ Create a sample page ] 3. Deploy the JSP page [ Deploy the page ]

Problem: 1. Created the server with package name "de.vogella.wtp.filecounter" and class name"HelloServlet.java".

followed below mentioned : Restart now appears in the Status column next to the Tomcat server. To restart the server, right-click the server in the Servers view, then select Restart > Start. Open a browser and navigate to http://localhost:8080/de.vogella.wtp.filecounter/HelloServlet (where de.vogella.wtp.filecounter is the name of your dynamic Web project).

Error: enter image description here

SOLUTION:

As Balus mentioned, worked perfect

Import :

import javax.servlet.annotation.WebServlet;

Added before the class declaration & after IMPORT :

@WebServlet("/hello")

Result :

enter image description here


Solution

  • This error means that the URL is wrong or that the servlet is not mapped in web.xml. It should match the <url-pattern> of the servlet as specified in its <servlet-mapping> in web.xml.

    If it's for example

    <url-pattern>/hello</url-pattern>
    

    and the context path of your project is indeed really de.vogella.wtp.filecounter (strange context path though):

    http://localhost:8080/de.vogella.wtp.filecounter/hello

    Or, since you're using Tomcat 7 already, you can also just use the new @WebServlet annotation instead, so that you don't need to fiddle with web.xml anymore. Put the following annotation on the servlet class:

    @WebServlet("/hello")
    

    This will map the servlet on the URL pattern of /hello.

    See also:

    • Our servlets wiki page - contains some hello world examples as well (and are more up to date than those old IBM/Vogella tutorials)