Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem importing Dog class

 
keerthana kuruba
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On observing your code, I believe, you need to import com.example.Dog.
 
keerthana kuruba
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kerthana,

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

 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic