Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFS page 171

 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
created Dog.java as follows

package com.example;
public class Dog{
private String breed;

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

compiled it using
javac -classpath /tomcat4.1/common/lib/servlet-api.jar:classes:.; C:\j2sdk1.4.2_05\lib\j2ee.jar; -d classes src/com/example/Dog.java
it compiled

But MyServletContextListener and ListenerTester when compiled showed the errors cannot resolve symbol class Dog .....what is thw silly mistake i am doing here

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){

}
}


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 attribute set by listenst<br>");
Dog dog=(Dog) getServletContext().getAttribute("dog");
out.println("Dog's breed:"+dog.getBreed());
}
}
 
vishwa venkat
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try setting classpath..
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deleted
[ September 20, 2004: Message edited by: Bryan Basham ]
 
Jayanthi Mani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,what was the answer to this problem - I am also getting the same error....

Jayanthi.
 
Jayanthi Mani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,Pallavi- this also worked for me:

javac MyServletContextListener.java Dog.java
 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
set classpath=%classpath%;D:\Tomcat4.1\webapps\listenertest\web-inf\classes\com\example

javac Dog.java
javac ListenerTester.java
javac MyServletContextListener.java
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic