1

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?

crmepham
  • 4,676
  • 19
  • 80
  • 155
  • 'Starting Tomcat v7.0 Server at localhost' has encountered a problem. Server Tomcat v7.0 Server at localhost failed to start. – crmepham May 25 '14 at 14:58
  • Is there any more detail about the error? Look at the `logs` folder stored under tomcat folder – Braj May 25 '14 at 15:01
  • Please go to your work space folder and then navigate to [workspace_folder]\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\logs check logs here to find what's going on. – vkg May 25 '14 at 15:02

2 Answers2

3

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I don't suppose you might know why I get a 404 error when I navigate to test/Testservlet after I've deployed this project to my remote Tomcat7 server? – crmepham May 25 '14 at 15:52
  • Did you create `test.war` or what is the context of your application? The project name is not the same as context used to deploy the application. You should specify context at which the application should be deployed. – Roman C May 25 '14 at 15:56
  • I created a test.war and deployed it in `webapps`. It creates the test directory, which I place in webapps/ROOT/. I can navigate to the index.jsp file fine with `http://host.biz:8080/test/` but I get a 404 error when i try to navigate to the Testservlet at `http://host.biz:8080/test/Testservlet` – crmepham May 25 '14 at 16:01
  • 1
    ROOT is for context `/`, which you should use in the url instead of `/test` and why you doing it manually? Just copy `test.war` to the `webapps` it will autodeploy. Or the better use the Tomcat manager to do it, but this requires privileges to work with manager. – Roman C May 25 '14 at 16:06
  • It does autodeploy when I place it in `webapps` but I have to move it inside ROOT to be able to navigate to it and it doesn't autodeploy in ROOT, I don't know why on both counts. I don't understand your first point in relation to the Testservlet 404. Thanks for helping. – crmepham May 25 '14 at 16:10
  • Why you want to move it to ROOT? You should not do it, just configure Tomcat to use your application with the root context, but test folder should be in `webapps` if it autodeployed. Dirty method is rename `test.war` to `ROOT.war` before copying to `webapps` it will deploy at ROOT then overriding everything that was before. If you will try that make backup of the ROOT folder before deployment. – Roman C May 25 '14 at 16:18
  • So it has to be in webapps to be able to navigate to test/Testservlet? – crmepham May 25 '14 at 16:27
  • yes, and if you move it to ROOT then use `/Testservlet`. – Roman C May 25 '14 at 16:28
  • Just removing servlet tags from web.xml helped. – shermi Aug 05 '16 at 13:43
0

Best solutions is add server Runtime at the time of creation of servlet 1.Add server runtime at sevelet name creation 2.Add specified tomcat installed locations