You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Jose Alfonso Martinez <tr...@linuxcenter.com.mx> on 2003/08/11 19:43:00 UTC

calling getServletContext() in a servlet - how to??

Hello,

I am testing a servlet, just the init() method like this:

	public void testPostMethod() throws ServletException
        {
                InscribeServlet inscribeServlet = new InscribeServlet();
                inscribeServlet.init(config);
        }

I get a java.lang.NullPointerException when calling getServletContext()...

in the init() method, my servlet calls getServletContext().getRealPath(). I guess there is where the exception occurs. It could also be in a call to get the DataSource via InitialContext. Attached is the stack trace and the init() method of my Servlet.

Does anyone knows what am I doing wrong???? thanks in advance.

Jose


Stack Trace
============

test2:
    [junit] Running com.cevex.servlet.TestInscribeServlet
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562 sec
    [junit] Testsuite: com.cevex.servlet.TestInscribeServlet
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562 sec

    [junit] Testcase: testPostMethod took 1.514 sec
    [junit] 	Caused an ERROR
    [junit] null
    [junit] java.lang.NullPointerException
    [junit] 	at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:204)
    [junit] 	at com.cevex.servlet.InscribeServlet.init(Unknown Source)
    [junit] 	at javax.servlet.GenericServlet.init(GenericServlet.java:256)
    [junit] 	at com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown Source)
    [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [junit] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [junit] 	at org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(ServerTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214)
    [junit] 	at org.apache.cactus.ServletTestCase.runTest(ServletTestCase.java:280)
    [junit] 	at org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:251)
    [junit] 	at org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:156)
    [junit] 	at org.apache.cactus.server.AbstractWebTestController.dispatch133_handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:130)
    [junit] 	at org.apache.cactus.server.AbstractWebTestController.around133_handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
    [junit] 	at org.apache.cactus.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
    [junit] 	at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
    [junit] 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    [junit] 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    [junit] 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
    [junit] 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
    [junit] 	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
    [junit] 	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    [junit] 	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    [junit] 	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
    [junit] 	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
    [junit] 	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
    [junit] 	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
    [junit] 	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
    [junit] 	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
    [junit] 	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
    [junit] 	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
    [junit] 	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
    [junit] 	at java.lang.Thread.run(Thread.java:536)




And the init() method of my Servlet
===================================
  public void init() throws CevexServletException
  {
    try
    {
      // get the initial context
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");

      // get the datasource
      ds = (DataSource) envCtx.lookup("jdbc/cevexDB");

      /* something is worng with the mail session, check later */
      //session = (Session) envCtx.lookup("mail/Session");         

      // initialize new mail seession
      Properties props = new Properties();
      props.put("mail.smtp.host", "localhost");
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(false);

      // Read email strings from files
      String realPath = getServletContext().getRealPath("/");
      mailInscripcionMasc = new CevexMail(session, realPath + getInitParameter("mailInscripcionMasc"));
      mailInscripcionFem  = new CevexMail(session, realPath + getInitParameter("mailInscripcionFem"));
      mailInscripcionConInvitacionMasc = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionMasc"));
      mailInscripcionConInvitacionFem = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionFem"));

    } // end try 
    .... catch statements
 }	

Re: calling getServletContext() in a servlet - how to??

Posted by Jose Alfonso Martinez <tr...@linuxcenter.com.mx>.
Thanks Vincent, 

Well I am going on vacation right now. I will follow your suggestion
when I got back and I will tell you what the error was so that everybody
knows...

Good wishes to anyone wherever in the world you are...

Jose


On Wed, Aug 13, 2003 at 08:07:42AM +0200, Vincent Massol wrote:
> 
> 
> > -----Original Message-----
> > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > Sent: 13 August 2003 03:17
> > To: Cactus Users List
> > Subject: Re: calling getServletContext() in a servlet - how to??
> > 
> > Yes, well I am only showing the init() method of InscribeServlet :(
> > I don't know if I could/should show more. If you think
> > it could be of any help I will give it another thought ok? :)
> 
> Ok. Here's what I suggest. Try writing another servlet, but a minimal
> one that reproduces the problem. Normally, by doing this, you'll find
> out that that servlet works, and by diffing with your InscribeServlet
> you'll see what was wrong in that one.
> 
> The only error I can see below is in TestInscribeServlet. You are using
> a ServletTestSuite. This is not needed. This should be used only for
> wrapping pure junit TestCase (that's the goal of it!). If you're
> extending ServletTestCase (as you are), you can simply remove your
> suite() method.
> 
> Try it! Maybe it'll help...
> 
> Thanks
> -Vincent
> 
> > 
> > ... since we both will benefit from doing it,  specially me :)
> > 
> > 
> > I was thinking it could be a classpath issue.  Something that avoids
> the
> > ServletRedirector to create the config object. Anyway, I may not
> > have an idea of what I am talking about. I will keep trying
> > and if I find out something... I'll tell you
> > 
> > Thanks a lot
> > 
> > 
> > TestInscribeServlet:
> > =====================
> > 
> > package com.cevex.servlet;
> > 
> > import com.cevex.servlet.InscribeServlet;
> > 
> > import javax.servlet.ServletException;
> > 
> > import org.apache.cactus.ServletTestCase;
> > import org.apache.cactus.ServletTestSuite;
> > import org.apache.cactus.WebRequest;
> > 
> > import junit.framework.Test;
> > 
> > public class TestInscribeServlet extends ServletTestCase
> > {
> >         public static void main(String[] theArgs)
> >         {
> >                 junit.textui.TestRunner.main(new String[]
> > {TestInscribeServlet.class.getName()});
> >         }
> > 
> >         public static Test suite()
> >         {
> >                 ServletTestSuite suite = new ServletTestSuite();
> >                 suite.addTestSuite(TestInscribeServlet.class);
> >                 return suite;
> >         }
> > 
> >         public void beginPostMethod(WebRequest theRequest)
> >         {
> >                 theRequest.addParameter("nombres", "Juan Alberto");
> >                 theRequest.addParameter("apellidos", "Robles");
> >                 theRequest.addParameter("email", "email@email.com");
> >                 theRequest.addParameter("ciudad", "Guadalajara");
> >                 theRequest.addParameter("estado", "Jalisco");
> >                 theRequest.addParameter("pais", "México");
> >                 theRequest.addParameter("deseaBoletines", "true");
> >                 theRequest.addParameter("tipoDeInscripcion",
> "regular");
> >                 theRequest.addParameter("sexo", "true");
> >                 theRequest.addParameter("anioNacimiento", "1988");
> >                 theRequest.addParameter("directo", "");
> >                 theRequest.addParameter("confirmacion", "true");
> >         }
> > 
> >         public void testPostMethod() throws ServletException
> >         {
> >                 assertNotNull("aha config is null", config);
> >                 InscribeServlet inscribeServlet = new
> InscribeServlet();
> >                 inscribeServlet.init(config);
> >                 //              inscribeServlet.doPost(request,
> response);
> >         }
> > 
> > }
> > 
> > 
> > 
> > InscribeServlet:
> > ====================
> > 
> > public class InscribeServlet extends HttpServlet {
> > 
> > ... some member fields declarations
> > 
> >   /** Initialize this servlet
> >    *  <UL>
> >    *  <LI> Get the context's datasource</LI>
> >    *  <LI> Initialize mail session </LI>
> >    *  <LI> Read emails </LI>
> >    *  </UL>
> >    **/
> >   public void init() throws CevexServletException
> >   {
> >     try
> >     {
> >       // get the context
> >       Context initCtx = new InitialContext();
> >       Context envCtx = (Context) initCtx.lookup("java:comp/env");
> > 
> >       // get the datasource
> >       ds = (DataSource) envCtx.lookup("jdbc/cevexDB");
> > 
> >       /* something is worng with the mail session, check later */
> >       //session = (Session) envCtx.lookup("mail/Session");
> > 
> >       // initialize new mail seession
> >       Properties props = new Properties();
> >       props.put("mail.smtp.host", "localhost");
> >       Session session = Session.getDefaultInstance(props, null);
> >       session.setDebug(false);
> > 
> >       // Read email strings from files
> >       String realPath = getServletContext().getRealPath("/");
> >       mailInscripcionMasc = new CevexMail(session, realPath +
> > getInitParameter("mailInscripcionMasc"));
> >       mailInscripcionFem  = new CevexMail(session, realPath +
> > getInitParameter("mailInscripcionFem"));
> >       mailInscripcionConInvitacionMasc = new CevexMail(session,
> realPath +
> > 
> > getInitParameter("mailInscripcionConInvitacionMasc"));
> >       mailInscripcionConInvitacionFem = new CevexMail(session,
> realPath +
> > 
> > getInitParameter("mailInscripcionConInvitacionFem"));
> > 
> >     } // end try
> >     catch (FileNotFoundException fe) {
> >        getServletContext().log("InscribeServlet: Al inicializar el
> > servlet, " +
> >                                "no se encontro un archivo: " +
> > fe.getMessage());
> >        throw new CevexServletException(getServletConfig(), "No se
> encontro
> > un archivo: " +
> >                                        fe.getMessage() + " al iniciar
> > InscribeServlet");
> >     }
> >     catch (IOException ioe)
> >     {
> >        getServletContext().log("InscribeServlet: Error al leer el
> archivo:
> > " + ioe.getMessage());
> >        throw new CevexServletException(getServletConfig(), "Error de
> > escritura en el archivo: " +
> >                                        ioe.getMessage() + " al iniciar
> > InscribeServlet");
> >     }
> >     catch (MessagingException me)
> >     {
> >         throw new CevexServletException(getServletConfig(), me);
> >     }
> >     catch (NamingException ne)
> >     {
> >        throw new CevexServletException(getServletConfig(), ne);
> >     }
> >   } //end init
> > 
> > 
> >   // Temporary allow GET for testing
> >    public void doGet(HttpServletRequest req,
> >            HttpServletResponse res) throws ServletException,
> IOException {
> >      doPost(req, res);
> >    }
> > 
> >    public void doPost(HttpServletRequest req,
> >            HttpServletResponse res) throws ServletException,
> IOException {
> > 
> > 
> > 
> > 	... bla bla bla ...
> >    }
> > }
> > 
> > 
> > 
> > On Tue, Aug 12, 2003 at 09:32:13PM +0200, Vincent Massol wrote:
> > >
> > >
> > > > -----Original Message-----
> > > > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > > > Sent: 12 August 2003 19:32
> > > > To: Cactus Users List
> > > > Subject: Re: calling getServletContext() in a servlet - how to??
> > > >
> > > >
> > > > In fact, config is null
> > > >
> > > > why does it happen???
> > > >
> > >
> > > If you had asked me, I would have said it isn't possible, seen the
> > > Cactus implementation code! :-)
> > >
> > > Would it be possible for you to send us your InscribeServlet and
> your
> > > TestInscribeServlet code?
> > >
> > > The only other option I see is that you happen to have an instance
> > > variable called config that isn't initialized... but then I'm pretty
> > > sure you would have noticed that... ;-)
> > >
> > > Thanks
> > > -Vincent
> > >
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> 

RE: calling getServletContext() in a servlet - how to??

Posted by Vincent Massol <vm...@pivolis.com>.

> -----Original Message-----
> From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> Sent: 13 August 2003 03:17
> To: Cactus Users List
> Subject: Re: calling getServletContext() in a servlet - how to??
> 
> Yes, well I am only showing the init() method of InscribeServlet :(
> I don't know if I could/should show more. If you think
> it could be of any help I will give it another thought ok? :)

Ok. Here's what I suggest. Try writing another servlet, but a minimal
one that reproduces the problem. Normally, by doing this, you'll find
out that that servlet works, and by diffing with your InscribeServlet
you'll see what was wrong in that one.

The only error I can see below is in TestInscribeServlet. You are using
a ServletTestSuite. This is not needed. This should be used only for
wrapping pure junit TestCase (that's the goal of it!). If you're
extending ServletTestCase (as you are), you can simply remove your
suite() method.

Try it! Maybe it'll help...

Thanks
-Vincent

> 
> ... since we both will benefit from doing it,  specially me :)
> 
> 
> I was thinking it could be a classpath issue.  Something that avoids
the
> ServletRedirector to create the config object. Anyway, I may not
> have an idea of what I am talking about. I will keep trying
> and if I find out something... I'll tell you
> 
> Thanks a lot
> 
> 
> TestInscribeServlet:
> =====================
> 
> package com.cevex.servlet;
> 
> import com.cevex.servlet.InscribeServlet;
> 
> import javax.servlet.ServletException;
> 
> import org.apache.cactus.ServletTestCase;
> import org.apache.cactus.ServletTestSuite;
> import org.apache.cactus.WebRequest;
> 
> import junit.framework.Test;
> 
> public class TestInscribeServlet extends ServletTestCase
> {
>         public static void main(String[] theArgs)
>         {
>                 junit.textui.TestRunner.main(new String[]
> {TestInscribeServlet.class.getName()});
>         }
> 
>         public static Test suite()
>         {
>                 ServletTestSuite suite = new ServletTestSuite();
>                 suite.addTestSuite(TestInscribeServlet.class);
>                 return suite;
>         }
> 
>         public void beginPostMethod(WebRequest theRequest)
>         {
>                 theRequest.addParameter("nombres", "Juan Alberto");
>                 theRequest.addParameter("apellidos", "Robles");
>                 theRequest.addParameter("email", "email@email.com");
>                 theRequest.addParameter("ciudad", "Guadalajara");
>                 theRequest.addParameter("estado", "Jalisco");
>                 theRequest.addParameter("pais", "México");
>                 theRequest.addParameter("deseaBoletines", "true");
>                 theRequest.addParameter("tipoDeInscripcion",
"regular");
>                 theRequest.addParameter("sexo", "true");
>                 theRequest.addParameter("anioNacimiento", "1988");
>                 theRequest.addParameter("directo", "");
>                 theRequest.addParameter("confirmacion", "true");
>         }
> 
>         public void testPostMethod() throws ServletException
>         {
>                 assertNotNull("aha config is null", config);
>                 InscribeServlet inscribeServlet = new
InscribeServlet();
>                 inscribeServlet.init(config);
>                 //              inscribeServlet.doPost(request,
response);
>         }
> 
> }
> 
> 
> 
> InscribeServlet:
> ====================
> 
> public class InscribeServlet extends HttpServlet {
> 
> ... some member fields declarations
> 
>   /** Initialize this servlet
>    *  <UL>
>    *  <LI> Get the context's datasource</LI>
>    *  <LI> Initialize mail session </LI>
>    *  <LI> Read emails </LI>
>    *  </UL>
>    **/
>   public void init() throws CevexServletException
>   {
>     try
>     {
>       // get the context
>       Context initCtx = new InitialContext();
>       Context envCtx = (Context) initCtx.lookup("java:comp/env");
> 
>       // get the datasource
>       ds = (DataSource) envCtx.lookup("jdbc/cevexDB");
> 
>       /* something is worng with the mail session, check later */
>       //session = (Session) envCtx.lookup("mail/Session");
> 
>       // initialize new mail seession
>       Properties props = new Properties();
>       props.put("mail.smtp.host", "localhost");
>       Session session = Session.getDefaultInstance(props, null);
>       session.setDebug(false);
> 
>       // Read email strings from files
>       String realPath = getServletContext().getRealPath("/");
>       mailInscripcionMasc = new CevexMail(session, realPath +
> getInitParameter("mailInscripcionMasc"));
>       mailInscripcionFem  = new CevexMail(session, realPath +
> getInitParameter("mailInscripcionFem"));
>       mailInscripcionConInvitacionMasc = new CevexMail(session,
realPath +
> 
> getInitParameter("mailInscripcionConInvitacionMasc"));
>       mailInscripcionConInvitacionFem = new CevexMail(session,
realPath +
> 
> getInitParameter("mailInscripcionConInvitacionFem"));
> 
>     } // end try
>     catch (FileNotFoundException fe) {
>        getServletContext().log("InscribeServlet: Al inicializar el
> servlet, " +
>                                "no se encontro un archivo: " +
> fe.getMessage());
>        throw new CevexServletException(getServletConfig(), "No se
encontro
> un archivo: " +
>                                        fe.getMessage() + " al iniciar
> InscribeServlet");
>     }
>     catch (IOException ioe)
>     {
>        getServletContext().log("InscribeServlet: Error al leer el
archivo:
> " + ioe.getMessage());
>        throw new CevexServletException(getServletConfig(), "Error de
> escritura en el archivo: " +
>                                        ioe.getMessage() + " al iniciar
> InscribeServlet");
>     }
>     catch (MessagingException me)
>     {
>         throw new CevexServletException(getServletConfig(), me);
>     }
>     catch (NamingException ne)
>     {
>        throw new CevexServletException(getServletConfig(), ne);
>     }
>   } //end init
> 
> 
>   // Temporary allow GET for testing
>    public void doGet(HttpServletRequest req,
>            HttpServletResponse res) throws ServletException,
IOException {
>      doPost(req, res);
>    }
> 
>    public void doPost(HttpServletRequest req,
>            HttpServletResponse res) throws ServletException,
IOException {
> 
> 
> 
> 	... bla bla bla ...
>    }
> }
> 
> 
> 
> On Tue, Aug 12, 2003 at 09:32:13PM +0200, Vincent Massol wrote:
> >
> >
> > > -----Original Message-----
> > > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > > Sent: 12 August 2003 19:32
> > > To: Cactus Users List
> > > Subject: Re: calling getServletContext() in a servlet - how to??
> > >
> > >
> > > In fact, config is null
> > >
> > > why does it happen???
> > >
> >
> > If you had asked me, I would have said it isn't possible, seen the
> > Cactus implementation code! :-)
> >
> > Would it be possible for you to send us your InscribeServlet and
your
> > TestInscribeServlet code?
> >
> > The only other option I see is that you happen to have an instance
> > variable called config that isn't initialized... but then I'm pretty
> > sure you would have noticed that... ;-)
> >
> > Thanks
> > -Vincent
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Re: calling getServletContext() in a servlet - how to??

Posted by Jose Alfonso Martinez <tr...@linuxcenter.com.mx>.
Yes, well I am only showing the init() method of InscribeServlet :(
I don't know if I could/should show more. If you think
it could be of any help I will give it another thought ok? :)

... since we both will benefit from doing it,  specially me :)  


I was thinking it could be a classpath issue.  Something that avoids the
ServletRedirector to create the config object. Anyway, I may not 
have an idea of what I am talking about. I will keep trying 
and if I find out something... I'll tell you

Thanks a lot


TestInscribeServlet:
=====================

package com.cevex.servlet;

import com.cevex.servlet.InscribeServlet;

import javax.servlet.ServletException;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.ServletTestSuite;
import org.apache.cactus.WebRequest;

import junit.framework.Test;

public class TestInscribeServlet extends ServletTestCase
{
        public static void main(String[] theArgs)
        {
                junit.textui.TestRunner.main(new String[] {TestInscribeServlet.class.getName()});
        }

        public static Test suite()
        {
                ServletTestSuite suite = new ServletTestSuite();
                suite.addTestSuite(TestInscribeServlet.class);
                return suite;
        }

        public void beginPostMethod(WebRequest theRequest)
        {
                theRequest.addParameter("nombres", "Juan Alberto");
                theRequest.addParameter("apellidos", "Robles");
                theRequest.addParameter("email", "email@email.com");
                theRequest.addParameter("ciudad", "Guadalajara");
                theRequest.addParameter("estado", "Jalisco");
                theRequest.addParameter("pais", "México");
                theRequest.addParameter("deseaBoletines", "true");
                theRequest.addParameter("tipoDeInscripcion", "regular");
                theRequest.addParameter("sexo", "true");
                theRequest.addParameter("anioNacimiento", "1988");
                theRequest.addParameter("directo", "");
                theRequest.addParameter("confirmacion", "true");
        }

        public void testPostMethod() throws ServletException
        {
                assertNotNull("aha config is null", config);
                InscribeServlet inscribeServlet = new InscribeServlet();
                inscribeServlet.init(config);
                //              inscribeServlet.doPost(request, response);
        }

}



InscribeServlet: 
====================

public class InscribeServlet extends HttpServlet {

... some member fields declarations

  /** Initialize this servlet 
   *  <UL>
   *  <LI> Get the context's datasource</LI>
   *  <LI> Initialize mail session </LI>
   *  <LI> Read emails </LI>
   *  </UL>
   **/
  public void init() throws CevexServletException
  {
    try
    {
      // get the context
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");

      // get the datasource
      ds = (DataSource) envCtx.lookup("jdbc/cevexDB");

      /* something is worng with the mail session, check later */
      //session = (Session) envCtx.lookup("mail/Session");         

      // initialize new mail seession
      Properties props = new Properties();
      props.put("mail.smtp.host", "localhost");
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(false);

      // Read email strings from files
      String realPath = getServletContext().getRealPath("/");
      mailInscripcionMasc = new CevexMail(session, realPath + getInitParameter("mailInscripcionMasc"));
      mailInscripcionFem  = new CevexMail(session, realPath + getInitParameter("mailInscripcionFem"));
      mailInscripcionConInvitacionMasc = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionMasc"));
      mailInscripcionConInvitacionFem = new CevexMail(session, realPath +
                                             getInitParameter("mailInscripcionConInvitacionFem"));

    } // end try 
    catch (FileNotFoundException fe) {
       getServletContext().log("InscribeServlet: Al inicializar el servlet, " +
                               "no se encontro un archivo: " + fe.getMessage());
       throw new CevexServletException(getServletConfig(), "No se encontro un archivo: " +
                                       fe.getMessage() + " al iniciar InscribeServlet");
    }
    catch (IOException ioe)
    {
       getServletContext().log("InscribeServlet: Error al leer el archivo: " + ioe.getMessage());
       throw new CevexServletException(getServletConfig(), "Error de escritura en el archivo: " +
                                       ioe.getMessage() + " al iniciar InscribeServlet");
    }
    catch (MessagingException me)
    {
        throw new CevexServletException(getServletConfig(), me);
    }
    catch (NamingException ne)
    {
       throw new CevexServletException(getServletConfig(), ne);
    }
  } //end init 


  // Temporary allow GET for testing
   public void doGet(HttpServletRequest req,
           HttpServletResponse res) throws ServletException, IOException {
     doPost(req, res);
   }

   public void doPost(HttpServletRequest req,
           HttpServletResponse res) throws ServletException, IOException {

	

	... bla bla bla ...
   }
}



On Tue, Aug 12, 2003 at 09:32:13PM +0200, Vincent Massol wrote:
> 
> 
> > -----Original Message-----
> > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > Sent: 12 August 2003 19:32
> > To: Cactus Users List
> > Subject: Re: calling getServletContext() in a servlet - how to??
> > 
> > 
> > In fact, config is null
> > 
> > why does it happen???
> > 
> 
> If you had asked me, I would have said it isn't possible, seen the
> Cactus implementation code! :-)
> 
> Would it be possible for you to send us your InscribeServlet and your
> TestInscribeServlet code?
> 
> The only other option I see is that you happen to have an instance
> variable called config that isn't initialized... but then I'm pretty
> sure you would have noticed that... ;-)
> 
> Thanks
> -Vincent
> 

RE: calling getServletContext() in a servlet - how to??

Posted by Vincent Massol <vm...@pivolis.com>.

> -----Original Message-----
> From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> Sent: 12 August 2003 19:32
> To: Cactus Users List
> Subject: Re: calling getServletContext() in a servlet - how to??
> 
> 
> In fact, config is null
> 
> why does it happen???
> 

If you had asked me, I would have said it isn't possible, seen the
Cactus implementation code! :-)

Would it be possible for you to send us your InscribeServlet and your
TestInscribeServlet code?

The only other option I see is that you happen to have an instance
variable called config that isn't initialized... but then I'm pretty
sure you would have noticed that... ;-)

Thanks
-Vincent

> hey thanks a lot Vincent, here is the stack trace when run via
browser:
> 
> junit.framework.AssertionFailedError: aha config is null
> 	at com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown
> Source)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:
> 39)
> 	at
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Im
> pl.java:25)
> 	at
>
org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(S
er
>
verTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214
)
> 	at
>
org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCal
le
> r.java:156)
> 	at
>
org.apache.cactus.server.AbstractWebTestController.dispatch133_handleReq
ue
>
st(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.a
j[
> 1k]:130)
> 	at
>
org.apache.cactus.server.AbstractWebTestController.around133_handleReque
st
>
(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[
1k
> ]:1158)
> 	at
>
org.apache.cactus.server.AbstractWebTestController.handleRequest(Abstrac
tW
> ebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
> 	at
>
org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(Servle
tT
> estRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
> 	at
>
org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletT
es
> tRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
> 	at
>
org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirec
to
> r.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
> 	at
>
org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(Servlet
Te
> stRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
> 	at
>
org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTe
st
> Redirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
> 	at
>
org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirect
or
> .java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> 	at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
ti
> onFilterChain.java:247)
> 	at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilt
er
> Chain.java:193)
> 	at
>
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValv
e.
> java:260)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
> 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
> 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> 	at
>
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValv
e.
> java:191)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
> 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
> 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> 	at
>
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:241
5)
> 	at
>
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java
:1
> 80)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
> 	at
>
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherVa
lv
> e.java:170)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:641)
> 	at
>
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java
:1
> 72)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:641)
> 	at
>
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509
)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:641)
> 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
> 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> 	at
>
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.
ja
> va:174)
> 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
> 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
> 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> 	at
>
org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
> 	at
>
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:43
2)
> 	at
>
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processC
on
> nection(Http11Protocol.java:386)
> 	at
>
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:53
4)
> 	at
>
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool
.j
> ava:530)
> 	at java.lang.Thread.run(Thread.java:536)
> 
> 
> 
> On Mon, Aug 11, 2003 at 08:35:43PM +0200, Vincent Massol wrote:
> > Hi Jose,
> >
> > Hmmm... very strange.
> >
> > Normally I would have said:
> >
> > Seen that the implementation of init() is:
> >
> >     public void init(ServletConfig config) throws ServletException {
> > 	this.config = config;
> > 	this.init();
> >     }
> >
> > and that line 204 of servlet-api-4 is:
> >
> >     public ServletContext getServletContext() {
> > 	return getServletConfig().getServletContext();
> >     }
> >
> > and that:
> >
> >     public ServletConfig getServletConfig() {
> > 	return config;
> >     }
> >
> > my conclusion is that your InscribeServlet must somehow override the
> > init(ServletConfig) method and it must not do a
super.init(config)...
> >
> > However, the stack trace says:
> >
> > > javax.servlet.GenericServlet.init(GenericServlet.java:256)
> >
> > and line 256 is:
> >
> > 254    public void init(ServletConfig config) throws
ServletException {
> > 255	this.config = config;
> > 256	this.init();
> > 257    }
> >
> > thus it means this.config = config must have been called... So
you're
> > not overriding GenericServlet.init(ServletConfig) or if you do you
are
> > correctly calling super.init(config)...
> >
> > hmm....
> >
> > Can you test that config is not null:
> >
> >  	public void testPostMethod() throws ServletException
> >          {
> >                  assertNotNull("aha config is null", config);
> >                  InscribeServlet inscribeServlet = new
> > InscribeServlet();
> >                  inscribeServlet.init(config);
> >          }
> >
> > Thanks
> > -Vincent
> >
> > > -----Original Message-----
> > > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > > Sent: 11 August 2003 19:43
> > > To: cactus-user@jakarta.apache.org
> > > Subject: calling getServletContext() in a servlet - how to??
> > >
> > > Hello,
> > >
> > > I am testing a servlet, just the init() method like this:
> > >
> > > 	public void testPostMethod() throws ServletException
> > >         {
> > >                 InscribeServlet inscribeServlet = new
> > InscribeServlet();
> > >                 inscribeServlet.init(config);
> > >         }
> > >
> > > I get a java.lang.NullPointerException when calling
> > getServletContext()...
> > >
> > > in the init() method, my servlet calls
> > getServletContext().getRealPath().
> > > I guess there is where the exception occurs. It could also be in a
> > call to
> > > get the DataSource via InitialContext. Attached is the stack trace
and
> > the
> > > init() method of my Servlet.
> > >
> > > Does anyone knows what am I doing wrong???? thanks in advance.
> > >
> > > Jose
> > >
> > >
> > > Stack Trace
> > > ============
> > >
> > > test2:
> > >     [junit] Running com.cevex.servlet.TestInscribeServlet
> > >     [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed:
1.562
> > sec
> > >     [junit] Testsuite: com.cevex.servlet.TestInscribeServlet
> > >     [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed:
1.562
> > sec
> > >
> > >     [junit] Testcase: testPostMethod took 1.514 sec
> > >     [junit] 	Caused an ERROR
> > >     [junit] null
> > >     [junit] java.lang.NullPointerException
> > >     [junit] 	at
> > >
> >
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:204)
> > >     [junit] 	at
com.cevex.servlet.InscribeServlet.init(Unknown
> > > Source)
> > >     [junit] 	at
> > > javax.servlet.GenericServlet.init(GenericServlet.java:256)
> > >     [junit] 	at
> > > com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown
Source)
> > >     [junit] 	at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> > > Method)
> > >     [junit] 	at
> > >
> >
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
> > a:
> > > 39)
> > >     [junit] 	at
> > >
> >
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
> > Im
> > > pl.java:25)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(S
> > er
> > >
> >
verTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214
> > )
> > >     [junit] 	at
> > >
org.apache.cactus.ServletTestCase.runTest(ServletTestCase.java:280)
> > >     [junit] 	at
> > >
org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:251)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCal
> > le
> > > r.java:156)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.AbstractWebTestController.dispatch133_handleReq
> > ue
> > >
> >
st(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.a
> > j[
> > > 1k]:130)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.AbstractWebTestController.around133_handleReque
> > st
> > >
> >
(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[
> > 1k
> > > ]:1158)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.AbstractWebTestController.handleRequest(Abstrac
> > tW
> > >
ebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(Servle
> > tT
> > >
estRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletT
> > es
> > > tRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirec
> > to
> > > r.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(Servlet
> > Te
> > > stRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTe
> > st
> > > Redirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
> > >     [junit] 	at
> > >
> >
org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirect
> > or
> > > .java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
> > >     [junit] 	at
> > > javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
> > >     [junit] 	at
> > > javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
> > ti
> > > onFilterChain.java:247)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilt
> > er
> > > Chain.java:193)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValv
> > e.
> > > java:260)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:643)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> > 80
> > > )
> > >     [junit] 	at
> > >
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValv
> > e.
> > > java:191)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:643)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> > 80
> > > )
> > >     [junit] 	at
> > >
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:241
> > 5)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java
> > :1
> > > 80)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:643)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherVa
> > lv
> > > e.java:170)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:641)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java
> > :1
> > > 72)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:641)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509
> > )
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:641)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> > 80
> > > )
> > >     [junit] 	at
> > >
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.
> > ja
> > > va:174)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> > nv
> > > okeNext(StandardPipeline.java:643)
> > >     [junit] 	at
> > >
> >
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> > 80
> > > )
> > >     [junit] 	at
> > >
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> > >     [junit] 	at
> > >
> >
org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
> > >     [junit] 	at
> > >
> >
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:43
> > 2)
> > >     [junit] 	at
> > >
> >
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processC
> > on
> > > nection(Http11Protocol.java:386)
> > >     [junit] 	at
> > >
> >
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:53
> > 4)
> > >     [junit] 	at
> > >
> >
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool
> > .j
> > > ava:530)
> > >     [junit] 	at java.lang.Thread.run(Thread.java:536)
> > >
> > >
> > >
> > >
> > > And the init() method of my Servlet
> > > ===================================
> > >   public void init() throws CevexServletException
> > >   {
> > >     try
> > >     {
> > >       // get the initial context
> > >       Context initCtx = new InitialContext();
> > >       Context envCtx = (Context) initCtx.lookup("java:comp/env");
> > >
> > >       // get the datasource
> > >       ds = (DataSource) envCtx.lookup("jdbc/cevexDB");
> > >
> > >       /* something is worng with the mail session, check later */
> > >       //session = (Session) envCtx.lookup("mail/Session");
> > >
> > >       // initialize new mail seession
> > >       Properties props = new Properties();
> > >       props.put("mail.smtp.host", "localhost");
> > >       Session session = Session.getDefaultInstance(props, null);
> > >       session.setDebug(false);
> > >
> > >       // Read email strings from files
> > >       String realPath = getServletContext().getRealPath("/");
> > >       mailInscripcionMasc = new CevexMail(session, realPath +
> > > getInitParameter("mailInscripcionMasc"));
> > >       mailInscripcionFem  = new CevexMail(session, realPath +
> > > getInitParameter("mailInscripcionFem"));
> > >       mailInscripcionConInvitacionMasc = new CevexMail(session,
> > realPath +
> > >
> > > getInitParameter("mailInscripcionConInvitacionMasc"));
> > >       mailInscripcionConInvitacionFem = new CevexMail(session,
> > realPath +
> > >
> > > getInitParameter("mailInscripcionConInvitacionFem"));
> > >
> > >     } // end try
> > >     .... catch statements
> > >  }
> > >
> > >
---------------------------------------------------------------------
> > > To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
cactus-user-help@jakarta.apache.org
> >
> >
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Re: calling getServletContext() in a servlet - how to??

Posted by Jose Alfonso Martinez <tr...@linuxcenter.com.mx>.
In fact, config is null

why does it happen???

hey thanks a lot Vincent, here is the stack trace when run via browser:

junit.framework.AssertionFailedError: aha config is null
	at com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(ServerTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214)
	at org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:156)
	at org.apache.cactus.server.AbstractWebTestController.dispatch133_handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:130)
	at org.apache.cactus.server.AbstractWebTestController.around133_handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
	at org.apache.cactus.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
	at org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
	at org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
	at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
	at org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
	at org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
	at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
	at java.lang.Thread.run(Thread.java:536)



On Mon, Aug 11, 2003 at 08:35:43PM +0200, Vincent Massol wrote:
> Hi Jose,
> 
> Hmmm... very strange.
> 
> Normally I would have said:
> 
> Seen that the implementation of init() is:
> 
>     public void init(ServletConfig config) throws ServletException {
> 	this.config = config;
> 	this.init();
>     }
> 
> and that line 204 of servlet-api-4 is:
> 
>     public ServletContext getServletContext() {
> 	return getServletConfig().getServletContext();
>     }
> 
> and that:
> 
>     public ServletConfig getServletConfig() {
> 	return config;
>     }
> 
> my conclusion is that your InscribeServlet must somehow override the
> init(ServletConfig) method and it must not do a super.init(config)...
> 
> However, the stack trace says:
> 
> > javax.servlet.GenericServlet.init(GenericServlet.java:256)
> 
> and line 256 is:
> 
> 254    public void init(ServletConfig config) throws ServletException {
> 255	this.config = config;
> 256	this.init();
> 257    }
> 
> thus it means this.config = config must have been called... So you're
> not overriding GenericServlet.init(ServletConfig) or if you do you are
> correctly calling super.init(config)...
> 
> hmm....
> 
> Can you test that config is not null:
> 
>  	public void testPostMethod() throws ServletException
>          {
>                  assertNotNull("aha config is null", config);
>                  InscribeServlet inscribeServlet = new
> InscribeServlet();
>                  inscribeServlet.init(config);
>          }
> 
> Thanks
> -Vincent
> 
> > -----Original Message-----
> > From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> > Sent: 11 August 2003 19:43
> > To: cactus-user@jakarta.apache.org
> > Subject: calling getServletContext() in a servlet - how to??
> > 
> > Hello,
> > 
> > I am testing a servlet, just the init() method like this:
> > 
> > 	public void testPostMethod() throws ServletException
> >         {
> >                 InscribeServlet inscribeServlet = new
> InscribeServlet();
> >                 inscribeServlet.init(config);
> >         }
> > 
> > I get a java.lang.NullPointerException when calling
> getServletContext()...
> > 
> > in the init() method, my servlet calls
> getServletContext().getRealPath().
> > I guess there is where the exception occurs. It could also be in a
> call to
> > get the DataSource via InitialContext. Attached is the stack trace and
> the
> > init() method of my Servlet.
> > 
> > Does anyone knows what am I doing wrong???? thanks in advance.
> > 
> > Jose
> > 
> > 
> > Stack Trace
> > ============
> > 
> > test2:
> >     [junit] Running com.cevex.servlet.TestInscribeServlet
> >     [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562
> sec
> >     [junit] Testsuite: com.cevex.servlet.TestInscribeServlet
> >     [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562
> sec
> > 
> >     [junit] Testcase: testPostMethod took 1.514 sec
> >     [junit] 	Caused an ERROR
> >     [junit] null
> >     [junit] java.lang.NullPointerException
> >     [junit] 	at
> >
> javax.servlet.GenericServlet.getServletContext(GenericServlet.java:204)
> >     [junit] 	at com.cevex.servlet.InscribeServlet.init(Unknown
> > Source)
> >     [junit] 	at
> > javax.servlet.GenericServlet.init(GenericServlet.java:256)
> >     [junit] 	at
> > com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown Source)
> >     [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> > Method)
> >     [junit] 	at
> >
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
> a:
> > 39)
> >     [junit] 	at
> >
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
> Im
> > pl.java:25)
> >     [junit] 	at
> >
> org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(S
> er
> >
> verTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214
> )
> >     [junit] 	at
> > org.apache.cactus.ServletTestCase.runTest(ServletTestCase.java:280)
> >     [junit] 	at
> > org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:251)
> >     [junit] 	at
> >
> org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCal
> le
> > r.java:156)
> >     [junit] 	at
> >
> org.apache.cactus.server.AbstractWebTestController.dispatch133_handleReq
> ue
> >
> st(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.a
> j[
> > 1k]:130)
> >     [junit] 	at
> >
> org.apache.cactus.server.AbstractWebTestController.around133_handleReque
> st
> >
> (AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[
> 1k
> > ]:1158)
> >     [junit] 	at
> >
> org.apache.cactus.server.AbstractWebTestController.handleRequest(Abstrac
> tW
> > ebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
> >     [junit] 	at
> >
> org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(Servle
> tT
> > estRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
> >     [junit] 	at
> >
> org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletT
> es
> > tRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
> >     [junit] 	at
> >
> org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirec
> to
> > r.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
> >     [junit] 	at
> >
> org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(Servlet
> Te
> > stRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
> >     [junit] 	at
> >
> org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTe
> st
> > Redirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
> >     [junit] 	at
> >
> org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirect
> or
> > .java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
> >     [junit] 	at
> > javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
> >     [junit] 	at
> > javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
> >     [junit] 	at
> >
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
> ti
> > onFilterChain.java:247)
> >     [junit] 	at
> >
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilt
> er
> > Chain.java:193)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValv
> e.
> > java:260)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:643)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> 80
> > )
> >     [junit] 	at
> > org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValv
> e.
> > java:191)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:643)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> 80
> > )
> >     [junit] 	at
> > org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardContext.invoke(StandardContext.java:241
> 5)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java
> :1
> > 80)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:643)
> >     [junit] 	at
> >
> org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherVa
> lv
> > e.java:170)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:641)
> >     [junit] 	at
> >
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java
> :1
> > 72)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:641)
> >     [junit] 	at
> >
> org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509
> )
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:641)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> 80
> > )
> >     [junit] 	at
> > org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.
> ja
> > va:174)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
> nv
> > okeNext(StandardPipeline.java:643)
> >     [junit] 	at
> >
> org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
> 80
> > )
> >     [junit] 	at
> > org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
> >     [junit] 	at
> >
> org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
> >     [junit] 	at
> >
> org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:43
> 2)
> >     [junit] 	at
> >
> org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processC
> on
> > nection(Http11Protocol.java:386)
> >     [junit] 	at
> >
> org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:53
> 4)
> >     [junit] 	at
> >
> org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool
> .j
> > ava:530)
> >     [junit] 	at java.lang.Thread.run(Thread.java:536)
> > 
> > 
> > 
> > 
> > And the init() method of my Servlet
> > ===================================
> >   public void init() throws CevexServletException
> >   {
> >     try
> >     {
> >       // get the initial context
> >       Context initCtx = new InitialContext();
> >       Context envCtx = (Context) initCtx.lookup("java:comp/env");
> > 
> >       // get the datasource
> >       ds = (DataSource) envCtx.lookup("jdbc/cevexDB");
> > 
> >       /* something is worng with the mail session, check later */
> >       //session = (Session) envCtx.lookup("mail/Session");
> > 
> >       // initialize new mail seession
> >       Properties props = new Properties();
> >       props.put("mail.smtp.host", "localhost");
> >       Session session = Session.getDefaultInstance(props, null);
> >       session.setDebug(false);
> > 
> >       // Read email strings from files
> >       String realPath = getServletContext().getRealPath("/");
> >       mailInscripcionMasc = new CevexMail(session, realPath +
> > getInitParameter("mailInscripcionMasc"));
> >       mailInscripcionFem  = new CevexMail(session, realPath +
> > getInitParameter("mailInscripcionFem"));
> >       mailInscripcionConInvitacionMasc = new CevexMail(session,
> realPath +
> > 
> > getInitParameter("mailInscripcionConInvitacionMasc"));
> >       mailInscripcionConInvitacionFem = new CevexMail(session,
> realPath +
> > 
> > getInitParameter("mailInscripcionConInvitacionFem"));
> > 
> >     } // end try
> >     .... catch statements
> >  }
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> 

RE: calling getServletContext() in a servlet - how to??

Posted by Vincent Massol <vm...@pivolis.com>.
Hi Jose,

Hmmm... very strange.

Normally I would have said:

Seen that the implementation of init() is:

    public void init(ServletConfig config) throws ServletException {
	this.config = config;
	this.init();
    }

and that line 204 of servlet-api-4 is:

    public ServletContext getServletContext() {
	return getServletConfig().getServletContext();
    }

and that:

    public ServletConfig getServletConfig() {
	return config;
    }

my conclusion is that your InscribeServlet must somehow override the
init(ServletConfig) method and it must not do a super.init(config)...

However, the stack trace says:

> javax.servlet.GenericServlet.init(GenericServlet.java:256)

and line 256 is:

254    public void init(ServletConfig config) throws ServletException {
255	this.config = config;
256	this.init();
257    }

thus it means this.config = config must have been called... So you're
not overriding GenericServlet.init(ServletConfig) or if you do you are
correctly calling super.init(config)...

hmm....

Can you test that config is not null:

 	public void testPostMethod() throws ServletException
         {
                 assertNotNull("aha config is null", config);
                 InscribeServlet inscribeServlet = new
InscribeServlet();
                 inscribeServlet.init(config);
         }

Thanks
-Vincent

> -----Original Message-----
> From: Jose Alfonso Martinez [mailto:trilock@linuxcenter.com.mx]
> Sent: 11 August 2003 19:43
> To: cactus-user@jakarta.apache.org
> Subject: calling getServletContext() in a servlet - how to??
> 
> Hello,
> 
> I am testing a servlet, just the init() method like this:
> 
> 	public void testPostMethod() throws ServletException
>         {
>                 InscribeServlet inscribeServlet = new
InscribeServlet();
>                 inscribeServlet.init(config);
>         }
> 
> I get a java.lang.NullPointerException when calling
getServletContext()...
> 
> in the init() method, my servlet calls
getServletContext().getRealPath().
> I guess there is where the exception occurs. It could also be in a
call to
> get the DataSource via InitialContext. Attached is the stack trace and
the
> init() method of my Servlet.
> 
> Does anyone knows what am I doing wrong???? thanks in advance.
> 
> Jose
> 
> 
> Stack Trace
> ============
> 
> test2:
>     [junit] Running com.cevex.servlet.TestInscribeServlet
>     [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562
sec
>     [junit] Testsuite: com.cevex.servlet.TestInscribeServlet
>     [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.562
sec
> 
>     [junit] Testcase: testPostMethod took 1.514 sec
>     [junit] 	Caused an ERROR
>     [junit] null
>     [junit] java.lang.NullPointerException
>     [junit] 	at
>
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:204)
>     [junit] 	at com.cevex.servlet.InscribeServlet.init(Unknown
> Source)
>     [junit] 	at
> javax.servlet.GenericServlet.init(GenericServlet.java:256)
>     [junit] 	at
> com.cevex.servlet.TestInscribeServlet.testPostMethod(Unknown Source)
>     [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>     [junit] 	at
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:
> 39)
>     [junit] 	at
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Im
> pl.java:25)
>     [junit] 	at
>
org.apache.cactus.internal.server.ServerTestCaseDelegate.runServerTest(S
er
>
verTestCaseDelegate.java;org/apache/cactus/util/log/LogAspect.aj[1k]:214
)
>     [junit] 	at
> org.apache.cactus.ServletTestCase.runTest(ServletTestCase.java:280)
>     [junit] 	at
> org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:251)
>     [junit] 	at
>
org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCal
le
> r.java:156)
>     [junit] 	at
>
org.apache.cactus.server.AbstractWebTestController.dispatch133_handleReq
ue
>
st(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.a
j[
> 1k]:130)
>     [junit] 	at
>
org.apache.cactus.server.AbstractWebTestController.around133_handleReque
st
>
(AbstractWebTestController.java;org/apache/cactus/util/log/LogAspect.aj[
1k
> ]:1158)
>     [junit] 	at
>
org.apache.cactus.server.AbstractWebTestController.handleRequest(Abstrac
tW
> ebTestController.java;org/apache/cactus/util/log/LogAspect.aj[1k]:101)
>     [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.dispatch160_doPost(Servle
tT
> estRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:125)
>     [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.around160_doPost(ServletT
es
> tRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
>     [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirec
to
> r.java;org/apache/cactus/util/log/LogAspect.aj[1k]:109)
>     [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.dispatch159_doGet(Servlet
Te
> stRedirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:96)
>     [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.around159_doGet(ServletTe
st
> Redirector.java;org/apache/cactus/util/log/LogAspect.aj[1k]:1158)
>     [junit] 	at
>
org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirect
or
> .java;org/apache/cactus/util/log/LogAspect.aj[1k]:92)
>     [junit] 	at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
>     [junit] 	at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>     [junit] 	at
>
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applica
ti
> onFilterChain.java:247)
>     [junit] 	at
>
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilt
er
> Chain.java:193)
>     [junit] 	at
>
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValv
e.
> java:260)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
>     [junit] 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
>     [junit] 	at
>
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValv
e.
> java:191)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
>     [junit] 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
>     [junit] 	at
>
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:241
5)
>     [junit] 	at
>
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java
:1
> 80)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
>     [junit] 	at
>
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherVa
lv
> e.java:170)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:641)
>     [junit] 	at
>
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java
:1
> 72)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:641)
>     [junit] 	at
>
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:509
)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:641)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
>     [junit] 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
>     [junit] 	at
>
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.
ja
> va:174)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.i
nv
> okeNext(StandardPipeline.java:643)
>     [junit] 	at
>
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:4
80
> )
>     [junit] 	at
> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
>     [junit] 	at
>
org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
>     [junit] 	at
>
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:43
2)
>     [junit] 	at
>
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processC
on
> nection(Http11Protocol.java:386)
>     [junit] 	at
>
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:53
4)
>     [junit] 	at
>
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool
.j
> ava:530)
>     [junit] 	at java.lang.Thread.run(Thread.java:536)
> 
> 
> 
> 
> And the init() method of my Servlet
> ===================================
>   public void init() throws CevexServletException
>   {
>     try
>     {
>       // get the initial context
>       Context initCtx = new InitialContext();
>       Context envCtx = (Context) initCtx.lookup("java:comp/env");
> 
>       // get the datasource
>       ds = (DataSource) envCtx.lookup("jdbc/cevexDB");
> 
>       /* something is worng with the mail session, check later */
>       //session = (Session) envCtx.lookup("mail/Session");
> 
>       // initialize new mail seession
>       Properties props = new Properties();
>       props.put("mail.smtp.host", "localhost");
>       Session session = Session.getDefaultInstance(props, null);
>       session.setDebug(false);
> 
>       // Read email strings from files
>       String realPath = getServletContext().getRealPath("/");
>       mailInscripcionMasc = new CevexMail(session, realPath +
> getInitParameter("mailInscripcionMasc"));
>       mailInscripcionFem  = new CevexMail(session, realPath +
> getInitParameter("mailInscripcionFem"));
>       mailInscripcionConInvitacionMasc = new CevexMail(session,
realPath +
> 
> getInitParameter("mailInscripcionConInvitacionMasc"));
>       mailInscripcionConInvitacionFem = new CevexMail(session,
realPath +
> 
> getInitParameter("mailInscripcionConInvitacionFem"));
> 
>     } // end try
>     .... catch statements
>  }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org