This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates 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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jspInit()" Watch "jspInit()" New topic
Author

jspInit()

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
I want to ask whether jspInit() is called from within init() or init(ServletConfig) ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

jspInit is called by the container


[My Blog]
All roads lead to JavaRanch
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Are you sure Satou?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

JSP spec, JSP.11.2.1 API Contracts

- Methods the JSP Container Invokes : void jspInit()
- Comments: Method is optionally defined in JSP page. Method is invoked when the JSP page is initialized. When method is called all the methods in servlet, including getServletConfig are available

Where else do you think it would be called ?
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
But JSP is converted ultimately into a servlet and then init() method of this servlet is called from where jspInit() is called.........I think
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

I want to ask whether jspInit() is called from within init() or init(ServletConfig) ?


If you are working in servlets and like to override the init() method you should always override the no-args init() method. Since init(ServletConfig) will directly be called by the container. If you happen to override init(ServletConfig) then you should always make a call to super.init(ServletConfig).

Yes jspInit() method will be called by the container as this page's life as a servlet. It is called from servlet's init(ServletConfig)[by the container by default], so by the time this method runs there is a servletconfig and sevletcontext available to the servlet generated from jsp page.

If you have HFSJ book check page: 308


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Vishnu is right.
HttpJspBase extends HttpServlet and jspInit() is called in init(ServletConfig ).
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Satou kurinosuke has explained with respect to apache tomcat


servlet generated from a JspOne.jsp page
public final class JspOne_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {


Let me explain the general scenario

When you use a jsp page for the first time after it has been deployed in a server it will be translated into java source code and compiled in to servlet class file by the container and that servlet will implement HttpJspPage.

HttpJspPage interface defines the life cycle method _jspService(HttpServletRequest request, HttpServletResponse response) and extends Servlet interface and JspPage interface in which life cycle methods jspInit() & jspDestroy() are defined. JspPage interface extends Servlet interface which defines init(ServletConfig config) method.
 
Don't get me started about those stupid light bulbs.
 
subject: jspInit()