我刚开始学习Hibernate和Servlets。 我有一个表单(姓名,电子邮件),我填写这些字段,请求转到服务器。 然后我从服务器获取数据并打印它们。 这项工作正确!
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("userName");
String email = request.getParameter("email");
String ip = request.getRemoteAddr();
response.getWriter().println("<html>");
response.getWriter().println("<head>");
response.getWriter().println("<title>This is a response</title>");
response.getWriter().println("<head>");
response.getWriter().println("<body>");
response.getWriter().println("<br>Your name is: " + name);
response.getWriter().println("<br>Your email is: " + email);
response.getWriter().println("<br>Your IP address is: " + ip);
response.getWriter().println("</body>");
response.getWriter().println("</html>");
}
此外,我有一个数据库(db和.xml文件已正确配置,ping成功,映射已写入)和Hibernate代码:
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Data data = new Data();
data.setUserName(name);
data.setUserEmail(email);
session.save(data);
session.getTransaction().commit();
session.close();
}
我还有一个Data类:
@Entity
public class Data {
@Id
@GeneratedValue
private int Id;
private String userName;
private String userEmail;
//getteres, setters...
}
但是,如果我将此代码放入Servlet,我会收到一个HTTP错误500.我可以确定错误消息:
javax.servlet.ServletException: Error instantiating servlet class org.server.db.test.ServletExample org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) java.lang.Thread.run(Thread.java:722)
如何将我的信息从服务器发送到数据库?
我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Example02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ServletExample</display-name>
<servlet-name>ServletExample</servlet-name>
<servlet-class>org.server.db.test.ServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletExample</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
</web-app>
hibernate cfg文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/db</property>
<property name="connection.username">postgres</property>
<property name="connection.password">***</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="org.server.db.test.Data" />
<mapping class="org.server.db.test.ServletExample" />
</session-factory>
</hibernate-configuration>
答案 0 :(得分:0)
javax.servlet.ServletException:实例化servlet类时出错org.server.db.test.ServletExample
到目前为止,在服务器的掩护下,以下构造失败并出现异常:
ServletExample servletExample = new ServletExample();
在堆栈跟踪中进一步提到了确切的原因。这是最底层的。阅读服务器日志以找到它。此问题至少 nothing 与“将数据从UI保存到数据库”。