This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 253

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

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

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

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

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
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: 9291
    
  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: 165

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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jspInit()
 
Similar Threads
Questions about ServletContext and ServletConfig
Thread Safe
passing ServletContext
getServletContext in jspInit
ServeltContext object