Pr.Pg Next Pg

JSP Interview Questions and Answers Part 8

 

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

 

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

 

73.What JSP lifecycle methods can I override?

  • You cannot override the _jspService() method within a JSP page.

  • You can however, override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page.

  • It is good programming practice to free any allocated resources within jspDestroy().

 

74. What are implicit objects in JSP?

  • Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page.

  • These objects need not be declared or instantiated by the JSP author.

  • They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.

  • The implicit objects available in JSP are as follows:

  • request

  • response

  • pageContext

  • session

  • application

  • out

  • config

  • page

  • exception

  • The implicit objects are parsed by the container and inserted into the generated servlet code. They are available only within the jspService method and not in any declaration.

 

75.What are JSP directives?

  • JSP directives are messages for the JSP engine. i.e., JSP directives serve as a message from a JSP page to the JSP container and control the processing of the entire page

  • They are used to set global values such as a class declaration, method implementation, output content type, etc.

  • They do not produce any output to the client.

  • Directives are always enclosed within <%@ .. %> tag.

  • Ex: page directive, include directive, etc.

 

76. What is page directive?

  • A page directive is to inform the JSP engine about the headers or facilities that page should get from the environment.

  • Typically, the page directive is found at the top of almost all of our JSP pages.

  • There can be any number of page directives within a JSP page (although the attribute value pair must be unique).

  • The syntax of the include directive is: <%@ page attribute="value">

  • Example:<%@ include file="header.jsp" %>

 

77. What are the attributes of page directive?

  • There are thirteen attributes defined for a page directive of which the important attributes are as follows:

  • import: It specifies the packages that are to be imported.

  • session: It specifies whether a session data is available to the JSP page.

  • contentType: It allows a user to set the content-type for a page.

  • isELIgnored: It specifies whether the EL expressions are ignored when a JSP is translated to a servlet.

 

78. What is the include directive?

  • There are thirteen attributes defined for a page directive of which the important attributes are as follows:

  • The include directive is used to statically insert the contents of a resource into the current JSP.

  • This enables a user to reuse the code without duplicating it, and includes the contents of the specified file at the translation time.

  • The syntax of the include directive is as follows:
    <%@ include file = "FileName" %>

  • This directive has only one attribute called file that specifies the name of the file to be included.

 

79. What are the JSP standard actions?

  • The JSP standard actions affect the overall runtime behavior of a JSP page and also the response sent back to the client.

  • They can be used to include a file at the request time, to find or instantiate a JavaBean, to forward a request to a new page, to generate a browser-specific code, etc.

  • Ex: include, forward, useBean,etc. object

 

80.What are the standard actions available in JSP?

  • The standard actions available in JSP are as follows:

  • <jsp:include>: It includes a response from a servlet or a JSP page into the current page. It differs from an include directive in that it includes a resource at request processing time, whereas the include directive includes a resource at translation time.

  • <jsp:forward>: It forwards a response from a servlet or a JSP page to another page.

  • <jsp:useBean>: It makes a JavaBean available to a page and instantiates the bean.

  • <jsp:setProperty>: It sets the properties for a JavaBean.

  • <jsp:getProperty>: It gets the value of a property from a JavaBean component and adds it to the response.

  • <jsp:param>: It is used in conjunction with <jsp:forward>;, <jsp:, or plugin>; to add a parameter to a request. These parameters are provided using the name-value pairs.

  • <jsp:plugin>: It is used to include a Java applet or a JavaBean in the current JSP page.


 

Pr.Pg border                                              Next Pg