JSP interview questions and answers

This page contains the most frequently asking JSP interview questions and answers.This will helps for freshers as well as experienced.

1. What are the advantages of JSP over Servlet?
                              OR
By using Servlet we can do UI implementation, then why to use JSP in any of the applications?

A:
  • JSP is a server side technology to make content generation a simple appear. 
  • The advantages of JSP is that they are document-centric. Servlet on the other hand, look and act like programs.
  • A Java Server Page(JSP) can contain Java program fragments that instantiate and execute java classes, but these occurs inside an HTML template file and are primarily used to generate dynamic content.
  • The power of JSP is that it is server-based and provides a framework for web application development.

2. What is the life cycle of JSP?
                       OR
Can explain about JSP life cycle?

A:
  • When a request is mapped to a JSP page for the first time, It translates the JSP page into a servlet class and compiles it.
  • It is that servlet that services the client requests.
  • A JSP page has seven phases in its life cycle, that sequence as like,
  1. Translation (From JSP file to Servlet class)
  2. Compilation (Compiles the translated Servlet class)
  3. Loading the class (.class file loads into JVM)
  4. Instantiating the class (Generates instance of the class)
  5. jspInit() invocation (Invocates the JSP initialization method)
  6. _jspService() invocation (Invocates the respective JSP service method)
  7. jspDestroy() invocation (Invocates final JSP destroy method)

3. What is the jspInit() method and what it will do in the process of JSP life cycle?

A: 
  • The jspInit() method of the javax.servlet.jsp.JspPage interface is similar to the init() method of servlet.
  • jspInit() method invoked by the container only once when a JSP page 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.
4. What is the _jspService() method?

A:
  • The _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 as its arguments.
  • A page author can not override this method, as its implementation is provided by the  container.
5. What is the jspDestroy() method?
A:
  • 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.
6. What all JSP life cycle methods can I override?

A:
  • You can not 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 fourth for the JSP page.
  • It is good programming practice to free any allocated resources within jspDestroy().
7. How can I override the jspInit() and jspDestroy() methods within a JSP page?

A:
  • The jspInit() and jspDestroy() methods are each exxecuted just once during the life cycle of a JSP page and are typically declared as JSP declarations.
<%!
    public void jspInit(){
        ....
    }
%>
<%!
    public void jspDestroy(){
        ....
    }
%>

8. What are implicit objects in JSP? (***)

A:
  • Implicit objects in JSP are the Java Object 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 variable. Hence, They are automatically implicit objects.
  • The implicit objects available in JSP are,
  1. request
  2. response
  3. page
  4. pagecontext
  5. session
  6. application
  7. out
  8. config
  9. exception
  • The implicit objects are parsed by the container and interested into the generated servlet code.
  • They are available only within the jspService() method and not in any declaration.
9. What are the different types of JSP tags?

A:
                                                                                 |-->Page Directive
                                                                                 |-->Include Directive
                                               |-->Directive Tags-->|--> Taglib Directive
                                               |                                 |-----------------------
                                               |
                                               |
                                               |                                             |-->Forward Action
           |-->Presentation Tags  |                                             |-->Include Action
           |                                   |                                             |-->UseBean Action
JSP-->|-->Standard Tags---->|-->Standard Action Tags-->|---------------------
           |                                   |
           |-->Custom Tags         |
                                               |
                                               |
                                               |                               |-->Scriplet Tag
                                               |                               |-->Expression Tag
                                               |-->Scriplet Tags-->|-->Declaration Tag
                                                                               |---------------------


10. What are JSP directives?

A:
  • 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 <%@ ...... %> 
  • Ex. page directive, include directive, etc.
11. What is page directive?

A:
  • A page directive is to inform the JSP engine about the header 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.
  • The syntax of the include directive is, <%@ page attribute="value" %>
  • Ex, <%@ include file="header.jsp" %>
12. What are the attributes of page directive?

A:
  • There are thirteen attributes defined for a page directive of which the important attributes are,
  • 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 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.
13. What is the include directive?

A: 
  • The include directive is used to statically insert the content of a resource into the current JSP.
  • This enables a user to reuse the code without duplicating it, and includes the content 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.
14. What are the JSP standard actions?

A:
  • 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 Java Bean, to forward a request to a new page, to generate a browser-specific code.
  • Ex, include, forward, useBean, etc. 
15.What are the standard actions available in JSP?

A:
  • <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, where as 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 property of 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: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 JavaBean in the current JSP page.
16: What is the <jsp:useBean> standard action?

A:
  • The <jsp:useBean> standard action is used to locate an existing JavaBean or to create a JavaBean if it does not exist. 
  • It has attributes to identity the object instance, to specify the lifetime of the bean, and to specify the fully qualified classpath and type.
17: What are the scopes available in <jsp:useBean>?

A:
  • page scope: It specifies that the object will be available for the entire JSP page but not outside the page.
  • request scope: It specifies that the object will be associated with a particular request and exist as long as the object will be available.
  • application scope: It specifies that the object will be available throughout the entire web application but not outside the application.
  • session scope: It specifies that the object will be available throughout the session with a particular client.
18: What is the <jsp:forward> standard action?

A:
  • The <jsp:forward> standard action forwards a response from a servlet or JSP page to another page.
  • The execution of the current page is stopped and control is transferred to the forwarded page.
  • The syntax of the <jsp:forward> standard action is: <jsp:forward page="/targetPage"/>. Here, targetPage is the page to be included in the current JSP.
19: What is the <jsp:include> standard action?

A:
  • The <jsp:include> standard action enables the current JSP page to include a static or a dynamic resource at runtime.
  • In contrast to the include directive, the include action is used for resources that change frequently.
  • The resource to be included must be in the same context. The syntax of the <jsp:include> standard action is, <jsp:include page="targetPage" flush="true">.

20: What is the difference between include directive and include action?
A:
Include Directive:
1. The include directive, includes the content of the specified file during the translation of the specified file during the translation phase when the page is converted to a servlet.
2. The includes directive is used to statically insert the contents of a resource into the current JSP.
3. Use the include directive if the file changes rarely. It's the fastest mechanism.

Include Action:
1. The include action, includes the response generated by executing the specified page during the request processing phase when the page is requested by a user.
2. The include standard action enables the current JSP page to include a static or a dynamic resource at runtime.
3. Use the include action only for content that changes often, and if which page to include cannot be decided until the mail page is requested.

21: Difference between pageContext.include and jsp:include?

A:
  • The <jsp:include> standard action and the pageContext.include() method are both used to include resources at runtime.
  • However, the pageContext.include() method always flushes the output of the current page before including the other components, whereas <jsp:include> flushes the  output of the current page only if the value of flush is explicitly set to true as <jsp:include page="/index.jsp" flush="true"/>
22: What is the jsp:setProperty action?

A:
  • You use jsp:setProperty to give values to properties of beans that have been referenced earlier.
  • You can do this in two contexts. First, you can use jsp:setProperty after, but outside of, a jsp:useBean element,
<jsp:useBean id="myName" .../>
......
<jsp:setProperty name="myName" property="myProperty" ..../>
  • In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.
  • A second context in whch jsp:setProperty can appear is inside the body of a jsp:useBean element,
<jsp:useBean id="myName" .../>
......
<jsp:setProperty name="myName" property="myProperty" ..../>

</jsp:useBean>
  • The jsp:setProperty is executed only if a new object was instantiated, not if an existing one was found.
23: What is the jsp:getProperty action?

A:
  • The <jsp:getProperty> action is used to access the properties of a bean that was set using the <jsp:setProperty> action. The container converts the property to a String as,
  • If it is an object, it uses the toString() method to convert it to a String.
  • If it is a primitive, it converts it directly to a String using the valueOf() method of the corresponding wrapper class.
  • The syntax of the <jsp:getProperty> method is <jsp:getProperty name="Name" property="Property">
  • Here, name is the id of the bean from which the property was set. The property attribute is the property to get. A user must create or locate a bean using the <jsp:useBean> action before using the <jsp:getProperty> action.
24: What is the <jsp:param> standard action?

A:
  • The <jsp:param> standard action is used with <jsp:include> or <jsp:forward> to pass parameter names and values to the target resource.
  • The syntax of the <jsp:param> standard action is, <jsp:param name="paramName" value="paramValue"/>
25: What is the jsp:plugin action?
A:
  • This action lets you insert the browser specific OBJECT or EMBED element needed to specify that the browser run an applet using the java plugin.
26: What are scripting elements? (***)
A:
  • JSP scripting elements let you insert java code into the servlet that will ben generated from the current JSP page.
  • 1. Expressions of the form <%= expression %> that are evaluated and inserted into the output.
  • 2. Scriplets of the form <% code %> that are inserted into the servlet's service method.
  • 3. Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.
27: What is a scriplets?
A:
  • A scriplet contains Java code that is executed every time a JSP is invoked.
  • When a JSP is translated to a servlet, the scriplets code goes into the service() method.
  • Method and variables written in scriplets are local to the service() method.
  • A scriplet is written between the <%  and  %> tags and is executed by the container at request processing time.
28: What are JSP declarations?
A:
  • As the name implies, JSP declarations are used to declare class variables and methods in JSP page.
  • They are initialized when the class is initialized.
  • Anything defined in a declaration is available for the whole JSP page.
  • A declaration block is enclosed between the <%! and %> tags.
  • A declaration is not included in the service() method when a JSP is translated to a servlet.
29: What is a JSP expression?
A:
  • A JSP expression is used to written an output without using the out.print statement. 
  • It can be said as a shorthand representation for scriplets.
  • An expression is written between the <%= and %> tags. 
  • It is not required to end the expression with a semicolon, as it implicitly adds a semicolon to all the expression within the expression tags.
30: How is scripting disabled?
A:
  • Scripting is disabled by setting the scripting invalid element of the deployment descriptor to true.
  • It is a sub-element of jsp-property-group.
  • Its valid values are true and false.

<jsp-property-group>
   <url-pattern>*.jsp</url-pattern>
   <scripting-invalid>true</scripting-invalid>
</jsp-property-group>

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example