Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My class is not a servlet error

Tags:

java

servlets

I have the following servlet code

public void doPost(HttpServletRequest request, HttpServletResponse response){

    Backup bup = new Backup();
    bup.doBackup();

    response.setContentType("text/html");
    PrintWriter out;
    try {
        out = response.getWriter();

    out.println("You backed up your data - well hopefully");
    out.flush();
    out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

And the following stack trace

javax.servlet.ServletException: Class view.BackupServlet is not a Servlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) java.lang.Thread.run(Thread.java:619)

root cause

java.lang.ClassCastException: view.BackupServlet cannot be cast to javax.servlet.Servlet org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) java.lang.Thread.run(Thread.java:619)

and the web.xml is

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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-<br>app_2_4.xsd">
<display-name>Backup</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description>BackupServlet</description>
    <display-name>BackupServlet</display-name>
    <servlet-name>BackupServlet</servlet-name><br>
    <servlet-class>view.BackupServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BackupServlet</servlet-name>
    <url-pattern>/BackupServlet</url-pattern>
  </servlet-mapping>
</web-app>`
like image 494
Ankur Avatar asked Jun 24 '09 06:06

Ankur


People also ask

What is servlet error?

The servlet ErrorHandler is defined in usual way as any other servlet and configured in web. xml. If there is any error with status code either 404 (Not Found) or 403 (Forbidden ), then ErrorHandler servlet would be called.

How do you create a class as a servlet in Java?

To create a servlet, write a public Java class that includes basic I/O support as well as the package javax. servlet. The class must extend either GenericServlet or HttpServlet. Because Sun Java System Web Server 7.0 servlets exist in an HTTP environment, the latter class is recommended.

What is a servlet class?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

What are the problems with servlets?

For non-java developers, servlet is not suitable as they required to have a broad knowledge of Java servlet. HTML code is mixed up with Java code therefore, changes done in one code can affect another code. Writing HTML code in servlet programming is very difficult. It also makes servlet looks bulky.


2 Answers

You haven't shown your class declaration - my guess is that your class doesn't extend Servlet or HttpServlet.

If it does, then check how many different servlet.jar files you have in your deployment - it could be that it's being loaded by two different classloaders.

like image 59
Jon Skeet Avatar answered Nov 24 '22 07:11

Jon Skeet


Your class containing the method doPost must extend javax.servlet.Servlet, but preferrably javax.servlet.HttpServlet

public class BackupServlet extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) {
     .
     .
     .
    }
}
like image 29
victor hugo Avatar answered Nov 24 '22 06:11

victor hugo