我有一个AuthorizationServlet,在登录页面中的用户点击按钮时调用。这是servlet的doPost方法的一段代码:
AuthorizationBean bean = new AuthorizationBean();
String login = (String) request.getSession().getAttribute("login");
String password = (String) request.getSession().getAttribute("password");
try {
UserType type = bean.getRoleOfUser(login, password);
switch(type) {
...
}
} catch (DaoException ex) {
throw new ServletException(ex.getMessage());
}
我省略了一些代码,因为它不适用于问题。实际上,DaoException
只能在此行UserType type = bean.getRoleOfUser(login, password);
中抛出
在方法getRoleOfUser
中,我使用tomcat的连接池连接到数据库(关于如何获得连接的描述在第一篇文章here中)
此处还有我的context.xml,它创建了db connection的资源:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Project">
<Resource name="jdbc/Project" auth="Container"
type="javax.sql.DataSource" maxActive="100"
maxIdle="30" maxWait="10000"
username="name"
password="password"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:XE"/>
</Context>
调用AuthorizationServlet时出现错误:
javax.servlet.ServletException: Error instantiating servlet class com.tspp.common.AuthorizationServlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
java.lang.Thread.run(Thread.java:662)
root cause
java.lang.NoClassDefFoundError: com/tspp/dao/exceptions/DaoException
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
java.lang.Class.getConstructor0(Class.java:2699)
java.lang.Class.newInstance0(Class.java:326)
java.lang.Class.newInstance(Class.java:308)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
java.lang.Thread.run(Thread.java:662)
root cause
java.lang.ClassNotFoundException: com.tspp.dao.exceptions.DaoException
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
java.lang.Class.getDeclaredConstructors0(Native Method)
java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
java.lang.Class.getConstructor0(Class.java:2699)
java.lang.Class.newInstance0(Class.java:326)
java.lang.Class.newInstance(Class.java:308)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
java.lang.Thread.run(Thread.java:662)
我试过调试它,但是调试器甚至没有进入servlet的doPost方法。 实际上,当我启动程序时(在servlet激活之前)抛出最后2个异常
我该如何解决这个问题?
答案 0 :(得分:2)
确保您拥有WEB-INF/classes
答案 1 :(得分:1)
JVM(com/tspp/dao/exceptions/DaoException
方法)找不到您的班级System.loadClass()
,因此您拥有ClassNotFoundException
。
确保您的代码已编译并包含在项目的WEB-INF\classes
文件夹中。如果它在jar中,请确保在JAR中找到该类。