JSP Interview Questions
1 - Explain JSP life Cycle?
JSP life cycle include- Translation - convert the JSP source code file to servlet.
- Compilation - compile the servlet to .class file.
- Loading - servlet class is loaded into the container.
- Phase - In this the container manage one or more instances of this class in response to requests and other events.
- Initialization - jspInit() method is called.
- _jspService() execution - called for every request of this JSP during its life cycle.
- jspDestroy() execution - called when this JSP is destroyed.
2 - What can be done with JSP that cannot be done with Servlet and vice-a-versa?
JSP is meant for web-developers those who are good at HTML. In JSP you can create your custom tags but you cannot create tags in servlet. JSP are http protocol based but servlet can use any protocol.3 - What are the implicit objects in JSP?
JSP has pre-available object on which you can work. These objects are parsed by the JSP engine and inserted into the generated servlet. The some of the implicit objects are list below.- request
- response
- pageContext
- session
- application
- out
- config
- page
- exception
4 - How many JSP scripting elements and what are they?
There are 3 scripting language elements- Declarations
- Scriptlets
- Expressions
5 - What is EL in JSP?
EL stands for Expression Language. It provides many objects that can be used directly like param, requestScope, applicationScope, request, session etc.6 - What are JSP directives?
JSP directives are message to web container that how to translate a jsp to servlet.- page directive : <%@ page attribute=" "%>
- include directive : <%@ include file=" " %>
- taglib directive : <%@ taglib uri=" " prefix=" " %>
7 - Can I override JSP Life Cycle?
You can only override the JSP life cycle by overriding jspInit() and jspDestroy() methods. jspInit() is useful for allocating resources like database connections, network connections etc for the JSP page. jspDestroy() method is use to free allocated resource.8 - What is the difference between include directive and include action?
include directive, includes the content of the other file during the translation to a servlet. | include action, includes the response generated by executing the specified page during the request processing phase. |
It is a static import. | It is a dynamic import. |
Need to compile after changes in file. | No need to compile after changes in file as change data will show in next request. |
Syntax : <%@ include file="import.jsp'%> | Syntax : <%@ include page="import.jsp" %> |
Include Directive used file attribute to include a file. | Include Action used page attribute to include a file. |
9 - What is the include directive?
include directive is use to include the content of any JSP, HTML, text file. The include directive include the original content of the included resource at page translation time.Syntax : : <% @include file="file name"%>
10 - What are the standard actions available in JSP?
There are following standard actions available in JSP- <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.
11 - What are the attributes of page directive?
There are several page directive attributes given below- Buffer
- autoFlush
- contentType
- errorPage
- isErrorPage
- extends
- import
- info
- isThreadSafe
- language
- session
- isELIgnored
- isScriptingEnabled
12 - What is a scriptlet?
A scriptlet can contain any number of JAVA code, variable or method declarations, or expressions.Syntax : <% java code %>
13 - What are JSP declarations?
Declaration is use to define one or more variables or method that you can use later in jsp file.Syntax : <%! int i = 0; %>
14 - What is a JSP expression?
It is mainly use to print values of variable or method. Instead of writing out.print() to write data expression is used.Syntax : <%= statement %>
15 - Is it possible for one JSP to extend another java class if yes how?
Use page directive to extend any java class.<%@ page extends="your.java.class">
16 - How can one JSP Communicate with Java file.
You can communicate with any java file in JSP, you just want to use page directive to extend any java class.Syntax : <%@ page extends="your.java.class">
17 - In JSP page how can we handle runtime exception?
You can use the errorPage attribute of the page directive, when run-time exceptions occur page directive will automatically forwarded to an error processing page. For example:<%@ page errorPage="error.jsp" %> redirects the browser to the error.jsp page.
18 - What is the need of tag library?
Tag library in jsp is called JSTL [Java Strandard Tag Library] , it is a collection of jsp tags which encapsulate core functionality common to many jsp applications. It is introduce in jsp to avoid the java code inside the jsp file and follow MVC pattern, as jsp meant for view in this pattern and view part should not contain java code. That why JSTL use tags which are easy for web developers to write in jsp file.19 - What is JSESSIONID in Java? When JSESSIONID gets created?
JSESSIONID is a cookie generated by servlet container. JSESSIONID is used for session management in J2EE web application for http protocol. As Http is a connectionless protocol, there is no way for the server to identify from which client the request is coming, so to identify client server maintain the JSESSIONID for each client.20 - How do you define application wide error page in JSP?
There are two to define error page in jsp application- Page wise error - It is define on each page of jsp, if any runtime unhandled exception occur it shows the error page.
- Default error page - It is shown if any unhandled exception occur the default error page will display, no page specific error.