Pr.Pg Next Pg

JSP Interview Questions and Answers Part 6


51.Can we override the jspInit(), _jspService() and jspDestroy() methods?

  • We can override jspinit() and jspDestroy() methods but not _jspService().


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


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


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


55.Can you override jspInit() method? If yes, In which cases?

  • Yes, 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.


56.What is the difference between directive include and jsp include?

  • <%@ include> : Used to include static resources during translation time. : Used to include dynamic content or static content during runtime.


57.What is the difference between RequestDispatcher and sendRedirect?

  • RequestDispatcher: server-side redirect with request and response objects. sendRedirect : Client-side redirect with new request and response objects.


58.How does JSP handle runtime exceptions?

  • Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.


59.How can my application get to know when a HttpSession is removed?

  • Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound() method.

  • Create an instance of that class and put that instance in HttpSession


60.What Class.forName will do while loading drivers?

  • It is used to create an instance of a driver and register it with the DriverManager.

  • When you have loaded a driver, it is available for making a connection with a DBMS.

Pr.Pg border                                              Next Pg