Updated March 15, 2023
Introduction to JSP Interview Questions and Answers
JSP (or Java Server Pages) interview questions are significant to gauge candidates’ knowledge and experience with Java web development. JSP technology is constantly evolving, with new features and improvements being introduced regularly. By asking JSP interview questions, companies can ensure that candidates stay up-to-date with the latest trends and advancements in the field of Java web development.
To land a job in the JSP domain, it is crucial that one prepares for the 2023 JSP interview questions. Even though each interview may differ depending on the job profiles, here is a compilation of the most common questions (and their answers) in the JSP interview.
Here are the top 10 questions in two parts:
Part 1 – JSP Interview Questions (Basic)
1. What do you understand by the JSP lifecycle phases?
Answer:
JSP belongs to the Java group, but its code looks more like HTML than Java. It is a JSP container that translates JSP pages and creates a servlet class that is usable in web applications. The lifecycle phases of JSP are as follows:
- Translation: The JSP container checks and parses the JSP page code to generate the servlet source code.
- Compilation: The JSP container compiles the JSP class source code and produces a class file.
- Classloading: In this phase, the container loads the class into memory.
- Instantiation: The container invokes the no-args constructor of the generated class and loads it into memory, and then the instantiation is complete.
- Initialization: In this phase, the container invokes the init method of the JSP class object, and the servlet configuration with init parameter is configured. Now, JSP can handle client requests. Translation and initialization in JSP often happen as soon as the first request for JSP comes.
- Request Processing: It is the most time-consuming phase of the JSP page. Here, the JSP page processes the requests from a client. The request processing phase is multi-threaded, and for every request, a new thread is used, and the Servlet-request and Servlet-response object is created. After this, the JSP service method is called.
- Destroy: It is the last phase of the JSP lifecycle, in which the JSP class is unloaded from memory. This is done when the application is undeployed or when the server is down.
2. Elaborate on JSP lifecycle methods?
Answer:
The JSP lifecycle methods are as follows:
- jspInit(): This method is called only once in the JSP lifecycle. It is used to initialize configuration parameters in a deployment descriptor and can be overridden using a JSP declaration scripting element. One undertakes overriding to initialize those resources that are usable on the JSP page.
- _jspService(): This method is invoked by the JSP container for each client request and passes the request and response object. This method cannot be overridden and thus starts with an underscore. Overriding it using JSP declaration scripting element is not suggested. It should be defined and declared in the HttpJspPage interface.
- jspDestroy(): This method is called when the JSP is unloaded from memory and is used to shut down the application or container. This method is also called only once in the JSP lifecycle and should only be overridden to release resources created in the jspInit() method.
3. Mention some JSP lifecycle methods that are overridable?
Answer:
The above answer explains that the jspInit() and jspDestroy() methods in JSP can be overridden using the JSP declaration scripting element. The jspInit() method is typically overridden to create common resources that are used in the JSP service method. On the other hand, the jspDestroy() method should be overridden to release the common resources that were created using jspInit().
4. What do you understand by JSP declarations?
Answer:
JSP declarations allow the declaration of one or more variables and methods for use in Java code within the JSP file. It is necessary to declare a variable or method before it can be used in the JSP file.
5. Explain JSP expressions?
Answer:
JSP expression is an element that contains a scripting language expression. It evaluates String conversion and insertion at the point where the expression appears in the JSP file. The expression element can contain any expression that is valid according to the Java Language Specification. One must refrain from using a semicolon to terminate a JSP expression.
Part 2 –JSP Interview Questions (Advanced)
6. How to use JSP in the MVC model?
Answer:
Developers commonly use JSP (JavaServer Pages) for presentation in the Model-View-Controller (MVC) pattern. In this architecture, JSP serves as the View component. The controller is responsible for calling the model and business classes. The model retrieves data and sends it back to the controller. The controller then passes this data to the JSP for rendering on the client-side.
7. What comprises JSP literals?
Answer:
JSP literals contain the following types of data formats:
- Boolean
- Integer
- String
- Floating point
- Null
8. Mention the different scope values for the given tag: <jsp:useBean>
Answer:
There are 4 values in the above tag:
- page
- request
- a session, and application
9. Explain how to configure init params in JSP?
Answer:
In JSPs, init params can be configured in a similar way to how it is done in a Servlet. The “init-param” tag helps do this within the “jsp-file” element. The main difference between configuring init params in a JSP and a Servlet is that in a Servlet, we do not need to specify the location of the JSP page, whereas in a JSP, we need to specify the JSP file location within the “jsp-file” element.
10. What is JSP Action Elements or JSP Action Tags?
Answer:
JSP action elements, also known as action tags, are tags that resemble HTML tags and provide several useful functionalities in JSP pages. These functionalities include working with JavaBeans, request forwarding, generating dynamic XML elements, and including resources, among others. The convention is that JSP action elements begin with “jsp:” and they can be used directly in a JSP page without importing any tag libraries or making additional configuration changes.
Some of the most commonly used JSP action elements include:
- jsp:useBean
- jsp:setProperty
- jsp:getProperty
- jsp:forward
- jsp:include
Recommended Article
We hope that the above article will be useful in preparing for JSP Interview Questions. Here are some further interview questions articles: