无法在servlet init方法中构建会话工厂

时间:2013-09-10 10:27:50

标签: java hibernate servlets

我正在尝试将hibernate与servlet一起使用。因为SessionFactory花了很多时间来初始化我决定在init方法中构建会话工厂。下面是我的servlet代码。

public void init()throws ServletException
{
    factory = new Configuration().configure().buildSessionFactory();

}

但我得到的错误如下。

Sep 10, 2013 4:27:12 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet cgi as unavailable
Sep 10, 2013 4:27:12 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /school threw load() exception
java.lang.SecurityException: Restricted class org.apache.catalina.servlets.CGIServlet
at  org.apache.catalina.core.DefaultInstanceManager.checkAccess(DefaultInstanceManager.java:432)
at org.apache.catalina.core.DefaultInstanceManager.checkAccess(DefaultInstanceManager.java:423)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:399)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

Sep 10, 2013 4:27:52 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [StaffAttendanceReport] in context with path    [/school] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at com.enext.security.base.BaseServlet.getSessionFactory(BaseServlet.java:45)
at com.enext.edu.school.attendance.servlet.StaffAttendanceReport.service(StaffAttendanceReport.java:56)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.smartware.antisql.AntiSQLFilter.doFilter(AntiSQLFilter.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at Filters.BlockSplChrFliter.doFilter(BlockSplChrFliter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

这个例外是什么意思。

SEVERE: Servlet /school threw load() exception
java.lang.SecurityException: Restricted class org.apache.catalina.servlets.CGIServlet

我改变了tomcat并尝试了同样的我得到了以下异常

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.

下面是我的hibernate.cfg.xml文件

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@ip:port:sid</property>
<property name="hibernate.connection.username">uname</property>
<property name="hibernate.connection.password">passwd</property>       
<mapping resource="com/enext/edu/school/attendance/entity/EmployeeMaster.hbm.xml"/>
<mapping resource="com/enext/edu/school/attendance/entity/StudentHolidayMasterBean.hbm.xml"/>    
<mapping resource="com/enext/edu/school/attendance/entity/EmployeeSignin.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

当你得到Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException

时,似乎有些jar遗失..dom4j-1.5.2.jar

答案 1 :(得分:0)

似乎你遗失了dom4j个罐子,请确保你有这些依赖

<dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version><version></version>
    </dependency>