File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Error while running ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Error while running ServletContextListener" Watch "Error while running ServletContextListener" New topic
Author

Error while running ServletContextListener

Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
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 ]

when you really want something, all the universe always conspires in your favour.<br /> <br />SCJP1.5-77%<br />SCWCD-89%
Selvaraj Subramanian
Ranch Hand

Joined: Jul 15, 2005
Posts: 37
senthil,
declare each listener under <listener> element and check the paths.
[ June 20, 2006: Message edited by: Selvaraj Subramanian ]

Selvaraj.S<br />SCJP 1.4 -83%<br />SCWCD 1.4 - 85%<br />SCEA 4 Part 1(310-051) - 89%
Naveen Kollipara
Ranch Hand

Joined: Feb 20, 2006
Posts: 32
Senthil,

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

Naveen


Regards,<br />Naveen<br />SCJP 5 - 83%<br />SCWCD - 84%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Senthil,
did you post your whole code for Fourth.java ?
Didn't you actually override init(ServletConfig config) ?


[My Blog]
All roads lead to JavaRanch
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

What is the difference between the init() and init(ServletConfig) methods?
http://faq.javaranch.com/view?ServletsFaq
Karne Reddy
Ranch Hand

Joined: May 28, 2006
Posts: 35
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%


asdfa
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Satou,
thanks a lot
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
karne,

Thanks a ton
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error while running ServletContextListener