• Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletContextListener

 
Sandip Chatterjee
Greenhorn
Posts: 21
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The few steps for preparing a Listener and make it work are somewhat like this -
1. Prepare Deployment Descriptor(web.xml) and put your context params and values.
2. Create the Listener Class.
3. Create the required attribute class.
4. Writing the servlet.

Now, in Servlet, a piece of code to access the Listener and have its benefits are like this(an Eg)

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

Now, getAttribute() returns a type Object(a object of Dog class here), so we need to typecast. 'getServletContext()' returns a ServletContext Object.
So, what is it that we are trying to do, in the above piece of code? Why do we need to have a ServletContext Object(a ServletContext Object of type Dog)?
Why this won't do -
Dog dog = (Dog) getAttribute("dog"); ?
 
Sandip Chatterjee
Greenhorn
Posts: 21
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog dog = (Dog) getServletContext.getAttribute("dog");

Is it because we are accessing getAttribute() from the ServletContext object(which the getServletContext() returns)?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandip Chatterjee wrote:
Now, in Servlet, a piece of code to access the Listener and have its benefits are like this(an Eg)

That code has nothing at all to do with the listener. Presumably, it's accessing a scoped variable named "dog" that has been placed in application scope, perhaps by the listener, but it has nothing to do with the listener per se.

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

This code will not compile. It should be:

So, what is it that we are trying to do, in the above piece of code? Why do we need to have a ServletContext Object(a ServletContext Object of type Dog)?

There are three scopes available to servlets: application, session and request. If the scoped variable named "dog" is placed in application scope, it must be obtained from the servlet context instance. If it's in other scopes, you'd call the getAttribute() method of the appropriate scope.

Why this won't do -
Dog dog = (Dog) getAttribute("dog"); ?

That would call the getAttribute() method of the request to obtain a scoped variable named "doing" in request scope, rather than application scope.
 
Sandip Chatterjee
Greenhorn
Posts: 21
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1. Dog dog = (Dog) getServletContext().getAttribute("dog");

Okay, so the above piece of code has nothing to do with listener. All it is trying to do is access the application scope variable(set by the listener).

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

A typo mistake; missed out the '()'; Sorry!

#3. 3 Scopes to Servlet. So, to access an application scope variable, we are using ServletContext instance. That's great.

#4. Dog dog = (Dog) getAttribute("dog");

In the above line, we are just trying to access the variable "dog" in request scope. // not "doing". typo mistake on your part.

Thanks for the reply, friend. Take Care.



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandip Chatterjee wrote:Okay, so the above piece of code has nothing to do with listener. All it is trying to do is access the application scope variable(set by the listener).

Correct. The fact that scoped variable was created by a listener is moot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!