File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes basic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "basic question" Watch "basic question" New topic
Author

basic question

Kumar Garlapati
Ranch Hand

Joined: Feb 04, 2008
Posts: 38
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

Joined: Mar 06, 2001
Posts: 13459

looks good to me, although there is possibly the ability to reorganise the first three without causing issues.
Kumar Garlapati
Ranch Hand

Joined: Feb 04, 2008
Posts: 38


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
Marshal

Joined: Mar 22, 2005
Posts: 39529
    
  27

Both objects exist when the init method is called. You'd access them using "sc" and "sc.getServletContext()".


Ping & DNS - updated with new look and Ping home screen widget
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kumar Garlapati
Ranch Hand

Joined: Feb 04, 2008
Posts: 38


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
Marshal

Joined: Mar 22, 2005
Posts: 39529
    
  27
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

Joined: Feb 04, 2008
Posts: 38
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.
 
 
subject: basic question
 
Similar Threads
ServletContext related
ServletContext within ServletConfig
ServeltContext object
Servlet Context vs Application Context
passing ServletContext