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

cannot find symbol : ServletContextListner

 
Deepti Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to do the ListnerTester example from Head First Servlet & Jsp Book (chapter 5 ).
It has 3 classes
1. Dog (Plain old java code)
2. ListnerTester (A servlet that test the listner class)
3. MyServletContextListner ( A listner that implements ServletContextListner)

I am able to compile Dog & ListnerTester but when i try to compile MyServletContextListner it gives en error cannot find symbol
symbol : class ServletContextListner

The code of MyServletContextListner is as follows

package com.example;

import javax.servlet.*;

public class MyServletContextListner implements ServletContextListner{

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

}



 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!
Did you have javax.servlet.* package? How do you suppose run this code?
 
Deepti Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to compile ListnerTester which also imports javax.servlet

It has the following code

package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ListnerTester 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 Listner<br>");
out.println("<br>");

Dog dog=(Dog) getServletContext().getAttribute("dog");

out.println("Dog's Breed is "+ dog.getBreed());


}
}
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please => UseCodeTags when post code snaps. Use Edit button and insert code tags. It'll be more readable!
For, running a servlet/jsp application, you need a web container like tomcat. Do you have/install it?
 
Deepti Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i have installed tomcat

i am trying to compile it from command prompt using the following command.

D:\MyProjects\listnerTest>javac -classpath c:\tomcat\common\lib\servlet-api.jar;
classes; -d classes src\com\example\MyServletContextListner.java
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The error message means exactly what it says. Why don't you check to see if you spelled the name of the interface correctly?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@deepti..try to put exactly the same copy of error which you got on commond prompt.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul has already identified the problem.
 
Deepti Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the name of the interface is correct.i am attaching the print screen of the command prompt.
error.jpg
Exact error which i got at command prompt
[Thumbnail for error.jpg]
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepti Arora wrote:I think the name of the interface is correct.

It isn't.
i am attaching the print screen of the command prompt.

Which tells you that it is not correct.
 
Deepti Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did not get it...am i implementing the wrong interface?
if yes then instead of ServletContextListner which interface should i implement?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepti Arora wrote:i did not get it...am i implementing the wrong interface?
if yes then instead of ServletContextListner which interface should i implement?

The one without the spelling mistakes.
 
Deepti Arora
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oopss... a silly mistake...
Thanks i am able to compile it now...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic