jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Vincent Massol" <vmas...@pivolis.com>
Subject RE: calling getServletContext() in a servlet - how to??
Date Tue, 12 Aug 2003 19:32:13 GMT


> -----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



Mime
View raw message