JSP Interview Questions and Answers

What is JSP?

JSP refers to the java server pages. Before starting discussion on jsp let us discuss the some problems of servlet.

See more at: JSP Overview.

What are the JSP lifecycle phases?

A JSP page is converted into servlet before service requests. JSP Lifecycle consists of following phases:
JSP Page Translation:
The container validates the JSP page and parse it to generate the servlet file.
JSP Page Compilation:
The generated servlet file is compiled into a servlet class.
Class Loading:
Container loads the generated servlet class into memory.
Instantiation:
Container invokes the no argument constructor of generated servlet class to load it into memory and instantiate it.
Initialization:
After creating the instance, jspInit() method is called to initialize it.
Request Processing:
The _jspService() is called by web container every time when a request is come for the jsp.
Syntax: public void _jspService() throws ServletException,IOException
Note: In this method underscore (_) represents that implementation of this method is provided by auto generated servlet and it can’t be overridden by developer.
Destroy:
The jspDestroy() method is called by web container to unload JSP from memory.

What are JSP lifecycle methods?

1. jspInit(): It is used to perform initialization process. This method is called by the web container only once when first request comes.
Syntax:public void jspInit()
2. _jspService(): This method is called by web container every time when a request is come for the jsp.
Syntax: public void _jspService() throws ServletException,IOException
Note: In this method underscore (_) represents that implementation of this method is provided by auto generated servlet and it can’t be overridden by developer.
3. jspDestroy(): It is used to perform cleanup process before destroying jsp page. This method is called by the web container only once.
Syntax: public void jspDestroy()

Which JSP lifecycle methods can be overridden?

The jspInit() and jspDestroy() methods can be overridden but we cannot override _jspService() method because implementation of this method is provided by auto generated servlet.

What is a sciptlet in JSP?

Scriptlet tag contains the any no. of java code statements. It may contain variables declaration or valid expressions. When JSP is translated into servlet, java code written in the scriplet tag is moves to the _jspService() method by web container.

See more at: JSP sciptlet tag.

What are JSP declarations?

Declaration tag is used to declare one or more variables or methods at class level. Variables and methods declare in declaration tag are initialized at the time of JSP initialization. These variables and methods are kept outside of the service method by web container to make them class level.

See more at: JSP declarations tag.

What are JSP expressions?

Expression tag evaluates the scripting language expression, converts the result into a string. This result is send back to the web user through response object. It writes the result into output stream of the response object.

See more at: JSP expressions tag.

What are JSP comments?

JSP comment tag is used to marks jsp code as comment i.e. the code inside comment tag will be ignored by web container.

See more at: JSP comment tag.

What are JSP Directives?

JSP directives provides the instructions and directions to the web container about how to control the processing JSP page.

See more at: JSP Directives.

What are the types of directive tags?

Types of JSP directives.
1. page directive.
2. include directive.
3. taglib directive.

See more at: JSP Directives.

What is a page directive?

Page directive is used to provide the instructions to the web container that are specific to the current JSP page. It defines the page specific attributes like scripting language, error page etc.

See more at: JSP page directive.

What is a include directive?

JSP include directive is used to merge or include the content of other resource into the current JSP page during translation phase. Other resource can be jsp, html or a text file. It provides the facility of code reusability.

See more at: JSP include directive

What is a taglib directive?

JSP taglib directive is used in case of custom tags. It specifies the tag library used for custom tags. We will learn more about taglib directive in custom tag section.
See more at: JSP taglib directive.

How to disable session in JSP?

<%@ page session="false" %>

See more at: JSP Session Attribute

Which directive is used in jsp custom tag?

JSP taglib directive.

How can a thread safe JSP page be implemented?

It can be achieved by implementing the SingleThreadModel Interface. Add <%@page isThreadSafe=”false” %> directive in the JSP page.

See more at: JSP isThreadSafe Attribute

What are JSP implicit objects?

JSP implicit objects are 9 and given below:
1. out (JspWriter).
2. request (HttpServletRequest).
3. response (HttpServletResponse).
4. config (ServletConfig).
5. application (ServletContext).
6. session (HttpSession).
7. pageContext (PageContext).
8. page (Object).
9. exception (Throwable).

See more at: JSP implicit objects

Can we use JSP implicit objects in a method defined in JSP Declaration?

No we can’t use JSP implicit objects in a method defined in JSP Declaration because JSP implicit objects are local to service method JSP declarations code goes outside the service method and used to create class level variables and methods and hence cannot use JSP implicit objects.

Which implicit object is not available in normal JSP pages?

JSP exception implicit object.
JSP exception object is an instance of javax.servlet.jsp.JspException. This object is used in exception handling to print the error message. But is only be used on that jsp page on which isErrorPage attribute is true

See more at: JSP exception implicit object

Explain JSP Action Tags?

JSP specification provides the action tags to control the behaviour of the servlet engine, to control page flow, to dynamically insert a file, to reuse JavaBeans components etc. jsp: is used as prefix.
Syntax:

See more at: JSP Action Tags

What is difference between include directive and jsp:include action?

include directive include action
  1. 1. In case of include directive contents are included at the translation time.
  2. 2. If the content of included file is changed than changes will not reflect.
  3. 3. Not use RequestDispatcher to include the content.
  4. 4. Can’t pass parameters.
  5. 5. Syntax: <%@ include file=”URL of another resource” %>
  1. 1. In case of jsp:include contents are included at the runtime.
  2. 2. If the content of included file is changed than changes will reflect.
  3. 3. The jsp:include action include the contents using RequestDispatcher.
  4. 4. Can pass parameters using jsp:param action tag.
  5. 5. Syntax: <jsp:include page=”URL of another resource” />

See more at: jsp:include action tag

How to disable caching on back button of the browser?

<%  
response.setHeader("Cache-Control","no-store");   
response.setHeader("Pragma","no-cache");   
response.setHeader ("Expires", "0"); //prevents caching at the proxy server  
%>

What is JSP Expression Language?

JSP Expression Language provides the facility to access the properties of java bean components or other implicit object. It is introduced in the JSP version 2.0. We can write both arithmetic and logical expressions using JSP Expression Language.

See more at: JSP Expression Language

What is JSP Standard Tag Library?

JSTL stands for JSP Standard Tag Library. It provides the no. of tags used for iteration, condition checking, manipulating XML documents etc.

See more at: JSTL (JSP Standard Tag Library)

What are the types of JSTL tags?

Types of JSTL tags:
1. JSTL Core Tags
2. JSTL Formatting tags
3. JSTL SQL tags
4. JSTL XML tags
5. JSTL Functions

See more at: JSTL (JSP Standard Tag Library)

What is JSP Custom Tag?

Custom tags are the user defined tags. These tags are mainly used for code re-usability.

See more at: JSP Custom tags.

How to handle exceptions in JSP?

Ways of exception handling in JSP:
1. Using page directive (errorPage and isErrorPage attributes).
2. Using attribute of web.xml.

See more at: Exception handling in JSP

How is scripting disabled?

Scripting is disabled by setting the scripting-invalid element of the deployment descriptor to true.

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

How to extend java class in jsp?

We can extend java class in jsp using <%@ include page extends="classname" %> directice.