2

我遇到了以下问题:我尝试使用 Eclipse 创建一个简单的 Servlep:

@WebServlet(description = "Adress book servlet", urlPatterns = {"/new_address"})
public class CAddressBookServlet extends HttpServlet 
{
    public CAddressBookServlet() 
    {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {}
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {}
    private static final long serialVersionUID = 1L;
}

它适用于Tomcat!但后来我要在我的项目中添加 Hibernate 功能。我只向 servlet 构造函数添加了一行:

@WebServlet(description = "Adress book servlet", urlPatterns = {"/new_address"})
public class CAddressBookServlet extends HttpServlet 
{
    public CAddressBookServlet() 
    {
        super();
        new AnnotationConfiguration();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {}
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {}
    private static final long serialVersionUID = 1L;
}

导致以下错误:

HTTP 状态 500 - 实例化 servlet 类 org.servlets.CAddressBookServlet 时出错


类型异常报告

消息实例化 servlet 类 org.servlets.CAddressBookServlet 时出错

描述 服务器遇到一个内部错误,阻止它完成这个请求。

例外

javax.servlet.ServletException: 实例化 servlet 类 org.servlets.CAddressBookServlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 时出错) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor .java:1004) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) java.util.concurrent .ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor$Worker。运行(未知来源) java.lang.Thread.run(未知来源)

根本原因

java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration org.servlets.CAddressBookServlet.(CAddressBookServlet.java:33) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun. reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect.Constructor.newInstance(Unknown Source) java.lang.Class.newInstance0(Unknown Source) java.lang.Class.newInstance(Unknown Source) org.apache.catalina. authenticationator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931) org. apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:第407章SocketProcessor.run(JIoEndpoint.java:310) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)

根本原因

java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) org .servlets.CAddressBookServlet.(CAddressBookServlet.java:33) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) java.lang.reflect .Constructor.newInstance(Unknown Source) java.lang.Class.newInstance0(Unknown Source) java.lang.Class.newInstance(Unknown Source) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org. apache.catalina.valves.ErrorReportValve。调用(ErrorReportValve.java:99)org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)org.apache.coyote。 http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java: 310) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor$Worker.run(未知来源) java.lang.Thread.run(未知来源)AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) java.lang.Thread.run(Unknown Source)运行(未知来源)运行(未知来源)

note Apache Tomcat/7.0.33 日志中提供了根本原因的完整堆栈跟踪。

如何使休眠与我的 servlet 一起工作?

4

2 回答 2

2

@Perception 有正确的答案。您需要在部署的 war 文件中包含 Hibernate 库。您可以将它们放在 WEB-INF/lib 或 Tomcat 认可的目录中。我更喜欢 WEB-INF/lib 位置,因此您不会干扰服务器上的任何其他应用程序。

于 2012-11-26T06:35:31.880 回答
0

我认为您缺少注释罐。 下载注释

将下载的 jar 文件复制到这些位置之一....

-> Tomcat 库目录
          或者
 -> 到文件夹 webapps/< your_project >/WEB-INF/lib  

并重启服务器

于 2012-11-26T06:25:16.780 回答