Sunday 1 February 2015

JAVA JSP Part6 Interview Questions and Answers

51.Can we implement an interface in a JSP? 
No

52.What is the difference between ServletContext and PageContext? 
ServletContext: Gives the information about the container
PageContext: Gives the information about the Request

53.What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()? 
request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.

54.How to pass information from JSP to included JSP? 
Using <%jsp:param> tag.

55.How is JSP include directive different from JSP include action. ?
When a JSP include directive is used, the included file's code is added into the added JSP page at page translation time, this happens before the JSP page is translated into a servlet. While if any page is included using action tag, the page's output is returned back to the added page. This happens at runtime.

56.Can we override the jspInit(), _jspService() and jspDestroy() methods? 
We can override jspinit() and jspDestroy() methods but not _jspService().

57.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.

58.What happens when a page is statically included in another JSP page? 
An include directive tells the JSP engine to include the contents of another file (HTML, JSP, etc.) in the current page. This process of including a file is also called as static include.

59.A JSP page, include.jsp, has a instance variable "int a", now this page is statically included in another JSP page, index.jsp, which has a instance variable "int a" declared. What happens when the index.jsp page is requested by the client? 
Compilation error, as two variables with same name can't be declared. This happens because, when a page is included statically, entire code of included page becomes part of the new page. at this time there are two declarations of variable 'a'. Hence compilation error.

60.Can you override jspInit() method? If yes, In which cases?
ye, we can. We do it usually when we need to initialize any members which are to be available for a servlet/JSP throughout its lifetime.
More Questions & Answers :-
Part1  Part2  Part3  Part4  Part5  Part6  Part7  Part8

No comments:

Post a Comment