2014-06-23 59 views
0

我已經在java中創建了一個Web基礎應用程序,並使用了Java的ValveBase類。代碼如下:擴展ValveBase類:java.lang.ClassNotFoundException

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 
+0

檢查你的包名和文件夾結構應該是一樣的包名 –

+0

是的,這是與包名相同。 –

回答

0

這可能是由於您已將您的jar放在不正確的服務器目錄。

-1

供將來參考 - 自定義閥源代碼必須導出到jar文件中,並放置在您的tomcat目錄的lib文件夾中。 (不一樣的項目目錄)

否則tomcat會無法找到自定義類,並會拋出ClassNotFoundException