Wednesday, March 17, 2010

JSP FAQs - 2

 

11.  What are the lifecycle phases of a JSP?

Life cycle of JSP contains the following phases:

1.    Page translation: -converting from .jsp file to .java file
2.    Page compilation: converting .java to .class file
3.    Page loading : This class file is loaded.
4.    Create an instance :- Instance of servlet is created
5.    jspInit() method is called
6.    _jspService() is called to handle service calls
7.    jspDestroy() is called to destroy it when the servlet is not required.

  
12.  Explain the life-cycle mehtods in JSP?

The jspInit()- The container calls the jspInit() to initialize te servlet instance.It is called before any other method, and is called only once for a servlet instance.
The _jspservice()- The container calls the _jspservice() for each request, passing it the request and the response objects.
The jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance
.

13. Difference between _jspService() and other life cycle methods.

JSP contains three life cycle methods namely jspInit( ), _jspService() and jspDestroy(). In these, jspInit() and jspDestroy() can be overridden and we cannot override _jspService().

Webcontainer always generate _jspService() method with JSP content. If we are writing _jspService() method , then generated servlet contains 2 _jspService() methods which will cause compile time error.

To show this difference _jspService() method is prefixed with '_' by the JSP container and the other two methods jspInit() and jspDestroy() has no special prefixes.

14 What is the jspInit() method?
The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of servlets. This method is invoked by the container only once when a JSP page is initialized. It can be overridden by a page author to initialize resources such as database and network connections, and to allow a JSP page to read persistent configuration data.

15. What is the _jspService() method?
SThe _jspService() method of the javax.servlet.jsp.HttpJspPage interface is invoked every time a new request comes to a JSP page. This method takes the HttpServletRequest and HttpServletResponse objects as its arguments. A page author cannot override this method, as its implementation is provided by the container.

16.  What is the jspDestroy() method?
The jspDestroy() method of the javax.servlet.jsp.JspPage interface is invoked by the container when a JSP page is about to be destroyed. This method is similar to the destroy() method of servlets. It can be overridden by a page author to perform any cleanup operation such as closing a database connection.

17.  What JSP lifecycle methods can I override?
We can override jspInit() and jspDestroy() methods but we cannot override _jspService() method.

18.  How can I override the jspInit() and jspDestroy() methods within a JSP page?
By using JSP declation tag

<%!
        public void jspInit() {
                . . .
        }
%>
<%!    
        public void jspDestroy() {
                . . .  
        }
%>

19 . Explain about translation and execution of Java Server pages?

A java server page is executed within a Java container. A Java container converts a Java file into a servlet. Conversion happens only once when the application is deployed onto the web server. During the process of compilation Java compiler checks for modifications if any modifications are present it would modify and then execute it.

20 . Why is _jspService() method starting with an '_' while other life cycle methods do not?

_jspService() method will be written by the container hence any methods which are not to be overridden by the end user are typically written starting with an '_'. This is the reason why we don't override _jspService() method in any JSP page.

 

No comments:

Post a Comment