2

I have try to run this program on Tomcat 7.0v server but getting a exception java.lang.ClassCastException: bean.Customer cannot be cast to javax.servlet.Servlet throw.


Bean class

 public class Customer {

@Id
private Integer id;
@Column
private String name;
@Column
private String address;
@Column
private String city;
@Column
private String postalcode;
@Column
private String country;

public Customer() {}

public Customer(Integer id, String name, String address, String city, String postalcode, String country) {
    this.id = id;
    this.name = name;
    this.address = address;
    this.city = city;
    this.postalcode = postalcode;
    this.country = country;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getPostalcode() {
    return postalcode;
}

public void setPostalcode(String postalcode) {
    this.postalcode = postalcode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}   

}

Servlet Code

public void init(ServletConfig config) throws ServletException {
    factory = new Configuration().configure("resources/mysql.cfg.xml").buildSessionFactory();}

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    int first = Integer.parseInt(request.getParameter("fr"));
    int second = Integer.parseInt(request.getParameter("mr"));

    Session session = factory.openSession();
    Criteria criteria = session.createCriteria(Customer.class);
    List customers = criteria.list();
    Iterator it = customers.iterator();

    while (it.hasNext()) {
        Customer customer = (Customer)it.next();
        out.println(customer.getId());
        out.println(customer.getName());
        out.println(customer.getAddress());
        out.println(customer.getCountry());
        out.println(customer.getPostalcode());
        out.println("===============================");
    }

    session.close();

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
    <servlet>
<servlet-name>Customer</servlet-name>
<servlet-class>bean.Customer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Customer</servlet-name>
<url-pattern>/data</url-pattern>
</servlet-mapping>
</web-app>      

Error

java.lang.ClassCastException: bean.Customer cannot be cast to javax.servlet.Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3988)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:425)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:745)
3
  • 1
    please paste your whole servlet code, this seems to be only a portion Commented Feb 17, 2017 at 14:18
  • 2
    Let me guess you added your Customer as a servlet to your web.xml... Commented Feb 17, 2017 at 14:21
  • Thank you #M Deinum sir actually i'm adding the Servlet Class but i'm Mention the Customers class. Commented Feb 17, 2017 at 20:21

2 Answers 2

3

You are telling your web container that bean.Customer is a Servlet with the directive

<servlet>
  <servlet-name>Customer</servlet-name>
  <servlet-class>bean.Customer</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

But this class does not implement javax.servlet.Servlet. You should configure the correct servlet class in your web.xml.

Sign up to request clarification or add additional context in comments.

2 Comments

Ng Sharma, can you provide any more specific info on how this was resolved (i.e., what does "configure the correct servlet class" mean in this context)?
What fixed this error for me was going into the pom.xml and setting the javax.servlet-api dependency to provided (stackoverflow.com/questions/17799295/…).
3

Make sure Your Customer class implements with HttpServlet for eg. public class Customer extends HttpServlet

1 Comment

It works, but your project may need to import javax.servlet.jar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.