aspose file tools*
The moose likes Beginning Java and the fly likes Problem importing Dog class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem importing Dog class" Watch "Problem importing Dog class" New topic
Author

Problem importing Dog class

keerthana kuruba
Greenhorn

Joined: May 12, 2011
Posts: 22
Hi ,

I have been reading headfirst servlet and jsp book from past 2 weeks.Iam getting error When ever i complied the servletContextListener example given in that book. when ever i compile MyServletContextListener.java the below error is coming


C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\listenerTester\WE
B-INF\classes\com\example>javac MyServletContextListener.java
MyServletContextListener.java:14: cannot find symbol
symbol : class Dog
location: class com.example.MyServletContextListener
Dog d =new Dog(dogBreed);
^
MyServletContextListener.java:14: cannot find symbol
symbol : class Dog
location: class com.example.MyServletContextListener
Dog d =new Dog(dogBreed);
^
2 errors


I know it may be simple error to solve.The thing is i have followed all the directory structure ,correct syntax.Still am not getting where i went wrong.newbie in servlets.Here is the code i was trying to get the parameter of dog breed.

ListenerTester.java

------------------------------

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.Breed());
}
}
---------------------------
MyservletContextLister.java

------------------------------

package com.example;

import javax.servlet.*;

public class MyServletContextListener implements ServletContextListener
{

public void contextInitialized(ServletContextEvent event)
{
ServletContext sc=event.getServletContext();

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

Dog d =new Dog(dogBreed);

sc.setAttribute("dog",d);

}

public void contextDestroyed(ServletContextEvent event)

{

}
----------------------------------
Dog.java
---------------------
package com.example;

public class Dog
{
private String breed;

public Dog(String breed)
{
this.breed=breed;
}

public String getBreed()
{
return breed;

}
}
---------------------------
web.xml
--------------------
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmns: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</servelet-name>
<servlet-class>com.example.ListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.example.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>
----------------------------------
please help me to solve the error.










Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

On observing your code, I believe, you need to import com.example.Dog.


S.D. MADHAN
Not many get the right opportunity !
keerthana kuruba
Greenhorn

Joined: May 12, 2011
Posts: 22
Still getting the same error...... .........



C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\listenerTester\WE
B-INF\classes\com\example>javac ListenerTester.java
ListenerTester.java:15: cannot find symbol
symbol : class Dog
location: class com.example.Dog.ListenerTester
Dog dog=(Dog) getServletContext().getAttribute("dog");
^
ListenerTester.java:15: cannot find symbol
symbol : class Dog
location: class com.example.Dog.ListenerTester
Dog dog=(Dog) getServletContext().getAttribute("dog");
^
2 errors


please give me any suggestion and changes to the above program.....
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Hello Kerthana,

Check the link javac for more info on how to compile multiple classes at the same time.....


SCJP 1.4, SCWCD 1.4
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

you have missed import Dog class in ListenerTester.java class.
from the two error logs, that you have posted,first one points to listener and second one points to ListenerTester.java.
Try adding import statement in ListenerTester.java. this might solve your issue


Ashwin Sridhar
SCJP | SCWCD | OCA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem importing Dog class