• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Error while running ServletContextListener

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is my servlet code.


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

public class Fourth extends HttpServlet
{

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
ServletContext ctx = getServletContext();
String dbName = ctx.getInitParameter("dbName");
//Adding to context
ctx.setAttribute("skipper","ganguly");
ctx.setAttribute("skipper","sachin");
ctx.removeAttribute("captain");

//Adding to request
req.setAttribute("captain","waugh");
req.setAttribute("captain","ponting");
req.removeAttribute("captain");
}


}


And my web.xml goes like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<servlet>
<servlet-name>Fourth</servlet-name>
<servlet-class>Fourth</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Fourth</servlet-name>
<url-pattern>/fourth.do</url-pattern>
</servlet-mapping>


<context-param>
<param-name> dbName </param-name>
<param-value> WEBCQLCL</param-value>
</context-param>

<listener>
<listener-class> MycontextListener </listener-class>
<listener-class>MyContextAttributeListener </listener-class>
<listener-class> MyRequestAttributeListener</listener-class>
<listener-class>MyRequestListener </listener-class>
</listener>

</web-app>

And my listener class is


import javax.servlet.*;

public class MyContextAttributeListener implements ServletContextAttributeListener
{

public void attributeAdded(ServletContextAttributeEvent ev){
System.out.println("SName ="+ev.getName());
System.out.println("SValue= "+ev.getValue());
}
public void attributeReplaced(ServletContextAttributeEvent ev){
System.out.println("SName ="+ev.getName());
System.out.println("SValue= "+ev.getValue());
}
public void attributeRemoved(ServletContextAttributeEvent ev){
System.out.println("SName ="+ev.getName());
System.out.println("SValue= "+ev.getValue());
}

}
but when i run the servlet i'm getting the following error.

java.lang.NullPointerException
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159)
Fourth.doGet(Fourth.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

In the server log i'm getting the log from the ServletRequestListener instead of from ServletContextListener.But Ideally servletContextListener should run before RequestListener right?.

RName =javax.servlet.error.exception
RValue= java.lang.NullPointerException

Please help me


[ June 20, 2006: Message edited by: Senthil Kumar SS ]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
senthil,
declare each listener under <listener> element and check the paths.
[ June 20, 2006: Message edited by: Selvaraj Subramanian ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Senthil,

U have implemented ServletContextAttributeListener but what abt the ServletContextListener with methods ContextInitialized and ContextDestroyed.I think u should have implement that also..

Naveen
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Senthil,
did you post your whole code for Fourth.java ?
Didn't you actually override init(ServletConfig config) ?
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou,

yes i did override init(ServletConfig) .but i didn't call super.init(ServletConfig) method.why is happens.i mean why don't get ServletConfig object

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't call super.init, then the following will fail :

because the parent class doesn't own the ServletConfig instance.

You've got to either :
1. call super.init
2. or ServletContext ctx = myconfig.getServletContext(); (assuming that you've stored the ServletConfig into 'myconfig')
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between the init() and init(ServletConfig) methods?
http://faq.javaranch.com/view?ServletsFaq
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Senthil,

ServletContext ctx = getServletContext();

This method will get the servletContext object from ServletConfig object of
this servlet.
As u are stopping the initialization of ServletConfig object by overriding the public void init(ServletConfig config) , the contaier will make a call
on servletConfig object which results in Null Pointer Exception.

This is from API
------------------------------
getServletContext
public ServletContext getServletContext()Returns a reference to the ServletContext in which this servlet is running. See ServletConfig.getServletContext().
This method is supplied for convenience. It gets the context from the servlet's ServletConfig object.

Specified by:
getServletContext in interface ServletConfig
Returns:
ServletContext the ServletContext object passed to this servlet by the init method
---------------------------------------------------------------

----------
SCJP1.4 91%
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou,
thanks a lot
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karne,

Thanks a ton
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey snthil, could u please mail me the code where u have overridden the
init(ServletConfig) mthod. i m really looking after a code example that can hilite the difference between overriding init( ) and overriding
init(ServletConfig).

please mail me on niranjan.deshpande@cognizant.com
 
It runs on an internal combustion engine. This ad does not:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic