2 Replies Latest reply on Jun 10, 2005 12:07 AM by Ryan Thomas

    ServletException - VelocityServlet is not a servlet

    Ryan Thomas Newbie

      Hi all,

      We have successfully deployed some servlets using JBoss 4.0.1. We are now trying to use the Velocity template engine (which we have used running just tomcat and apache). When accessing the servlet all we get is the exception below, saying that the class is not a servlet.

      javax.servlet.ServletException: Class com.transactive.online.submission.LoanSubmitServlet is not a Servlet
       org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
       org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:150)
       org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:54)
       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
       org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
       org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
       org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
       org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
       org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
       java.lang.Thread.run(Thread.java:536)
      
      root cause
      
      java.lang.ClassCastException
       com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
       javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
       com.transactive.online.submission.LoanSubmitServlet.<init>(LoanSubmitServlet.java:35)
       sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
       sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
       java.lang.reflect.Constructor.newInstance(Constructor.java:274)
       java.lang.Class.newInstance0(Class.java:306)
       java.lang.Class.newInstance(Class.java:259)
       org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
       org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:150)
       org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:54)
       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
       org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
       org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
       org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
       org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
       org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
       java.lang.Thread.run(Thread.java:536)
      


      Here is the code for the servlet:

      package com.transactive.online.submission;
      
      import java.io.FileNotFoundException;
      import java.io.IOException;
      import java.util.Properties;
      
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.rmi.PortableRemoteObject;
      import javax.servlet.ServletConfig;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.apache.velocity.Template;
      import org.apache.velocity.app.Velocity;
      import org.apache.velocity.context.Context;
      import org.apache.velocity.exception.ParseErrorException;
      import org.apache.velocity.exception.ResourceNotFoundException;
      import org.apache.velocity.servlet.VelocityServlet;
      
      public class LoanSubmitServlet extends /*HttpServlet*/ VelocityServlet
      {
       /*public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
       PrintWriter out = response.getWriter();
       out.print("<HTML><H1>TEST</H1></HTML>");
       out.flush();
       return;
       }*/
      
       LoanSubmit loanSubmit = null;
       public LoanSubmitServlet() throws NamingException
       {
       javax.naming.Context ctx = new InitialContext();
       LoanSubmitHome home = (LoanSubmitHome) PortableRemoteObject.narrow(ctx.lookup("LoanSubmit"), LoanSubmit.class);
       try
       {
       loanSubmit = home.create();
       }
       catch(Exception e) {}
       }
      
       protected Properties loadConfiguration(ServletConfig config) throws IOException, FileNotFoundException
       {
       Properties p = new Properties();
       String path = config.getServletContext().getRealPath("/vm");
       if(path == null)
       {
       System.out.println(" SampleServlet.loadConfiguration() : unable to get "
       + "the current webapp root. Using '/'. Please fix.");
       path = "/";
       }
       p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
       p.setProperty("runtime.log", path + "velocity.log");
       return p;
       }
      
       public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx)
       {
       Template outty = null;
       try
       {
       outty = getTemplate("login.vm");
       }
       catch(ParseErrorException pee)
       {
       System.out.println("parse error for template " + pee);
       }
       catch(ResourceNotFoundException rnfe)
       {
       System.out.println("template not found " + rnfe);
       }
       catch(Exception e)
       {
       System.out.println("Error " + e);
       }
       return outty;
       }
      }
      


      Changing it to a HttpServlet and implementing the doGet method works as expected, its just when we extend VelocityServlet that it throws the exception.

      Any help on this matter would be greatly appreciated.

      Cheers,

      Ryan Thomas
      TransActive Systems

        • 1. Re: ServletException - VelocityServlet is not a servlet
          Ryan Thomas Newbie

          I found out what the issue was.

          public LoanSubmitServlet() throws NamingException
          {
           javax.naming.Context ctx = new InitialContext();
           LoanSubmitHome home = (LoanSubmitHome) PortableRemoteObject.narrow(ctx.lookup("LoanSubmit"), LoanSubmit.class);
           try
           {
           loanSubmit = home.create();
           }
           catch(Exception e) {}
          }
          


          Commenting out the lookup causes it to work, I think this is a config issue , will continue looking into it.

          Cheers,

          Ryan Thomas
          TransActive Systems

          • 2. Re: ServletException - VelocityServlet is not a servlet
            Ryan Thomas Newbie

            After much googleing, a thread I saw at theserverside.com helped me out, I had to change the PortableRemoteObject call and replace it with this line:

            LoanSubmitHome home = (LoanSubmitHome) ctx.lookup("LoanSubmit"); //PortableRemoteObject.narrow(ctx.lookup("LoanSubmit"), LoanSubmit.class);
            


            This now works, however I don't really understand the theory behind why this has worked for me up until I tried to use velocity.

            If someone could explain this, or point me in a direction of the info I'd appreciate it.

            Heres the thread on theserverside.com:
            http://www.theserverside.com/discussions/thread.tss?thread_id=29200

            Cheers,

            Ryan Thomas
            TransActive Systems