crm crm - 4 months ago 60
Java Question

Tomcat 7 server failed to start after creating a servlet using eclipse

There error I get when I attempt to start the local Tomcat 7 server in Eclipse:


'Starting Tomcat v7.0 Server at localhost' has encountered a problem.
Server Tomcat v7.0 Server at localhost failed to start.


These are the steps I took:


  1. Create a new project called "test".

  2. Create a new index.jsp file.

  3. Create a new
    servlet
    called "Testservlet".

    Package name: testpackage

  4. Configure a new build path for Web App Libraries. Add an External JAR from the Tomcat 7 directory I add
    servlet-api.jar
    .



This is the path structure in Eclipse:

enter image description here

The contents of the
web.xml
file:

<?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" id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>Testservlet</servlet-name>
<servlet-class>testpackage.Testservlet</servlet-class>
</servlet>

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


Contents of the
Testservlet.java
file:

package testpackage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(description = "a test servlet", urlPatterns = { "/Testservlet" })
public class Testservlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("This is a test servlet");
}
}


Next I add the project to the Tomcat server and attempt to start the server. Which is when I get the error stopping the server from running.

If I remove the servlet related markup from the web.xml file the server starts perfectly fine. This is the markup I remove:

<servlet>
<servlet-name>Testservlet</servlet-name>
<servlet-class>testpackage.Testservlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Testservlet</servlet-name>
<url-pattern>/Testservlet</url-pattern>
</servlet-mapping>


I assume that I need the above markup in the web.xml in order for the servlet to display when I navigate to test/Testservlet though? Especially when I deploy the project to a remote server.

What am I doing wrong here?

Answer

Servlet API 3.0 included a new package called javax.servlet.annotation which provides annotations like @WebServlet to be used to annotate a servlet class and this can eliminate the use of servlet configuration in web.xml. So, servlet and servlet-mapping are obsolete.

Also you should not deploy servlet-api.jar with your application. Tomcat 7 already has these classes and it supports many Servlet APIs. It will automatically choose which servlet API to load using your web application deployment descriptor.

Comments