我是Hibernate的新手,servlet。我正在尝试使用html注册表格中的hibernate将数据存储在mysql数据库中。但是我一直将此错误作为根本原因。 这是弹出的两个根本原因。
HTTP status 500 Internal server error.
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.NoClassDefFoundError: javax/persistence/PersistenceException
java.base/java.lang.ClassLoader.defineClass1(Native Method)
java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2339)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:829)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1278)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
kish.hibernate.servletController.registerData.doGet(registerData.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.ClassNotFoundException: javax.persistence.PersistenceException
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1309)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
java.base/java.lang.ClassLoader.defineClass1(Native Method)
java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2339)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:829)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1278)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1137)
kish.hibernate.servletController.registerData.doGet(registerData.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
这是我的hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configurationn>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">Kish</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hbm2ddl.auto">create</property> <!-- create table every time -->
<property name="show_sql">true</property>
<mapping class="kish.hibernate.Pojo.Student"></mapping>
</session-factory>
</hibernate-configurationn>
这是我的html文件
<html>
<head>
<title> RegistrationForm</title>
<link rel = "stylesheet" type = "text/css" href = "style1.css">
<body>
<div class= "registrationBox">
<img src= "avatar1.png" class= "avatar">
<h1> enter your details below form</h1>
<form action = "registerData" method ="get">
<p>Firstname</p>
<input type="text" name="first_name" placeholder="Enter your firstname">
<p>Lastname</p>
<input type = "text" name= "last_name" placeholder="Your lastname ">
<p>Branch</p>
<input type = "text" name="branch" placeholder="your branch">
<p>Id</p>
<input type = "text" name = "Id" placeholder="your id">
<p>password</p>
<input type="password" name="password" placeholder="create your password">
<input type="submit" name="submit" value = "confirm">
</form>
</div>
</body>
</head>
</html>
servlet文件registerData.java
[package kish.hibernate.servletController;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kish.hibernate.DAO.MainController;
@WebServlet("/registerData")
public class registerData extends HttpServlet {
private static final long serialVersionUID = 1L;
public registerData() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
String branch = request.getParameter("branch");
int Id = Integer.parseInt(request.getParameter("Id"));
String password = request.getParameter("password");
MainController mc = new MainController();
mc.addUserDeatails(first_name, last_name, branch, Id, password);
response.sendRedirect("success");
}
}
如有需要,我将发布更多数据。
任何建议表示赞赏。 谢谢。
编辑:
“ javax.persistence.jar”
它显然包含“ javax.persistence.PersistenceException.class”
添加了这个jar并仍然显示相同的错误。
答案 0 :(得分:0)
ClassNotFoundException在JVM尝试在运行时动态加载类时出现
NoClassDefFoundError编译期间存在问题的类,这就是程序成功编译但由于任何原因在运行时不可用的原因
请检查您的课程路径。由于它是Maven,请检查您setting.xml中的lib文件夹路径,并检查jar是否在m2.repository中可用。
答案 1 :(得分:0)
将jar放在 WEB-INF / lib文件夹下。构建您的项目。它将起作用。
答案 2 :(得分:0)