aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jspInit() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jspInit()" Watch "jspInit()" New topic
Author

jspInit()

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
There is a question in HFSJ that mentions

jspInit() has access to ServletContext.

From what i know , ServletConfig Object is passed on to the init() and jspInit() methods.
And this ServletConfig Object has reference to ServletContext Object
So I thought the above statement is wrong because it has indirect access....

How come statement is true ?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Nabila ,

ServletConfig Object is passed on to the init() and jspInit() methods.


ServletConfig is passed to init() method of servlet as an argument but not to jspInit(). jspInit() is a method which does not take any argument.

In jspInit method, we need to use function getServletConfig() to obtain ServletConfig object. I believe, by the word access they might be referring to direct or indirect access that can be obtained without using composition (HAS-A) relationship with other object.

Regards
Salil Verma
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Nabila.
From what i know , ServletConfig Object is passed on to the init() and jspInit() methods.

After loading, instatiation, container calls servlet's init(ServletConfig sc) method, which calls jspInit() method(of JspPage). Here getServletContext() is available to it by GenericServlet, through which we can get ServletContext Object. Container will not pass ServletConfig object to jspInit() method, which has no arguments.
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

Hi Nabila.

From what i know , ServletConfig Object is passed on to the init() and jspInit() methods.

After loading, instatiation, container calls servlet's init(ServletConfig sc) method, which calls jspInit() method(of JspPage). Here getServletContext() is available to it by GenericServlet, through which we can get ServletContext Object. Container will not pass ServletConfig object to jspInit() method, which has no arguments.


Doesn't JspPage implement javax.servlet.Servlet interface?Does javax.servlet.Servlet has getServletContext() method?I don't know.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hey Bindu,

Doesn't JspPage implement javax.servlet.Servlet interface?Does javax.servlet.Servlet has getServletContext() method?I don't know.


javax.servlet.jsp.JspPage is an interface not a class so it does not implement any method. javax.servlet.Servlet interface does not have any getServletContext method in it.

I believe, Chinmaya's explaination make some sense..
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

Oh yes.JspPage is an interface.Thanks Salil.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Bindu.
There is a method getServletContext() present in GenericServlet class. GenericServlet class can get that method by implementing ServletConfig interface. ServletConfig interface has a method getServletContext(). I think it is the root.
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

Thanks Chinmaya,you have a very nice name I must say.
Well.I could not fully understand this

Chinmaya Chowdary wrote:Here getServletContext() is available to it by GenericServlet, through which we can get ServletContext Object.


Can getServletContext() method itself be used in jspInit() method?Howcome?
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Bindu. Thankyou.
Can getServletContext() method itself be used in jspInit() method?Howcome?


During jsp page translation i.e converting from test.jsp to test.java, jsp translator extends test.java with HttpJspBase class(basically it is an abstract class, tomcat will make it concrete class). HttpJspBase class, has super classes HttpServlet, GenericServlet classes, which also implements
javax.servlet.jsp.HttpJspPage, javax.servlet.jsp.JspPage, java.io.Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig
interfaces.

Container will implement all these and provide concrete HttpJspBase class. Obviously jspInit() can access to getServletContext() method.

To be clear see all relative api's and also see, the generated source file(that is present in tomcat/work/.. folder).
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Why does it matter whether the getServletContext method is available to the JSP or not. The getServletConfig method is available in the JspPage interface. The statement never says that the JSP page has DIRECT access to the ServletContext

jspInit() has access to ServletContext.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
okay , I got your point :-)

Thanks everyone!
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

Yes,getServletContext() is available in jspInit.
As the generated servlet extends org.apache.jasper.runtime.HttpJspBase and implements org.apache.jasper.runtime.JspSourceDependent.
I was looking at javax.servlet.jsp.HttpJspBase and was confused.

 
wood burning stoves
 
subject: jspInit()