我遇到了以下问题: 我试图用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
输入例外报告
message实例化servlet类时出错 org.servlets.CAddressBookServlet
description服务器遇到阻止它的内部错误 完成此请求。
例外
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.run(未知来源) java.lang.Thread.run(未知来源)
根本原因
java.lang.NoClassDefFoundError: 组织/休眠/ cfg /中AnnotationConfiguration org.servlets.CAddressBookServlet。(CAddressBookServlet.java:33) sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)java.lang.reflect.Constructor.newInstance(未知来源) java.lang.Class.newInstance0(未知来源) java.lang.Class.newInstance(未知来源) 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.run(未知来源) java.lang.Thread.run(未知来源)
根本原因
抛出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(本机方法) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)java.lang.reflect.Constructor.newInstance(未知来源) java.lang.Class.newInstance0(未知来源) java.lang.Class.newInstance(未知来源) 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.run(未知来源) java.lang.Thread.run(未知来源)
注意Apache中提供了根本原因的完整堆栈跟踪 Tomcat / 7.0.33日志。
如何让hibernate使用我的servlet?
答案 0 :(得分:2)
@Perception有正确的答案。您需要将Hibernate库包含在已部署的war文件中。您可以将它们放在WEB-INF / lib或Tomcat背书目录中。我更喜欢WEB-INF / lib位置,因此您不会干扰服务器上的任何其他应用程序。
答案 1 :(得分:0)
我认为你错过了注释jar。 Download the Annotations
将下载的jar文件复制到其中一个位置....
-> Tomcat lib directory or -> To the folder webapps/< your_project >/WEB-INF/lib
然后重启服务器