# Error ServletContextListener auf Tomcat 6



## raptor (19. Feb 2008)

Hallo, bin blutiger Anfänger im Bereich Java EE und Versuche mich gerade mit Servlets und deren EventListener.

Das Testservlet läuft problemlos:

```
package test;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		ServletContext servletContext = session.getServletContext();
		
		servletContext.log("start MyServlet.doGet():" + System.currentTimeMillis());
		
		PrintWriter out = response.getWriter();
		
		response.setContentType("text/html");
		out.println("<html>");
		out.println("<head>");
		out.println("<title>MyServlet</title>");
		out.println("</head>");
		out.println("<body>");
		
		
		out.println("<h1>MyServlet</h1>");
		out.println(request.getContextPath());

		servletContext.setAttribute("myValue", new String("Bla"));
		
		out.println("<div>myValue: "+ (Person)servletContext.getAttribute("myValue") +"</div>");
		out.println("<div>getServletContextName(): "+ servletContext.getServletContextName() +"</div>");
		
		out.println("Context-Info: " + servletContext.getServerInfo());
		out.println("</body>");
		out.println("</html>");
		
		servletContext.log("end MyServlet.doGet():" + System.currentTimeMillis());
	}
}
```

Leider bekomme ich folgenden Listener nicht hin:

```
package test;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

class MyServletListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent event) {
		ServletContext cntxt = event.getServletContext();
		cntxt.setAttribute("test", "wert");
	}
	
	public void contextDestroyed(ServletContextEvent event) {
	}
	
}
```

Die entsprechende web.xml sieht folgender maßen aus:

```
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
	<display-name>MyListener</display-name>
	<listener>
		<listener-class>test.MyServletListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>MyServlet</servlet-name>
		<servlet-class>test.MyServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>MyServlet</servlet-name>
		<url-pattern>servlet1</url-pattern>
	</servlet-mapping>
</web-app>
```

Als Fehlermeldung bekommt ich das hier:

```
19.02.2008 16:05:01 org.apache.catalina.core.StandardContext listenerStart
SCHWERWIEGEND: Error configuring application listener of class dennis.MyServletListener
java.lang.IllegalAccessException: Class org.apache.catalina.core.StandardContext can not access a member of class dennis.MyServletListener with modifiers ""
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
	at java.lang.Class.newInstance0(Class.java:349)
	at java.lang.Class.newInstance(Class.java:308)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3774)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337)
	at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1237)
	at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:591)
	at org.apache.catalina.manager.HTMLManagerServlet.doGet(HTMLManagerServlet.java:128)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)
19.02.2008 16:05:01 org.apache.catalina.core.StandardContext listenerStart
SCHWERWIEGEND: Skipped installing application listeners due to previous error(s)
```

Im Netz habe ich Info gefunden, dass man bei einer Tomcat Version 5.2.16 nicht die exe, sonder die zip-Version nehmen soll. 
Ich benutze allerdings Tomcat 6.0.14
Außerdem ist Java 1.6 im Einsatz und das ganze wird auf Windows XP probiert.


----------



## maki (19. Feb 2008)

Die Exception ist ziemlich eindeutig.


```
class MyServletListener...
```
sollte heissen

```
public class MyServletListener...
```


----------



## raptor (19. Feb 2008)

Huch. So einfach kann das sein...  :autsch: 
Danke.


----------

