HTTP状态500 - javax.servlet.ServletException:java.lang.NoClassDefFoundError:org / hibernate / Session

时间:2016-01-30 11:34:41

标签: java hibernate jsp servlets

这里我在Eclipse Mars中使用JSP和Hibernate创建一个简单的注册表单。当我运行一个jsp页面时,我得到一个例外:

  

HTTP状态500 - javax.servlet.ServletException:   java.lang.NoClassDefFoundError:org / hibernate / Session。

这里我放了必需的文件和目录结构。

1)User.java

package com.nick.mypack;
 public class User {

 private int id;
 private String name,password,email;

 public int getId() {
    return id;
 }
 public void setId(int id) {
    this.id = id;
 }
 public String getName() {
    return name;
 }
 public void setName(String name) {
    this.name = name;
 }
 public String getPassword() {
    return password;
 }
 public void setPassword(String password) {
    this.password = password;
 }
 public String getEmail() {
    return email;
 }
 public void setEmail(String email) {
    this.email = email;
 }

 }

2)UserDao.java

package com.nick.mypack;

import org.hibernate.Session;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  

public class UserDao {  

 public static int register(User u){  
 int i=0;  
 Session session=new Configuration().  
 configure().buildSessionFactory().openSession();  

 Transaction t=session.beginTransaction();  
 t.begin();  

 i=(Integer)session.save(u);  

 t.commit();  
 session.close();  

return i;  
}  
}  

3)的index.jsp

<html>
  <form action="register.jsp" method="post">  
  Name:<input type="text" name="name"/><br><br/>  
  Password:<input type="password" name="password"/><br><br/>  
  Email ID:<input type="text" name="email"/><br><br/>  
  <input type="submit" value="register"/>"  

  </form>
</html>

4)register.jsp

<%@page import="com.nick.mypack.UserDao" %>
 <jsp:useBean id="obj" class="com.nick.mypack.User">  
 </jsp:useBean>  
 <jsp:setProperty property="*" name="obj"/>  

<%   
 int i=UserDao.register(obj);  
 if(i>0)  
 out.print("You are successfully registered");  
%>    

This is a directory structure of a Project.

请帮我解决这个问题。 提前谢谢!

2 个答案:

答案 0 :(得分:1)

好吧,你的错误是:

static void StartClient() { int port = 150; IPAddress ip = IPAddress.Parse("127.0.0.1"); TcpClient client = new TcpClient(); client.Connect(ip, port); // Do something with client. }

这意味着您的容器(Tomcat或您正在使用的任何服务器)无法加载该类。您可能没有正确打包部署。

您需要WEB-INF / lib中的Hibernate jar。它在这里看起来是空的,所以当你的应用程序运行时它们不在类路径上。

答案 1 :(得分:0)

确保配置正确

<强>配置

  1. 从[here] [1]下载Hibernate 5。
  2. 从里面的文件夹中提取必需的jar并将其添加到以下hibernate-release-5.0.7.Final.zip\hibernate-release-5.0.7.Final\lib\required的构建路径中。
  3. 确保将以下jar添加到项目路径(这应该位于WebApp的lib文件夹中)

    antlr-2.7.7.jar
    commons-dbcp2-2.1.1.jar
    dom4j-1.6.1.jar
    geronimo-jta_1.1_spec-1.1.1.jar
    hibernate-commons-annotations-5.0.1.Final.jar 
    hibernate-core-5.0.7.Final.jar
    hibernate-jpa-2.1-api-1.0.0.Final.jar
    jandex-2.0.0.Final.jar
    javassist-3.18.1-GA.jar
    javax.servlet.jsp.jstl-api-1.2.1-sources.jar
    jboss-logging-3.3.0.Final.jar
    sqljdbc4-3.0.jar //or whatever database you use
    hibernate-entitymanager-5.0.7.Final
    
  4. HibernateUtil.java

    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
    
         private static final SessionFactory sessionFactory = buildSessionFactory();
    
         private static SessionFactory buildSessionFactory() {
         try {
            // Create the SessionFactory from hibernate.cfg.xml
              return new Configuration().configure().buildSessionFactory();
         }
         catch (Throwable ex) {
           // Make sure you log the exception, as it might be swallowed
          System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
          }
        }
    
       public static SessionFactory getSessionFactory() {
          return sessionFactory;
       }
    
       public static void shutdown() {
        // Close caches and connection pools
         getSessionFactory().close();
       }
    
     }