ERROR INFORMATION ListenerTester.java:3: package com.example does not exist
import com.example.*;
^
ListenerTester.java:15: cannot access Dog
bad class file: .\Dog.class
class file contains wrong class: com.example.Dog
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Dog dog = (Dog) getServletContext().getAttribute("dog");
^
2 errors
C:\apache-tomcat-5.5.16\webapps\beer-v1\WEB-INF\classes\com\example>javac Listen
erTester.java
ListenerTester.java:15: cannot access Dog
bad class file: .\Dog.class
class file contains wrong class: com.example.Dog
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Dog dog = (Dog) getServletContext().getAttribute("dog");
^
1 error
Please help me !!! The following is source file. package com.example;
import javax.servlet.*;
public class MyServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
ServletContext sc=event.getServletContext();
String dogBreed=sc.getServletContext("breed");
Dog d=new Dog(dogBreed);
sc.setAttribute("dog",d);
}
public void contextDestroyed(ServletContextEvent event)
{
}
}
package com.example;
public class Dog
{
private String breed;
public Dog(String breed)
{
this.breed;
}
public String getBreed()
{
return breed;
}
}
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ListenerTester extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("test context attributes set by listener<br>");
out.println("<br>");
Dog dog=(Dog) getServletContext().getAttribute("dog");
out.println("Dog's breed is: " + dog.getBreed());
}
}
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com.xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"
<
servlet>
<servlet-name>ListenerTester</servlet-class>
<servlet-class>com.example.ListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenerTest.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>
<listener>
<listener-class>
com.example.MyServletContextListener
</listener-class>
</listener>
</web-app>
[ June 02, 2006: Message edited by: owen huan ]