扩展ValveBase类:java.lang.ClassNotFoundException

时间:2014-06-23 05:08:37

标签: java tomcat-valve

我在java中创建了一个Web基础应用程序,并使用了Valve的ValveBase类。代码如下:

public class RenewSessionValve extends ValveBase {

    private String authenticationUrl = "j_security_check";

    @Override
    public void invoke(Request req, Response res) throws IOException,
            ServletException {
        // TODO Auto-generated method stub
        System.out.println("invoke in SessionFixationValve "+req.getRequestURI());
        if (req.getRequestURI().contains(authenticationUrl)) {

        // save old session
        Session oldSession = req.getSessionInternal(true);
        SavedRequest saved = (SavedRequest) oldSession.
        getNote(Constants.FORM_REQUEST_NOTE);
        System.out.println("before invalidating session");
        // invalidate old session
        req.getSession(true).invalidate();
        req.setRequestedSessionId(null);
        req.clearCookies();

        System.out.println("after invalidating session");
        // create a new session and set it to the request
        Session newSession = req.getSessionInternal(true);
        req.setRequestedSessionId(newSession.getId());

        System.out.println("saved obj is : "+saved);
        // copy data from the old session
        // to the new one
        if (saved != null) {
        newSession.setNote(Constants.FORM_REQUEST_NOTE, saved);
        }

        }
        System.out.println("before invoke");
        // after processing the request forward it
        getNext().invoke(req, res);
        }   
}

现在我编译了该文件并创建了该项目的jar文件并将其放入服务器的lib文件夹中。 我更改的server.xml文件的一部分:

<Host appBase="webapps" autoDeploy="false" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false" deployOnStartup="false" deployXML="false">
        <Valve className="com.cmc.RenewSessionValve" />
</Host>

现在我创建一个新项目并运行它。它给了我如下错误:

SEVERE: Begin event threw exception
java.lang.ClassNotFoundException: com.cmc.RenewSessionValve
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:205)
    at org.apache.tomcat.util.digester.Rule.begin(Rule.java:153)
    at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1356)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1642)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:524)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:562)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Jun 23, 2014 10:33:16 AM org.apache.catalina.startup.Catalina load
WARNING: Catalina.start using conf/server.xml: 
java.lang.ClassNotFoundException: com.cmc.RenewSessionValve
    at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2806)
    at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2832)
    at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1359)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1642)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:524)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:562)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:261)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.ClassNotFoundException: com.cmc.RenewSessionValve
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:205)
    at org.apache.tomcat.util.digester.Rule.begin(Rule.java:153)
    at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1356)
    ... 20 more
Jun 23, 2014 10:33:16 AM org.apache.tomcat.util.digester.Digester startElement

2 个答案:

答案 0 :(得分:0)

这可能是因为您已将jar放在不正确的服务器目录中。

答案 1 :(得分:-1)

供将来参考 - 必须将自定义阀门源代码导出到jar文件中,并放在tomcat目录的<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div data-ng-app='carApp' data-ng-controller='carAppCtrlr'> <ul> <li data-ng-repeat='v in vehicles'>{{v.type +" " + v.color}}</li> </ul> </div>文件夹中。 (与项目目录不同)

否则tomcat将无法找到自定义类并将抛出lib