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()

 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to ask whether jspInit() is called from within init() or init(ServletConfig) ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jspInit is called by the container
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure Satou?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishnu is right.
HttpJspBase extends HttpServlet and jspInit() is called in init(ServletConfig ).
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic