Hello Guys, First of all I would like to thank you for appreciating the tutorials on JSP. Most of you wanted me to share the interview questions on Java Server Pages (JSP), so here am I sharing the latest set of interview questions on JSP. Before you continue reading further, I would suggest you to go through the JSP basics first because that would be a good start for you as most of the interviews begin with the basics then the interviewer discusses further based on the responses.
Q) What is JSP?
A) JSP stands for Java Server Pages, it is a server side technology which is used for creating dynamic web pages. It is the extension of servlets.
Based on the above response, the interviewer may ask below follow-up questions:
What does dynamic web page means here? The web pages that are generated based on user’s response and may be different for each user are called dynamic web pages unlike the static web pages that are same for every user no matter how they interact with the application.
What does Server side technology means? There are basically two types of technologies: client-side and Server-side. Client-side means that the action takes place on the user’s (the client’s) computer. Server-side means that the action takes place on a web server (the place where you have stored all your JSP pages).
What is Servlet? Refer this.
Q) What are JSP life cycle phases?
A) A JSP page goes through the below phases:
1) Compilation: In this phase the JSP code gets converted into the equivalent servlet code.
2) Initialization: The converted JSP code gets loaded into the memory. jspInit() method gets called in this phase.
3) Execution: _jspService() method gets called in this phase. In this step a response is generated for the user based on the request made by them.
4) Destroy: jspDestroy() method gets called in this phase to unload the JSP from the memory. This is also known as cleanup step.
For more details on JSP life cycle phases read this article.
Q) What all JSP lifecycle methods can you override in your JSP application?
You can only override jspInit() and jspDestroy(), you cannot override the _jspService() method within a JSP page. By overriding jspInit() method you can initialize things like database connections, network connections etc. Whatever you initialize in jspInit() method can be freed up (released) in jspDestroy() method.
Q) How many implicit objects you have in JSP, name them?
The objects that can directly be used on any JSP page without the need of being declared first are known as implicit objects. In JSP we have total 9 implicit objects, they are as follows:
To learn implicit objects in detail with examples, refer this article.
Q) What is the difference between include directive and include action tag?
A) Refer this article: include directive vs. include action tag.
Q) What is the purpose of scriptlets in JSP? What’s the syntax of it?
A) A scriptlet is used for including java code in a JSP page.
<% Java Code %>
Refer this article for more detail on Scriptlet.
Q) What is JSP declaration tag?
A) A JSP declaration tag is used for declaring variables and methods so that you can use them later on a JSP page based on the requirement.
<%! Declare variables /Methods %>
Refer this article for more detail on JSP declaration tag.
Q) What all directives available in JSP?
A) There are three types of directives available in JSP
1) Page directive: This directive is used for setting up the attributes of a JSP page. Refer this article for complete page directive tutorial.
<%@ page attribute=”value”%>
2) Include directive: Include a JSP file into another JSP file during the translation phase of JSP life cycle. Refer include directive tutorial for more detail.
<%@ include attribute=”value”%>
3) Taglib directive: This directive is basically used for custom tags. Read more about custom tags here.
Q) How to handle an exception in JSP?
A) Refer this article: Exception handling in JSP
Q) What is expression language in JSP?
A) Refer this article: EL in JSP
Q) How do you disable a session on a particular JSP page?
A) By using the session attribute of page directive, we can disable the session on a particular JSP page. This is how we can do it: <%@ page session=”false”>
By default the session attribute is set to true.
Q) How do you add a comment on a JSP page?
A) This is how we can do it: <%– JSP comment –%>
Q) Is it possible to import a package in a JSP page?
A) Yes, we can import a package using import attribute of page directive.
Q) Explain JSTL?
A) Refer JSTL tutorial.