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:
- Create a new project called "test".
- Create a new index.jsp file.
Create a new
servlet
called "Testservlet".Package name: testpackage
- 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:
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?