HTTP状态500 - Servlet执行引发异常java.lang.ExceptionInInitializerError

时间:2017-09-04 19:14:15

标签: java jsp servlets tomcat8

在Java WEB中新建并尝试创建简单的应用程序。但是当我点击index.jsp页面上的按钮时,我遇到了一些问题。 Error screen

现在我只使用UsersEntity并尝试将用户保存在数据库中。 这是我的代码。

UsersDAOImpl.java

package com.hiber.test.dao;

import com.hiber.test.model.UsersEntity;
import com.hiber.test.utils.HibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.List;

public class UsersDAOImpl implements UsersDAO {
@Override
public void addUser(UsersEntity usersEntity) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    session.beginTransaction();
    session.save(usersEntity);
    session.getTransaction().commit();
    session.close();
}

@Override
public void updateUser(UsersEntity usersEntity) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    session.beginTransaction();
    session.update(usersEntity);
    session.getTransaction().commit();
    session.close();
}

@Override
public void deleteUser(int id) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    session.beginTransaction();
    Query query = session.createQuery("from UsersEntity where id=:userId");
    query.setInteger("userId", id);
    UsersEntity usersEntity = (UsersEntity) query.uniqueResult();
    session.delete(usersEntity);
    session.getTransaction().commit();
    session.close();
}

@Override
public UsersEntity getUserById(int id) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    session.beginTransaction();
    Query query = session.createQuery("from UsersEntity where id = :id");
    query.setInteger("id", id);
    UsersEntity usersEntity = (UsersEntity) query.uniqueResult();
    session.getTransaction().commit();
    session.close();
    return usersEntity;
}

@Override
public List<UsersEntity> getAllUsers() {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    session.beginTransaction();
    Query query = session.createQuery("from UsersEntity ");
    List<UsersEntity> list = query.list();
    session.getTransaction().commit();
    session.close();
    return list;
}
}

SaveServlet.java

package com.hiber.test.servlets;

import com.hiber.test.dao.UsersDAOImpl;
import com.hiber.test.model.UsersEntity;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SaveServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse 
response) throws javax.servlet.ServletException, IOException {
    UsersDAOImpl usersDAOImple = new UsersDAOImpl();

    if (request.getParameter("update")!=null){
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setName(request.getParameter("name"));
        usersDAOImple.updateUser(usersEntity);
        request.setAttribute("list", usersDAOImple.getAllUsers());
        RequestDispatcher requestDispatcher = 
request.getRequestDispatcher("list.jsp");
        requestDispatcher.forward(request, response);
    }
}

protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws javax.servlet.ServletException, IOException {
    UsersDAOImpl usersDAOImple = new UsersDAOImpl();

    if (request.getParameter("add")!=null){
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setName(request.getParameter("name"));
        usersDAOImple.addUser(usersEntity);
        request.setAttribute("list", usersDAOImple.getAllUsers());
        RequestDispatcher requestDispatcher = 
request.getRequestDispatcher("list.jsp");
        requestDispatcher.forward(request, response);
    }
    if (request.getParameter("showAll")!=null){
        request.setAttribute("list", usersDAOImple.getAllUsers());
        RequestDispatcher requestDispatcher = 
request.getRequestDispatcher("list.jsp");
        requestDispatcher.forward(request, response);
    }

    String action = request.getParameter("action");
    if ("update".equalsIgnoreCase(action)){
        request.setAttribute("user", 
usersDAOImple.getUserById(Integer.parseInt(request.getParameter("userId"))));
        RequestDispatcher requestDispatcher = 
request.getRequestDispatcher("update.jsp");
        requestDispatcher.forward(request, response);
    }
    if ("delete".equalsIgnoreCase(action)){

usersDAOImple.deleteUser(Integer.parseInt(request.getParameter("userId")));
        request.setAttribute("list", usersDAOImple.getAllUsers());
        RequestDispatcher requestDispatcher = 
 request.getRequestDispatcher("list.jsp");
        requestDispatcher.forward(request, response);
    }
}
}

HibernateSessionFactory.java

package com.hiber.test.utils;

import com.hiber.test.model.UsersEntity;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {
private static SessionFactory sessionFactory = buildSessionFactory();

protected static SessionFactory buildSessionFactory(){
    Configuration configuration = new 
Configuration().addAnnotatedClass(UsersEntity.class).configure();
    configuration.configure("hibernate.cfg.xml");
    StandardServiceRegistryBuilder ssrb = new 
 StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
    sessionFactory = configuration.buildSessionFactory(ssrb.build());

    return sessionFactory;
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
}
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

<servlet>
    <servlet-name>SaveServlet</servlet-name>
    <servlet-class>com.hiber.test.servlets.SaveServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>SaveServlet</servlet-name>
    <url-pattern>/save</url-pattern>
</servlet-mapping>
</web-app>

的index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form method="get" action="/save">
 <%--<input type="text" name="id"> --%>
<input type="text" name="name">
<input type="submit" name="add" value="add">
<input type="submit" name="showAll" value="showAll">
</form>
</body>
</html>

谢谢!

0 个答案:

没有答案