7

I get an exception whenever I try getting context parameter from we.XML into a ServletContextListener class, I am really having hard times understanding why It is not working, here's the exception in Apache Tomcat 7.0.11 log :

 Oct 21, 2011 1:24:23 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class alaa.ServletContextListener
java.lang.ClassNotFoundException: alaa.ServletContextListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at   
   org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at      

at    org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4618)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

Here's a part of my web.xml :

 <context-param>
    <param-name>catName</param-name>
    <param-value>meshmesh</param-value>
</context-param>
<context-param>
    <param-name>catBreed</param-name>
    <param-value>egyptian</param-value>
</context-param>  
<listener>
   <listener-class>alaa.CatLisenter</listener-class>
</listener>
<session-config>
      <session-timeout>
        30
      </session-timeout>
</session-config>

Here's my ServletContextListener.java:

package alaa;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CatLisenter implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServletContext sc = sce.getServletContext();
    String name = sc.getInitParameter("catName");

    String breed = sc.getInitParameter("catBreed");

    Cat maCat = new Cat();
    maCat.setName(name);
    maCat.setBreed(breed);

    sc.setAttribute("cat", maCat);
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    throw new UnsupportedOperationException("Not supported yet.");
}   
}



Here's Cat.java : 





package alaa;

public class Cat {
private String name;
private String breed;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getBreed() {
    return breed;
}


public void setBreed(String breed) {
    this.breed = breed;
}
 }

many thanks 
Rehme
  • 323
  • 3
  • 6
  • 20

7 Answers7

10

Try to clear the tomcat work directory and clean. After that, publish your project and run again.

  • I did as you told me to, deleted context file from \Apache Tomcat 7.0.11\conf and ran my webApp again, and It worked just perfect, thanks a million . – Rehme Oct 21 '11 at 17:07
5

My guess is that you have packaged the servlet-api jar in your webapp (in the WEB-INF/lib) folder and this is causing conflicts since the servlet-api will already be present in the container. Make sure you don't include any servlet-api or jsp-api (or Java EE api) jars in your webapp when you deploy it.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
pap
  • 27,064
  • 6
  • 41
  • 46
  • Did not create any servlet or jsp page yet, Is this what you meant ? I'm figuring on creating a servlet so that I could get ServletContext attribute then get the cat instances back . – Rehme Oct 21 '11 at 13:00
  • @RealSilhouette no, that's not what I meant. The final build that you are deploying to tomcat, does it include any jar-files in the WEB-INF/lib directory called something like servlet-api.jar or jsp-api.jar? If so, you are not packaging your war correctly as those jars already exist in the tomcat container, causing conflicts. How are you building? Maven? Through the IDE? – pap Oct 21 '11 at 13:04
  • Where is there a conflict? It says the listener class isn't found (assuming my phone is displaying everything correctly :( – Dave Newton Oct 21 '11 at 13:20
  • @pap the WEB-INF/lib directory does not has any jar-files – Rehme Oct 21 '11 at 14:11
1

I had the same problem running JUnit in a Tomcat 7 environment and I solved it adding a dependency in maven (pom.xml) like this:

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-servlet-api</artifactId>
        <version>7.0.54</version>
        <scope>provided</scope>
    </dependency>
Celia
  • 11
  • 1
1

If you are working in Eclipse, Then just clean your project.

Follow this simple step, Go to Project > clean...> clean all projects > Ok

Rama Krishna
  • 275
  • 3
  • 12
0

I had the same issue when I tried with eclipse LUNA version and tomcat 7. The same code without any extra changes worked in eclipse JUNO with tomcat 7.

gauti
  • 527
  • 1
  • 6
  • 16
0

Verify the space in disk. When eclipse copy the folder libs if not space in disk this error may occur

Emir Marques
  • 2,603
  • 2
  • 16
  • 22
0

I had a similar problem. It Maybe not be related to what you had but it might save someone some time. I had my listener-class wrong in the web descriptor file:)

George Kibira
  • 59
  • 1
  • 8