Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problem importing Dog class

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.










 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kerthana,

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

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic