HTTP Status 500 - Error instantiating servlet class pkg.coreServlet
Asked Answered
E

10

12

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?

Eldoraeldorado answered 10/4, 2013 at 9:8 Comment(0)
B
14

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

Basuto answered 10/4, 2013 at 9:11 Comment(8)
I am doing with the help of mkyong.com/servlet/a-simple-servlet-example-write-deploy-run Is, that is wrong ?Eldoraeldorado
even in this, src is out side of WEB-INFSheik
Yeah. He is putting src folder just in the root directory.Basuto
@NickJ Does folder name really matters, I mean I am putting .class files in src, does it matters ?Eldoraeldorado
Step #5 shows the correct directly structure. I think you got your source and deployed directories mixed upSaar
yes, it does affect, otherwise container wont be able to find class filesSheik
Ya in step#5 I changed classes into src, I am asking that name of folder is really matters ?Eldoraeldorado
@TechDon it is very important to get the directory names right. Tomcat will look for classes in a directory named classes, or jars in a directory called lib. It won't find them anywhere else.Saar
S
8

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...

Shaver answered 9/6, 2014 at 10:21 Comment(0)
W
5

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

Westberg answered 29/3, 2015 at 6:21 Comment(0)
P
4

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

Patricapatrice answered 10/4, 2013 at 9:15 Comment(1)
Sorry I miss that in post.Eldoraeldorado
H
4

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

Hultin answered 10/4, 2013 at 9:17 Comment(0)
W
2

The above error can occur for multiple cases during servlet startup / request. Hope you check the full stack trace of the server log, If you have tomcat, you can also see the exact causes in html preview of the 500 Internal Server Error page.

Weird thing is, if you try to hit the request url a second time, you would get 404 Not Found page.

You can also debug this issue, by placing breakpoints on all the classes constructor initialization block, whose objects are created during servlet startup/request.

In my case, I didn't had javaassist jar loaded for the Weld CDI injection to work. And it shown NoClassDefFound Error.

Wist answered 5/1, 2020 at 17:54 Comment(1)
so how to fix this error? i have tried many solutions but nothing works for me.Jerkin
Z
0

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.

Zareba answered 14/4, 2017 at 18:28 Comment(0)
T
0

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>
Tamtama answered 6/12, 2017 at 6:22 Comment(0)
S
0

Make sure the following:

  1. Proper "war" file structure i.e. WEB-INF & META-INF
  2. "web.xml" file is setup correct.
  3. Last and important: private static final long serialVersionUID = 1L; should be there in your class (<servlet-class>MyClass</servlet-class>).
Switcheroo answered 27/4, 2019 at 18:38 Comment(0)
U
0

might be helpful for someone who seeks...

works for me.

  • keeping the servlet-name same as class name.
  • clean and rebuild (make sure your other projects are closed).
Unbuild answered 10/1, 2023 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.