10 Replies Latest reply on Apr 18, 2011 2:28 PM by peterj

    cannot be cast to javax.servlet.Servlet

    pkondaka1

      Hello Folks,

       

      This might be a duplicate post, but honestly i couldn't find any solutions to this. please help me.

       

      I am trying to expose my POJO as web service using maven. I have _logic.jar, _war.war and _logic jar is available in WEB-INF/lib/_logic.jar

       

      Have a POJO based on javax.jws.* and have simple method

       

      @WebMethod

      public String sayHello(String name)

      {

        return "Hi " + name + " from the New York.";

      }

       

      <servlet>

        <display-name>Hello Web Service</display-name>

        <servlet-name>HelloService</servlet-name>

        <servlet-class>xxx.xxxx.xxxx.xxxx.Hello</servlet-class>

        <load-on-startup>1</load-on-startup>

      </servlet>

      <servlet-mapping>

        <servlet-name>HelloService</servlet-name>

        <url-pattern>/HelloService</url-pattern>

      </servlet-mapping>

      .

      I am getting following error while deploying

      java.lang.ClassCastException: mil.oni.gdb.webservice.Hello cannot be cast to javax.servlet.Servlet

       

      Do i need to add/edit any other files like jboss-web.xml or any other configuration file missing? I was googling for long time, but couldn't find any solution.

       

      PLEASE HELP ME

        • 1. cannot be cast to javax.servlet.Servlet
          pkondaka1

          Sorry. I am using Jboss-6.0.0 with jdk-1.6

          • 2. cannot be cast to javax.servlet.Servlet
            peterj

            How is the compiler plugin defined in your pom.xml? By default, Maven compiles everything for 1.4 (or ws it 1.3?), unless you tell it otherwise, regardless of which JDK version you are using.

            • 3. cannot be cast to javax.servlet.Servlet
              pkondaka1

              <plugin>

                  <groupId>org.apache.maven.plugins</groupId>

                              <artifactId>maven-compiler-plugin</artifactId>

                              <version>2.0.2</version>      

                  <configuration>

                   <source>1.6</source>

                   <target>1.6</target>

                  </configuration>

                 </plugin>

              • 4. cannot be cast to javax.servlet.Servlet
                peterj

                How are you accessing the web service?

                • 5. cannot be cast to javax.servlet.Servlet
                  pkondaka1

                  It is failing while deploying. ClassCastException. Reason being i am casting my POJO object to Servlet in web.xml

                   

                  Am i missing anything?

                  • 6. cannot be cast to javax.servlet.Servlet
                    pkondaka1

                    Now i am getting following error while accessing the url. deployment was fine.

                    I am accessing via https https://localhosthttps://localhost:9:9443/myapp

                     

                    Allocate exception for servlet TestService: java.lang.ClassCastException: xxx.xxxx.xxxx.MyBean cannot be cast to javax.servlet.Servlet

                    • 7. cannot be cast to javax.servlet.Servlet
                      peterj

                      Where did MyBean come from? The original post stated the clas was named Hello.

                       

                      I don't see anything wrong in what you posted, so there must be something you are leaving out. Please zip up your project directory and post it.

                      • 8. cannot be cast to javax.servlet.Servlet
                        pkondaka1

                        Sorry for the typo. here are the files for review

                         

                        Hello.java - these class files are jarred up in my _logic.jar

                         

                        package com.cg.webservice;

                        import javax.jws.WebMethod;
                        import javax.jws.WebService;
                        import javax.jws.soap.SOAPBinding;

                        @WebService(name="Hello")
                        @SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
                        public class Hello
                        {
                        @WebMethod
                        public String sayHello(String name)
                        {
                          return "Hello " + name + " from the web service.";
                        }
                        }

                         

                         

                        web.xml - under my _war.war file and WAR file includes _logic.jar in WEB-INF\lib

                         

                        <?xml version="1.0" encoding="UTF-8"?>
                        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                            xmlns="http://java.sun.com/xml/ns/javaee"
                            xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                            id="WebApp_ID" version="2.5">
                           
                           
                        <servlet>
                            <servlet-name>HelloService</servlet-name>
                            <servlet-class>com.cg.webservice.Hello</servlet-class>
                          </servlet>

                          <servlet-mapping>
                            <servlet-name>HelloService</servlet-name>
                            <url-pattern>/*</url-pattern>
                          </servlet-mapping>
                         

                           <welcome-file-list>
                               <welcome-file>index.html</welcome-file>
                               <welcome-file>index.jsp</welcome-file>
                           </welcome-file-list>
                          
                          
                        </web-app>

                        • 9. cannot be cast to javax.servlet.Servlet
                          pkondaka1

                          This is actual error message. Am i missing any jar files in jboss classpath? any dependencies missing? Please help me

                           

                           

                          13:58:32,946 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/2gdb]] Marking servlet HelloService as unavailable

                          13:58:32,946 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/2gdb].[TestService]] Allocate exception for servlet HelloService: java.lang.ClassCastException: com.cg.webservice.Hello cannot be cast to javax.servlet.Servlet

                          at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1162) [:6.0.0.Final]

                          • 10. cannot be cast to javax.servlet.Servlet
                            peterj

                            There are only two things you are doing differently from me:

                             

                            a) I placed my classes into WEB-INF/classes (I did not create a JAR)

                             

                            b) My url-pattern in web.xml contains a context name, such as "/hello", not '/*'

                             

                            Of the 2, I suspect that "b" is the problem.