HTTP Status 500 - Error instantiating servlet clas

2019-02-12 18:29发布

问题:

I am creating simple servlet and deploying it in tomcat server but I am getting the following error:

HTTP Status 500 - Error instantiating servlet class pkg.coreServlet

File Structure on the tomcat server:

webapps     
| 
- aarya
  |
  - WEB-INF
    |
     -web.xml
     -src(folder)
       |
       -pkg
         |
         -coreServlet.class

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<servlet>
    <servlet-name>aaryaservlet</servlet-name>
    <servlet-class>pkg.coreServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>aaryaservlet</servlet-name>
        <url-pattern>/coreServlet</url-pattern>
    </servlet-mapping>
</web-app>

coreServlet.java:

package pkg;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; 

public class coreServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req,HttpServletResponse res)
 throws ServletException,IOException


   {
        PrintWriter out = res.getWriter();
        res.setContentType("text/html");
        out.println("this is First servlet Example ");
    }
}

url I am giving is http://localhost:8080/aarya/coreServlet I try by restarting tomcat but I am getting same error. Where I am doing wrong?

回答1:

Do not put the src folder in the WEB-INF directory!!



回答2:

Change the

private static final long serialVersionUID = 1L;

to any other value like

private static final long serialVersionUID = 102831973239L;

also you can generate it automatically in eclipse.

It is because each servlet in a app has a unique id.and tomcat causes problem with two servlets having same id...



回答3:

Have you closed the < web-app > tag in your web.xml? From what you have posted, the closing tag seems to be missing.



回答4:

The servlet class should be in the WEB-INF/classes not WEB-INF/src.



回答5:

In my case missing private static final long serialVersionUID = 1L; line caused the same error. I added the line and it worked!



回答6:

I had an issue with Servlet instantiation. I cleaned the project and it worked for me. In eclipse menu, Go to Project->Clean. It should work.



回答7:

Try This:)

before:-

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>TestServlet</servlet-class>  
</servlet>

After:-

 <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>operation.TestServlet</servlet-class>
 </servlet>