7 Replies Latest reply: Mar 31, 2011 12:37 AM by EJP RSS

    cannot be cast to javax.servlet.Filter

    YE
      I get errors of cannot be cast to javax.servlet.Filter during deployment. Which part of the following code is wrong?


      --------------------------------------------------------------------------------

      package com.su.rest.filters;

      import java.io.IOException;

      import javax.servlet.Filter;
      import javax.servlet.FilterChain;
      import javax.servlet.FilterConfig;
      import javax.servlet.ServletException;
      import javax.servlet.ServletRequest;
      import javax.servlet.ServletResponse;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import org.apache.log4j.Logger;

      import com.su.rest.util.OutputCaptureResponseWrapper;

      public class JSONFormatterFilter extends HttpServlet implements Filter {

      private static Logger log = Logger.getLogger(JSONFormatterFilter.class);

      public void destroy() {

      }

      public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse,
      FilterChain filterchain) throws IOException, ServletException {
      HttpServletResponse httpservletresponse;
      String s;
      label0:
      {
      HttpServletRequest httpservletrequest = (HttpServletRequest)servletrequest;
      httpservletrequest.setCharacterEncoding("utf-8");
      httpservletresponse = (HttpServletResponse)servletresponse;
      httpservletresponse.setCharacterEncoding("utf-8");
      httpservletresponse.setContentType("text/html");
      if(!"json".equalsIgnoreCase(servletrequest.getParameter("f"))){


      filterchain.doFilter(httpservletrequest, httpservletresponse);
      return;
      }
      OutputCaptureResponseWrapper outputcaptureresponsewrapper = new OutputCaptureResponseWrapper(httpservletresponse);
      filterchain.doFilter(servletrequest, outputcaptureresponsewrapper);
      // s = new String(outputcaptureresponsewrapper.getOutputAsByteArray());
      s= outputcaptureresponsewrapper.getOutputAsString("UTF-8");
      log.debug(s);



      String callBack = httpservletrequest.getParameter("callback");
      String s2=s.trim();
      if(callBack!=null){
      s2=(new StringBuilder()).append(callBack).append("(").append(s2).append(")").toString();
      s=s2;
      }
      }
      httpservletresponse.getWriter().write(s);
      }

      public void init(FilterConfig arg0) throws ServletException {

      }
      }

      ----------------web.xml--------------------------------------

      <filter>
      <filter-name>JSONFormatter</filter-name>
      <filter-class>com.sunz.rest.filters.JSONFormatterFilter</filter-class>
      </filter>


      <filter-mapping>
      <filter-name>JSONFormatter</filter-name>
      <url-pattern>/services/rest/*</url-pattern>
      </filter-mapping>
        • 1. Re: cannot be cast to javax.servlet.Filter
          EJP
          Clearly the code you are deploying isn't the code you posted here, but why on earth does your filter extend HttpServlet? Either it is a servlet or a filter, you can't have it both ways.
          • 2. Re: cannot be cast to javax.servlet.Filter
            YE
            I change the code as follow. But I still get the same problem when I deploy the EAR to weblogic 10.3.4

            -------------------------
            package com.sunz.rest.filters;

            import java.io.IOException;

            import javax.servlet.Filter;
            import javax.servlet.FilterChain;
            import javax.servlet.FilterConfig;
            import javax.servlet.ServletException;
            import javax.servlet.ServletRequest;
            import javax.servlet.ServletResponse;

            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            import org.apache.log4j.Logger;

            import com.sunz.rest.util.OutputCaptureResponseWrapper;

            public class JSONFormatterFilter implements Filter {

                 private static Logger log = Logger.getLogger(JSONFormatterFilter.class);
                 
                 public void destroy() {

                 }

                 public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse,
                           FilterChain filterchain) throws IOException, ServletException {
                      HttpServletResponse httpservletresponse;
            String s;
            label0:
                      {
                           HttpServletRequest httpservletrequest = (HttpServletRequest)servletrequest;
                           httpservletrequest.setCharacterEncoding("utf-8");
                 httpservletresponse = (HttpServletResponse)servletresponse;
                 httpservletresponse.setCharacterEncoding("utf-8");
                 httpservletresponse.setContentType("text/html");
                           if(!"json".equalsIgnoreCase(servletrequest.getParameter("f"))){
                                
                                filterchain.doFilter(httpservletrequest, httpservletresponse);
            return;
                           }
                           OutputCaptureResponseWrapper outputcaptureresponsewrapper = new OutputCaptureResponseWrapper(httpservletresponse);
                           filterchain.doFilter(servletrequest, outputcaptureresponsewrapper);
            //               s = new String(outputcaptureresponsewrapper.getOutputAsByteArray());
                           s= outputcaptureresponsewrapper.getOutputAsString("UTF-8");
                           log.debug(s);

                           
                           String callBack = httpservletrequest.getParameter("callback");
                           String s2=s.trim();
                           if(callBack!=null){
                                s2=(new StringBuilder()).append(callBack).append("(").append(s2).append(")").toString();
                                s=s2;
                           }
                      }
            httpservletresponse.getWriter().write(s);
                 }

                 public void init(FilterConfig arg0) throws ServletException {

                 }
            }

            --------------------web.xml----------------
            <filter>
            <filter-name>JSONFormatter</filter-name>
            <filter-class>com.sunz.rest.filters.JSONFormatterFilter</filter-class>
            </filter>

            <filter-mapping>
            <filter-name>JSONFormatter</filter-name>
            <url-pattern>/services/rest/*</url-pattern>
            </filter-mapping>

            ---------------weblogic.xml------------------------------------------------

            <?xml version = '1.0' encoding = 'UTF-8'?>
            <weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd" xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
            <context-root>szmap</context-root>
            <container-descriptor>
            <prefer-web-inf-classes>true</prefer-web-inf-classes>
            </container-descriptor>
            </weblogic-web-app>

            -------------------weblogic-application.xml--------------------------

            <?xml version = '1.0' encoding = 'UTF-8'?>
            <weblogic-application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-application http://www.bea.com/ns/weblogic/weblogic-application/1.0/weblogic-application.xsd"
            xmlns="http://www.bea.com/ns/weblogic/weblogic-application">

            <application-param>
            <param-name>webapp.encoding.default</param-name>
            <param-value>UTF-8</param-value>
            </application-param>
            <prefer-application-packages>

            .........................................
            <!--E:\testSVN\szmap\WebContent\WEB-INF\lib\geronimo-servlet_2.5_spec-1.2.jar-->
            <package-name>javax.servlet</package-name>
            <package-name>javax.servlet.*</package-name>
            <package-name>javax.servlet.http</package-name>
            <package-name>javax.servlet.http.*</package-name>
            <package-name>javax.servlet.resources</package-name>
            <package-name>javax.servlet.resources.*</package-name>

            ............................................

            </prefer-application-packages>

            </weblogic-application>
            • 3. Re: cannot be cast to javax.servlet.Filter
              EJP
              Do you have multiple versions of servlet-api.jar lying around? There will be one in the container, you don't need to deploy your own with the web-app.
              • 4. Re: cannot be cast to javax.servlet.Filter
                YE
                I delete geronimo-servlet_2.5_spec-1.2.jar and rebuild it. However, I get new errors.

                ---------------------------------

                weblogic.application.ModuleException:
                     at weblogic.servlet.internal.WebAppModule.prepare(WebAppModule.java:393)
                     at weblogic.application.internal.flow.ScopedModuleDriver.prepare(ScopedModuleDriver.java:176)
                               at weblogic.work.ExecuteThread.run(ExecuteThread.java:176)

                Caused By: java.lang.ClassNotFoundException: javax.servlet.ServletRequestListener
                     at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
                     at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
                     at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(C
                • 5. Re: cannot be cast to javax.servlet.Filter
                  EJP
                  I delete geronimo-servlet_2.5_spec-1.2.jar
                  From what?
                  and rebuild it.
                  Rebuilt what?
                  • 6. Re: cannot be cast to javax.servlet.Filter
                    YE
                    This project is created by an Eclipse programmer. It works fine on Tomcat. I am not a programmer but have some knowledge of Jdeveloper.
                    This programmer deny to change the source code until I find the solution of deploying Eclipse project to weblogic 10.3.4

                    I delete geronimo-servlet_2.5_spec-1.2.jar from WEB-INFO/lib. Then, I rebuild this project in Eclipse. Then, I create a war file from this project and create a project in Jdeveloper based on this war. I delete

                    <package-name>javax.servlet</package-name>
                    <package-name>javax.servlet.*</package-name>
                    <package-name>javax.servlet.http</package-name>
                    <package-name>javax.servlet.http.*</package-name>
                    <package-name>javax.servlet.resources</package-name>
                    <package-name>javax.servlet.resources.*</package-name>

                    from weblogic-application.xml. After that , I create an EAR file. However, after deployment, I get errors. CAT of weblogic 10.3.4 cannot run. I cannot find my application in its list after login.

                    weblogic.application.ModuleException:
                         at weblogic.servlet.internal.WebAppModule.prepare(WebAppModule.java:393)
                         at weblogic.application.internal.flow.ScopedModuleDriver.prepare(ScopedModuleDriver.java:176)
                         at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:199)
                         at weblogic.application.internal.flow.DeploymentCallbackFlow$1.next(DeploymentCallbackFlow.java:517)
                         at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
                         at weblogic.application.internal.flow.DeploymentCallbackFlow.prepare(DeploymentCallbackFlow.java:159)
                         at weblogic.application.internal.flow.DeploymentCallbackFlow.prepare(DeploymentCallbackFlow.java:45)
                         at weblogic.application.internal.BaseDeployment$1.next(BaseDeployment.java:613)
                         at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
                         at weblogic.application.internal.BaseDeployment.prepare(BaseDeployment.java:184)
                         at weblogic.application.internal.EarDeployment.prepare(EarDeployment.java:58)
                         at weblogic.application.internal.DeploymentStateChecker.prepare(DeploymentStateChecker.java:154)
                         at weblogic.deploy.internal.targetserver.AppContainerInvoker.prepare(AppContainerInvoker.java:60)
                         at weblogic.deploy.internal.targetserver.operations.ActivateOperation.createAndPrepareContainer(ActivateOperation.java:207)
                         at weblogic.deploy.internal.targetserver.operations.StartOperation.createAndPrepareContainer(StartOperation.java:85)
                         at weblogic.deploy.internal.targetserver.operations.StartOperation.doPrepare(StartOperation.java:98)
                         at weblogic.deploy.internal.targetserver.operations.AbstractOperation.prepare(AbstractOperation.java:217)
                         at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentPrepare(DeploymentManager.java:747)
                         at weblogic.deploy.internal.targetserver.DeploymentManager.prepareDeploymentList(DeploymentManager.java:1216)
                         at weblogic.deploy.internal.targetserver.DeploymentManager.handlePrepare(DeploymentManager.java:250)
                         at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.prepare(DeploymentServiceDispatcher.java:159)
                         at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doPrepareCallback(DeploymentReceiverCallbackDeliverer.java:171)
                         at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$000(DeploymentReceiverCallbackDeliverer.java:13)
                         at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$1.run(DeploymentReceiverCallbackDeliverer.java:46)
                         at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:528)
                         at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
                         at weblogic.work.ExecuteThread.run(ExecuteThread.java:176)
                    Caused By: java.lang.ClassNotFoundException: javax.servlet.ServletRequestListener
                         at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
                         at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
                         at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(C
                    • 7. Re: cannot be cast to javax.servlet.Filter
                      EJP
                      The JAR file shouldn't be deployed with the WAR file as far as I know but what all that other stuff is you will have to ask the developer. I wouldn't be deleting those <package-name> things myself. And why you are using a 2nd IDE at all is entirely beyond me. None of this is about Java servlet programming, it is about how to use the IDE(s).