Here is most frequently asked jsp interview questions with answers. Following jsp interview questions are deeply covered for both freshers and experience on interview basis.
1.What is JSP and why do we need it?
Java Server page is like HTML page connecting to the servlet in back-end and contains some java code inside the scriptlet tag. JSP pages are mainly used to create dynamic web pages easily. It is a lightweight code.
2. Difference between HTML and JSP Comment?
- HTML comment tag is <!– Html comment –> and JSP comment tag is <%– jsp comment –%>
- HTML comment are called output comment whereas JSP comment are called hide comment.
- When we view source file, HTML comments are displayed whereas JSP comments are not displayed.
3. Difference between JSP and servlet?
- Java server page is HTML based code and servlet is java code.
- Servlet is faster than JSP.
- Servlet accept all request but JSP accepts only HTTP request.
4. Explain JSP Lifecycle?
The JSP pages follow the following phases:
1) Request from the JSP (index.jsp) file is loaded.
2) Translation phase
The index.jsp page is converted into index_jsp.java file.
3) Compilation phase
The index_jsp.java is converted into index_jsp.class file.
4) The index_jsp.class is loaded and the output response is displayed on the browser page.
5. Explain the types of directives in JSP?
There are three types of directives in JSP. They are,
It defines the page dependent attribute like scripting language,
error page, etc and it’s applied to the whole jsp page.
<%@ page attribute= “value”%>
It adds file resources into the JSP page. File attribute is used to include resource during translation time.
<%@ include file=“resourceName” %>
The JSP taglib directive is used to define a tag library which defines many tags and are used in the JSP pages by JSP standard tag libraries.
<%@ taglib uri=“uriofthetaglibrary”
6. What is Scriptlet, Expression, and Declaration in JSP?
The scriptlet, expression, and declaration are used as JSP tags.
They are used to place java code inside the JSP pages.
Syntax: <% java code %>
It is mainly used to get the values of variables or methods.
Syntax: <%=expression %>
It is used to define methods in jsp pages.
Syntax: <%! Declaration %>
7. Is it possible to use java comment in JSP?
Yes, java comment (single line comment, multiline comment) can be used in JSP pages but only inside the scriptlet tag.
8. What are JSP implicit objects?
JSP implicit object is the Java Objects and they are predefined variables used directly in JSP pages. There are 9 JSP implicit objects. They are out, request, response, session, page, application, config, page context, exception.
9. Use of page implicit object?
It is an implicit object of the type Object class. Page implicit object is used to indicate the current JSP page.
10. Use of application implicit object?
It is an implicit object of type Servlet Context. It is used to get initialization the parameter to use in all JSP pages.
11. Which implicit object is not available in normal JSP pages?
Exception implicit object is not available in normal JSP pages. It is used in error pages to catch the exception thrown in the page.
12. What are the benefits of the Page Context implicit object?
- It is only implicit object common in both JSP implicit objects and JSP EL objects.
- It is used to set or get the attribute from other scopes.
13. What is JSP Expression Language and what is it benefits?
JSP Expression Language is used to get parameter values from java bean class. For using this JSP EL it should be enabled in page directives in JSP page. Its benefit is code reduction.
14. What are JSP EL implicit objects and how it’s different from JSP implicit Objects?
The JSP EL implicit objects are pageScope, sessionScope, applicationScope, cookie, pageContext, requestScope, param, paramValues, initParam, header, headerValue.
15. How do we print “<br> creates a new line in HTML” in JSP?
out.print(“<br> creates a new line in HTML”)
16. How to ignore the EL expression evaluation in a JSP?
To ignore EL expression in a JSP page, the page directive attribute having isELIgnored should be set to true value.
17. How can we avoid direct access to JSP pages from client browser?
We can avoid direct access of JSP pages from client browser by placing the JSP pages inside the WEB-INF. It can be accessed only by using a request dispatcher servlet.
18. What is the difference between include directive and JSP: include action?
It adds file resources into the JSP page.
File attribute is used to include resource during translation time.
jsp: include action
It adds page response into JSP page.
Page attribute is used to add resources during request time.
19. Can we use JSP implicit objects in JSP Declaration?
No, we can’t use JSP implicit objects inside JSP declaration.