Table of contents
1.
Introduction to Java Server Pages(JSP)
2.
JSP Interview Questions for Freshers
2.1.
1. What are Java Server Pages (JSP)?
2.2.
2. Explain the meaning of the dynamic web page?
2.3.
3. What are the advantages of using JSP?
2.4.
4. What are the life-cycle methods for a JSP?
2.5.
5. Explain the JSP while loop.
2.6.
6. How to include static files on a JSP page?
2.7.
7. What are Servlets?
2.8.
8. What is JSTL?
2.9.
9. What are Implicit JSP Objects?
2.10.
10. Explain how to use JSP in a loop.
2.11.
11. What are the JSTL Core tags?
2.12.
12. How are Custom Tags in JSP created?
2.13.
13. Which methods are used for reading from data using JSP?
2.14.
14. Differentiate between <jsp:forward page =...> and response.sendRedirect(url).
2.15.
15. Explain the life cycle of a servlet.
3.
JSP Interview Questions for Experienced
3.1.
16. Define the term expression and composition?
3.2.
17. What is MVC in JSP?
3.3.
18. In a JSP, which of the following tags is used to run Java source code?
3.4.
19. What are Cookies? Explain their types and how each type differs from the other?
3.5.
20. In JSP, what is the purpose of the tag library?
3.6.
21. What is the best way to create a thread-safe JSP page? What are the benefits and drawbacks of using it?
3.7.
22. What are the advantages of JSP over Active Server Pages (ASP)?
3.8.
23. What are the benefits of JSP over pure servlets?
3.9.
24. Why does _jspService() begin with a '_' but not the other lifecycle methods?    
3.10.
25. What do you mean by including directive?        
3.11.
26. What are JSP Operators?
3.12.
27. How can we stop errors on Display on a JSP Page?
3.13.
28. How can information from one JSP be passed to another JSP?
3.14.
29. What Literals are used for JSP?
4.
JSP MCQ
4.1.
1. Which tag is used to include files in JSP?
4.2.
2. What does JSP stand for?
4.3.
3. In JSP, which directive is used to define attributes and scriptlets?
4.4.
4. What is the default scope of JSP objects like request, response, and session?
4.5.
5. Which tag is used to forward a request from one page to another in JSP?
4.6.
6. Which JSP element is used to store the output of an expression?
4.7.
7. Which JSP directive defines the scripting language used in the page?
4.8.
8. What is the life cycle method called before the JSP page is destroyed?
4.9.
9. How can you handle exceptions in JSP?
4.10.
10. Which tag is used for including dynamic content from another resource in JSP?
5.
Coding Ninjas Resources
6.
Conclusion
Last Updated: Sep 28, 2024
Easy

JSP Interview Questions

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction to Java Server Pages(JSP)

Java Server Pages (JSP) is a server-side programming language that allows developers to create dynamic, platform-independent Web applications. JSPs have access to the entire Java API (Application Programming Interface) family, including the JDBC API( Java Database Connectivity API), enabling them to connect to enterprise databases.

jsp interview questions

It is an extension of Java Servlet that access Java API, implicit objects, and custom tags into HTML pages to create dynamic web content.

Let’s discuss the following set of JSP Interview Questions and Answers which have been categorised below:

  • JSP Interview Questions for Freshers
  • JSP Interview Questions for Experienced
  • JSP MCQ

JSP Interview Questions for Freshers

1. What are Java Server Pages (JSP)?

JavaServer Pages (JSP) is a dynamic content-supporting Web page development technology that allows programmers to insert Java code into HTML(Hyper Text Markup Langauge) pages using specific JSP tags.

JSP is a technology that controls the content and appearance of Web pages through the use of servlets, which are small programs specified in a Web page and run on the Web server to modify the Web page before it is sent to the user who requested it. The JSP technology is also known as the Servlet application program interface by Sun Microsystems, the creator of Java (API). A Java Server Page calls a Java program executed by the Web server.

An HTML page that links to a Java servlet has sometimes named the file as a suffix.JSP, most of which start with <% and end with %>.

Also Read: Java OOPs Interview Questions

2. Explain the meaning of the dynamic web page?

A dynamic web page modifies its content every time it is viewed. For example, the page may change depending on the time of day, the user who accesses the webpage, or the type of user interaction.

There are two types of dynamic web pages:

dynamic web page in jsp

 

  • CLIENT-SIDE SCRIPTING

 It is used on web pages that change response to an action on the page, such as a mouse or keyboard action.

Client-side scripts create content for the client. The client-side content is on the user's computer rather than the server. The user's web browser would download the web page content from the server, process the code embedded in the web page, and then display the updated content to the user in these cases.

A web page can respond to client-side events using scripting languages like JavaScript and Flash.

  • SERVER-SIDE SCRIPTING

It is used to change web pages when they are loaded or visited. The content generated when a web page is loaded known as server-side content. Server-side scripting is used on login pages, forums, submission forms, and shopping carts, for example, because those web pages change based on what is submitted to them.

A web page can respond to submission events using scripting languages like PHP, ASP, ASP.NET, etc.

3. What are the advantages of using JSP?

  • Developers can easily update presentation code because JSP pages are dynamically compiled into servlets.
  • Pre-compilation of JSP pages is possible.
  • JSP pages can easily be combined with code that generates dynamic content and static templates, such as HTML or XML( Extensible Markup Language) fragments.
  • Page authors can access customized JSP tag libraries using an XML-like syntax, which developers can provide.
  • Without editing the individual pages that use the application's logic, developers can make logic changes at the component level.

4. What are the life-cycle methods for a JSP?

S.No. Life cycle methods Explanation
1.

Translation of JSP page to Servlet

The JSP life cycle begins with this phase. 
2. Compilation of JSP page Now, a class file is created from the resulting Java servlet file (test.java) (test.class).
3. Classloading  The container is now loaded with the Servlet class that was loaded from the JSP source.
4. Instantiation  In this case, a class instance is created.
5.  Initialization After generating a Servlet instance from JSP, the jspInit() method is only ever called once during the life cycle.
6.  Request processing _jspService() is used to handle JSP requests.
7.  JSP Cleanup The jspDestroy()function is used to either remove the JSP from the container's use or to destroy the servlet method.

5. Explain the JSP while loop.

The JSP While loop is used to iterate the elements where it has one parameter of the condition.

Syntax of While loop:

While(Condition)
{
   // Statements
}

6. How to include static files on a JSP page?

The JSP include directive is always used to have static pages. This way, the inclusion is done only once during the translation phase. It's worth noting that the file attribute requires a relative URL. While static resources can be included, it is not recommended because it is essential to have each request.

7. What are Servlets?

In the same application, JSP pages and servlets are frequently in use together. The Java servlet specification is the foundation for the JSP specification. Simply put, a servlet is a piece of code that adds new functionality to a web server, like CGI(Common Gateway Interface) and proprietary server extensions like NSAPI and ISAPI. Servlets have several advantages over other technologies:

  • Platform and vendor independence
  • Integration
  • Efficiency
  • Scalability
  • Security and robustness
     

Know What is Servlet in detail.

8. What is JSTL?

The JSTL(Java Server Pages Standard Tag Library )is  a collection of custom JSP tag libraries that provide standard web development functionality.

Some of JSTL's properties are as follows:

  • The code is neat.
  • It provides a rich layer of portable functionality to JSP pages as a Standard Tag.
  • It supports automatic Javabeans introspection. The JSTL expression language is very good at handling JavaBean code. The objects that retrieved as scoped attributes do not need to be downcast.
  • Humans can read it more efficiently, and computers can understand it more easily.

9. What are Implicit JSP Objects?

Variable Name Java Type Description
request javax.servlet.http.HttpServletRequest The request object requests information like a parameter, header information, server name, etc.
session javax.servlet.http.HttpSession This is used to get, set, and remove attributes to session scope and get session information.
response javax.servlet.http.HttpServletResponse The response is an instance of a class that represents the response that can be given to the client
pageContext javax.servlet.jsp.PageContext This is used to get, set, and remove the attributes from a particular scope.
application javax.servlet.ServletContext This is used to get the context information and attributes in JSP.
out javax.servlet.jsp.JspWriter This implicit object is used to write the data to the buffer and send output to the client in response.
Config javax.servlet.ServletConfig Config is used to get the initialization parameter in web.xml
page java.lang.Object This implicit variable holds the currently executed servlet object for the corresponding JSP.
Exception java. lang.Throwable Exception, the implicit object of the throwable class, is used for exception handling in JSP.

10. Explain how to use JSP in a loop.

The JSP ‘For’ loop is used to iterate the elements for a specific condition, and it has three parameters:

- The counter variable is set to zero.

- If the condition is true, the loop must be executed.

- It is necessary to increment the counter.

Syntax:

for(inti=0;i<n;i++)
{
   // Block of statements
}

11. What are the JSTL Core tags?

The JSTL Core tags are used for the following purposes:

  • Iteration
  • Conditional logic
  • Catch exception
  • URL forward
  • Redirect, etc.
     

Following is the syntax to include a tag library:

<%@ taglib prefix="c" uri=http://java.sun.com/jsp/jstl/core%>

12. How are Custom Tags in JSP created?

The following steps are used to create custom tags in JSP.

  • How to Make a Tag Handler Class.
  • How to Make a TLD File
  • How to Make a JSP File

    Let us discuss these in detail:
     
  • How to Make a Tag Handler Class:

We must inherit the TagSupport Class and then override the doStartTag() method to create a Tag Handler Class. 

The JspWriter class is used to write data for JSP pages. 

The getOut() method of the PageContext class returns an instance of the JspWriter class. 

The TagSupport class later provides a default instance of pageContext.

  • How to Make a TLD File:

Tag Library Descriptor file (TLD) is an abbreviation for Tag Library Descriptor file. It's where you'll find information about tags and Tag Hander classes. It has to be kept in the WEB-INF folder.

  • How to Make a JSP File:

The path to the TLD file will be directly specified. Using the URI(Uniform Resource Identifier) name rather than the full path to the TLD file is preferable.

Must Read Sdet Interview Questions

13. Which methods are used for reading from data using JSP?

Ans. The form data parsing is handled automatically using JSP. It terminates  in one of the following ways, depending on the circumstances:

To get the value of a form parameter, use the

  • getParameter(): It is a method that returns a parameter.
  • getParameterValues(): It is called if a parameter appears multiple times and returns multiple values.
  • getParameterNames(): It is used if you want a complete list of all parameters in the current request.
  • getInputStream(): It reads binary data streams from the client.

 

14. Differentiate between <jsp:forward page =...> and response.sendRedirect(url).

Ans. The <jsp: forward> element passes the request object from one JSP file to the next. The target file can be HTML, servlet, or another JSP, but it must be in the same application context as the forwarding JSP.

SendRedirect sends the browser an HTTP temporary redirect response. After that, the browser sends a new request to the redirected page. The session variables are destroyed.

 

15. Explain the life cycle of a servlet.

Ans. These are the steps in the life cycle of a servlet:

servlet life cycle
  • Load Servlet Class: When the first request is received, the web container loads the servlet. This step is only performed once at the time of the initial request.
     
  • Create Servlet Instance: After loading the servlet class, the web container creates the servlet instance. For each servlet, only one instance is made, and the same servlet instance handles all concurrent requests.
     
  • Call init() method: After the servlet instance is created, the web container calls the servlet's init method. Before processing the first request, this method is used to initialize the servlet. The web container only makes one call to it.
     
  • Call service() method: Following the initialization process, the web container invokes the service() method. Every request invokes the service method. Servlet creates a new thread for each request.
     
  • Call destroy() method: The web container calls this method before removing the servlet instance. The destroy method requests that the servlet release all resources associated with it. When all servlet threads have exited or if a timeout occurs, the web container only calls it once.

JSP Interview Questions for Experienced

16. Define the term expression and composition?

Ans. The expression tag is used to insert Java values in the output directly. Its syntax is as follows:

<%= expression%>
You can also try this code with Online Java Compiler
Run Code

 

17. What is MVC in JSP?

 Ans. In MVC,

  • M stands for Model
  • V stands for View
  • C stands for the Controller
     

Model View Controller (MVC) is an acronym for Model View Controller and Model View. It's a design pattern in which the business logic, presentation logic, and data are separated.

The Controller acts as a link between the View and the Model. All incoming requests are intercepted by the Controller.

The state of the application, or data, is represented by the Model. Business logic can also be included in the Model.

The presentation, or UI(User Interface), is represented comes under the View.

MVC in JSP

18. In a JSP, which of the following tags is used to run Java source code?

Ans. Scriptlet tag: Using the scriptlet tag in JSP, Java code can be written inside the jsp page. Scripting elements in JSP, enable java code to be inserted into the jsp. 

Scripting elements are divided into three categories:

  •  expression tag
  •  declaration tag 
  •  scriptlet tag
     

 In JSP, the scriptlet tag is used to run java source code. The following is the syntax:

<%java code block%>

 

19. What are Cookies? Explain their types and how each type differs from the other?

Ans. Cookies are text files that are created in the client machine and store details of web searches or sites explored using the browser, date, and time of visit, along with IP address. The presence of cookies helps websites identify clients that return to their sites.

Cookies are of various types, such as Authentication, Tracking, and Session cookies.

  • Session cookies are short-lived until the user explores a particular website. Once he leaves the site, session cookies are deleted by browsers.
  • Tracking cookies, as the name suggests, records how often a particular website has been visited.
  • Authentication cookies validate a genuineness of a valid user that revisits the website.


20. In JSP, what is the purpose of the tag library?

Ans. In JSP, standard tag libraries are helpful in overcoming the limitations of custom tags. There are three main tag libraries in JSP: core, SQL, and XML. They are primarily used for iteration and conditional tasks, XML document manipulation, SQL database operations, and internationalization.


21. What is the best way to create a thread-safe JSP page? What are the benefits and drawbacks of using it?

Ans. By implementing the SingleThreadModel interface, you can make your JSPs thread-safe. This is accomplished by including the directive <% @ page isThreadSafe="false"% > in your JSP page.


22. What are the advantages of JSP over Active Server Pages (ASP)?

Ans. JSP has two distinct advantages.

  • First, the active part is written in Java rather than Visual Basic or another Microsoft-specific language, making it more powerful and user-friendly.
  • Second, it can run on non-Microsoft operating systems and Web servers.


23. What are the benefits of JSP over pure servlets?

Ans. The following are some of the significant advantages of JSP over pure servlets:    

  It's easier to write and modify standard HTML than to have many println statements generating HTML.

  • Java code can be embedded in HTML pages.
  • Independent of the platform.
  • Web applications that are database-driven are created.
  • Capabilities for server-side programming.


24. Why does _jspService() begin with a '_' but not the other lifecycle methods?    

Ans. The container places whatever content is created in a jsp page inside the _jspService() method. The compiler throws an error if overridden, but the other two lifecycles are simple to override. As a result, the '_' indicates that we cannot override this method.


25. What do you mean by including directive?        

Ans. The include directive inserts the contents of a resource into the current JSP in a static manner. It facilitates code reuse without duplication. It includes the contents of the file at the time of translation. It has the following syntax: 

<% @ include file="Filename" %>
You can also try this code with Online Java Compiler
Run Code


26. What are JSP Operators?

Ans. JSP Operators support most of the arithmetic and logical operators supported by java within expression language (EL) tags.

Following are the frequently used jsp operators:

Operator Description
. Access a bean property or Map entry.
[] Access an array or List element.
() Group a subexpression to change an evaluation order.
+ Addition
- Subtraction or negation of a value
* Multiplication
/ or div Division
% or mod Modulo (remainder)
== or eq Test for equality
!= or ne Test for inequality
< or lt Test for less than
> or gt Test for greater than
<= or le Test for less than or equal
>= or ge Test for greater than or equal
&& or AND Test for logical AND
|| or OR Test for logical OR
! or not Unary Boolean complement
Empty Test for empty variable values


27. How can we stop errors on Display on a JSP Page?

Ans. We can prevent errors from being displayed on a JSP page by setting the "ErrorPage" attribute of the PAGE directory to the name of the JSP page's error page and then selecting "isErrorpage=" TRUE" in the error JSP page.


28. How can information from one JSP be passed to another JSP?

Ans. The tag <Jsp: param> allows us to pass information between multiple Jsps.


29. What Literals are used for JSP?

Ans. Literals used in Java server pages are as follows:

  • Boolean
  • Integer
  • Floating point
  • String
  • Null.

JSP MCQ

1. Which tag is used to include files in JSP?

A. <jsp:include>
B. <jsp:forward>
C. <jsp:get>
D. <jsp:put>

Answer: A. <jsp:include>

2. What does JSP stand for?

A. Java Servlet Pages
B. Java Server Pages
C. JavaScript Pages
D. Java Scripting Pages

Answer: B. Java Server Pages

3. In JSP, which directive is used to define attributes and scriptlets?

A. page
B. taglib
C. include
D. forward

Answer: A. page

4. What is the default scope of JSP objects like request, response, and session?

A. page
B. session
C. application
D. request

Answer: A. page

5. Which tag is used to forward a request from one page to another in JSP?

A. <jsp:forward>
B. <jsp:include>
C. <jsp:transfer>
D. <jsp:redirect>

Answer: A. <jsp:forward>

6. Which JSP element is used to store the output of an expression?

A. Declaration
B. Scriptlet
C. Expression
D. Comment

Answer: C. Expression

7. Which JSP directive defines the scripting language used in the page?

A. include
B. page
C. taglib
D. script

Answer: B. page

8. What is the life cycle method called before the JSP page is destroyed?

A. jspInit()
B. jspDestroy()
C. _jspService()
D. jspService()

Answer: B. jspDestroy()

9. How can you handle exceptions in JSP?

A. Using try-catch block
B. Using errorPage attribute
C. Using page directive
D. All of the above

Answer: D. All of the above

10. Which tag is used for including dynamic content from another resource in JSP?

A. <jsp:include>
B. <jsp:forward>
C. <jsp:get>
D. <jsp:set>

Answer: A. <jsp:include>

Coding Ninjas Resources

We also have courses and articles containing popular questions from the Javascript syllabus, which will help you clear the JSP interview questions. 

You can view our guided path resources on - 

Here the JSP interview questions have come to an end. The areas covered in this JSP interview questions are the most sought-after skill sets in a candidate, according to the recruiters. These JSP interview questions will undoubtedly assist you in acing your next employment interview. 

Conclusion

In this blog, we learned about JSP interview questions. By understanding these key topics, including JSP directives, lifecycle methods, and core elements like tags and expressions, you will be well-prepared for any JSP-related interview. Whether you're a fresher or an experienced professional, mastering JSP will not only enhance your web development skills but also give you a competitive edge in Java-based enterprise applications.

Read more, Html interview questions, Azure Data Engineer Interview Questions

Refer to our Code360 to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Live masterclass