Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jspInit()

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chinmaya Chowdary
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes.JspPage is an interface.Thanks Salil.
 
Chinmaya Chowdary
Ranch Hand
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay , I got your point :-)

Thanks everyone!
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic