This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic question

 
Kumar Garlapati
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

This is my first mail in JavaRanch, Hope this continues....

I have a basic question regarding Servletcontext and servletconfig object

Constructor
ServletContext
ServletConfig
init() method
service method

Could any one please arrange the order in which container calls/creates them. I think my order is correct, want a confirmation, bit confuuuused . Please clarify.

Regards,
Ravi Kumar
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks good to me, although there is possibly the ability to reorganise the first three without causing issues.
 
Kumar Garlapati
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this is throwing
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
at com.putech.SampleServlet.init(SampleServlet.java:17)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)

When exactly ServletContext object gets created ???
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Both objects exist when the init method is called. You'd access them using "sc" and "sc.getServletContext()".
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kumar Garlapati:

Constructor
ServletContext
ServletConfig
init() method
service method


The context is not part of the servlet.
It is the namespace in which the servlet lives and is set up before any servlets come into play.
[ February 04, 2008: Message edited by: Ben Souther ]
 
Kumar Garlapati
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why the above line is not working from init(Servletconfig sc) method. it supposed to work fine right ???
atleast they should display null values if they dont have any.

why getServletContext() doesn't work as is ???
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work because the method doesn't do what the javadoc of GenericServlet.init(ServletConfig) says it should do - call super.init(ServletConfig).

By the way, a single question mark is sufficient at the end of a question.
[ February 04, 2008: Message edited by: Ulf Dittmer ]
 
Kumar Garlapati
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for multiple '?' marks.

Thank You, I came to the same conclusion and about to post that.

Looking into the code of GenericServlet.java, I understood that until servlet gets ServlteConfig object, we can't access ServletContext object.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic