尝试通过servlet在hibernate中执行select操作时出现NoClassDefFound错误和文档异常

时间:2015-07-04 11:04:38

标签: hibernate jsp servlets

我正在尝试使用Hibernate通过JSP + Servlet创建一个简单的登录页面。

我的Servlet类的

doPost()

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        System.out.println("Inside doPost() of Login Servlet!");
        System.out.println(request.getParameter("sapid"));
        System.out.println(request.getParameter("pass"));
        System.out.println("Role = "+request.getParameter("role"));

        User user=new User();
        Role role=new Role();

        user.setSapid(request.getParameter("sapid"));
        user.setPass(request.getParameter("pass"));
        role.setRoledesc(request.getParameter("role"));


        LoginServiceInterface service=new LoginServiceImpl();
        boolean status=service.validate(user,role);
        if(status)
        {
            HttpSession session =request.getSession(true);
            session.setAttribute("user",user);
            request.getRequestDispatcher("/Home.jsp").forward(request,response);
        }
        else
        {
            request.getRequestDispatcher("/sfp/loginerror.jsp").forward(request,response);
        }
    }

我的Dao课程:

package com.cba.quiz.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cba.quiz.domain.Role;
import com.cba.quiz.domain.User;
import com.cba.quiz.util.HibernateUtil;
public class LoginDaoImpl implements LoginDaoInterface {

    @Override
    public boolean validate(User user ,Role role) {

        int resstat = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<User> employee = new ArrayList<User>();
        try {
            transaction = session.beginTransaction();
            Query query = session.getNamedQuery("findRoleforaUser");

            query.setString("sapid", user.getSapid());
            query.setString("pass", user.getPass());
            query.setString("roledesc",role.getRoledesc());
            employee = query.list();
            transaction.commit();
            if (employee.size() == 0)
                resstat=0;
            else {
                resstat=1;
                for (User l : employee) {
                    System.out.println(l.getName() + "\n");
                }
            }
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();

        } finally {

            session.close();

        }

        if (resstat == 1)
            return true;
        else
            return false;

    }

}

问题: 当我尝试将Dao类作为独立程序运行时(通过将validate()更改为main()方法并在query.SetString()中传递一些硬编码值,例如{{1}它等,它的工作原理很精细。但是当我试着打电话给Dao班时#39;方法从Servlet类传递一些值,它会抛出一些异常和错误。

控制台O / P

query.setString("sapid", "51527123");

项目结构:

Project Structure

映射XML:

1)Role.hbm.xml

Inside doPost() of Login Servlet!
51527690
asd
Role = user
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
SessionFactory creation failedorg.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Jul 04, 2015 4:25:27 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.cba.quiz.controller.LoginServlet] in context with path [/CBAQuiz] threw exception [Servlet execution threw an exception] with root cause
org.dom4j.DocumentException: C:\Program Files\eclipse-jee-luna-R-win32\eclipse\src\dtd\hibernate-configuration-3.0.dtd (The system cannot find the path specified) Nested exception: C:\Program Files\eclipse-jee-luna-R-win32\eclipse\src\dtd\hibernate-configuration-3.0.dtd (The system cannot find the path specified)
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.cba.quiz.util.HibernateUtil.<clinit>(HibernateUtil.java:10)
    at com.cba.quiz.dao.LoginDaoImpl.validate(LoginDaoImpl.java:21)
    at com.cba.quiz.service.LoginServiceImpl.validate(LoginServiceImpl.java:15)
    at com.cba.quiz.controller.LoginServlet.doPost(LoginServlet.java:61)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:722)
Nested exception: 
java.io.FileNotFoundException: C:\Program Files\eclipse-jee-luna-R-win32\eclipse\src\dtd\hibernate-configuration-3.0.dtd (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
    at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
    at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.cba.quiz.util.HibernateUtil.<clinit>(HibernateUtil.java:10)
    at com.cba.quiz.dao.LoginDaoImpl.validate(LoginDaoImpl.java:21)
    at com.cba.quiz.service.LoginServiceImpl.validate(LoginServiceImpl.java:15)
    at com.cba.quiz.controller.LoginServlet.doPost(LoginServlet.java:61)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:722)

2)User.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping SYSTEM "src\dtd\hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.cba.quiz.domain.Role" table="roles">
        <id name="roleid" type="int" column="ROLEID" />
        <property name="roledesc" type="string" column="ROLEDESC" />
        <set name="users" cascade="all">
            <key column="roleid"/>
            <one-to-many class="com.cba.quiz.domain.User"/>
       </set>
    </class>
    <query name="findRoleforaUser">
        <![CDATA[select u from Role as r left join r.users as u where u.sapid=:sapid and u.pass=:pass and r.roledesc=:roledesc]]>
    </query>    
</hibernate-mapping>

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping SYSTEM "src\dtd\hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.cba.quiz.domain.User" table="users">
        <id name="sapid" type="string" column="SAPID" />
        <property name="name" type="string" column="NAME" />
        <property name="pass" type="string" column="PASS" />
    </class>

</hibernate-mapping>

似乎是什么问题?

1 个答案:

答案 0 :(得分:1)

正如错误清楚地说明的那样,Hibernate无法找到文件C:\Program Files\eclipse-jee-luna-R-win32\eclipse\src\dtd\hibernate-configuration-3.0.dtd。它是您的XML配置所需的(引用为src\dtd\hibernate-configuration-3.0.dtd,请注意相对路径,该路径是针对工作目录解析的。)

当您将程序作为独立应用程序运行时,工作目录是项目的根目录,因此相对路径指向现有文件,正如您所期望的那样。但是,当您将其作为Web应用程序运行时,工作目录是其他内容,因此该路径指向不存在的文件。

要解决此问题,您应该更改配置文件中的DOCTYPE

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

并在映射文件中:

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">