简体   繁体   English

java.lang.ClassCastException:无法转换为javax.servlet.Servlet

[英]java.lang.ClassCastException: cannot be cast to javax.servlet.Servlet

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. 我尝试在Tomcat 7.0v服务器上运行此程序,但遇到异常java.lang.ClassCastException:bean.Customer无法转换为javax.servlet.Servlet抛出。


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 Servlet代码

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

You are telling your web container that bean.Customer is a Servlet with the directive 您正在告诉Web容器bean.Customer是带有指令的Servlet

<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. 但是此类不实现javax.servlet.Servlet。 You should configure the correct servlet class in your web.xml. 您应该在web.xml中配置正确的servlet类。

Make sure Your Customer class implements with HttpServlet for eg. 确保您的Customer类使用HttpServlet实现。 public class Customer extends HttpServlet 公共类Customer扩展HttpServlet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:org.glassfish.jersey.servlet.ServletContainer无法转换为javax.servlet.Servlet - java.lang.ClassCastException: org.glassfish.jersey.servlet.ServletContainer cannot be cast to javax.servlet.Servlet java.lang.ClassCastException:无法将 Servlet.Telnet 转换为 javax.servlet.Servlet - java.lang.ClassCastException: Servlet.Telnet cannot be cast to javax.servlet.Servlet java.lang.ClassCastException:无法将spark.servlet.SparkFilter强制转换为javax.servlet.Filter - java.lang.ClassCastException: spark.servlet.SparkFilter cannot be cast to javax.servlet.Filter javax.servlet.ServletException: java.lang.ClassCastException: XXX 不能转换为 spark.servlet.SparkApplication - javax.servlet.ServletException: java.lang.ClassCastException: XXX cannot be cast to spark.servlet.SparkApplication RepositoryRestMvcConfiguration无法强制转换为javax.servlet.Servlet - RepositoryRestMvcConfiguration cannot be cast to javax.servlet.Servlet HTTP状态500-java.lang.ClassCastException:rest.rest页面无法转换为javax.servlet.http.HttpServletRequest - HTTP Status 500 - java.lang.ClassCastException: rest.rest Page cannot be cast to javax.servlet.http.HttpServletRequest eclipse servlet java.lang.ClassNotFoundException:javax.servlet.Servlet - eclipse servlet java.lang.ClassNotFoundException: javax.servlet.Servlet 尝试使用 Servlet 打包 REST App 时如何修复“无法转换为 javax.servlet.Servlet”错误 - How to fix 'cannot be cast to javax.servlet.Servlet' error while trying to package REST App with Servlet Java异常:java.lang.ClassCastException:javax.swing.Timer无法转换为javax.swing.JButton - Java exception: java.lang.ClassCastException: javax.swing.Timer cannot be cast to javax.swing.JButton java.lang.ClassCastException:MainActivity无法强制转换? - java.lang.ClassCastException: MainActivity cannot be cast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM