You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mario Imperato <me...@mclink.it> on 2000/04/14 10:57:14 UTC
getServletContext() Error and init(..)
Hi folks, my apologies for the question. I'm experiencing a strange behaviour from a
test servlet I'm just developing. I wanted to have the servlet loaded at start up time
and added an init(ServletConfig config) method to initialize some stuff.
In the doGet method when I try to get the ServletContext I get a NullPointerException.
This doesn't happen if I remove the init method from the class.
What am I missing?
I'm running on Linux RH with ibm jdk118 (not sure what release).
Thank you in advance for your assistence
Mario
Mario A. Imperato
e-mail: imperato@mclink.it
cell: 0335-5398828
Re: getServletContext() Error and init(..)
Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Mario Imperato wrote:
> Hi folks, my apologies for the question. I'm experiencing a strange behaviour from a
> test servlet I'm just developing. I wanted to have the servlet loaded at start up time
> and added an init(ServletConfig config) method to initialize some stuff.
> In the doGet method when I try to get the ServletContext I get a NullPointerException.
> This doesn't happen if I remove the init method from the class.
> What am I missing?
>
Without looking at the line of code where the NullPointerException is actually thrown,
it's impossible to tell you exactly what the problem is. However, I can describe a very
common case that lots of people get caught on.
At the beginning of your init(ServletConfig config) method, don't forget to call:
super(config);
so that the base class initializer gets called as well. Otherwise, later references to
things like getServletContext() will fail. This error happened to so many people that a
new version of the init() method signature was added (with no arguments) so that you can
override that one instead. You'd do this:
public void init() throws ServletException {
ServletConfig config = getServletConfig();
... all the rest of your current initialization ...
}
> Mario
>
Craig McClanahan
Re: getServletContext() Error and init(..)
Posted by Gael Stehagen <qp...@epk.ericsson.se>.
Hello Mario,
I checked into your problem, and tried it my application.
I worked fine for me. I'm runnig Tomcat 3.1, supporting servlet2.2 API, in case you're
not.
Furthermore I'm using jdk1.2.2 on Solaris.
As I understand, you're having trouble when you include init(ServletConfig confi) method.
I use the later init(), that are supposed to work the same, just less writing...
However I mention this, because it works for me in the doGet() method. I send you an
extract of my code:
using init() as my previous posting...
in doGet(...)
...
String webmaster = getServletContext().getInitParameter("webmaster");
System.out.println("webmaster: " + webmaster);
...
and in web.xml:
<context-param>
<param-name>webmaster</param-name>
<param-value>qpkgael@epk.ericsson.se</param-value>
<description>
The email address of the administrator, to whom questions and
comments about this application should be adressed.
</description>
</context-param>
Hope this gave some feedback...
Mario Imperato wrote:
> Hi folks, my apologies for the question. I'm experiencing a strange behaviour from a
> test servlet I'm just developing. I wanted to have the servlet loaded at start up time
> and added an init(ServletConfig config) method to initialize some stuff.
> In the doGet method when I try to get the ServletContext I get a NullPointerException.
> This doesn't happen if I remove the init method from the class.
> What am I missing?
>
> I'm running on Linux RH with ibm jdk118 (not sure what release).
> Thank you in advance for your assistence
> Mario
>
> Mario A. Imperato
> e-mail: imperato@mclink.it
> cell: 0335-5398828
>
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org
--
************************
Gael Stehagen
qpkgael@epk.ericsson.se
0708-882910