Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
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.
 
2.11.
 
2.12.
10. Explain how to use JSP in a loop.
2.13.
11. What are the JSTL Core tags?
2.14.
12. How are Custom Tags in JSP created?
2.15.
13. Which methods are used for reading from data using JSP?
2.16.
14. Differentiate between <jsp:forward page =...> and response.sendRedirect(url).
2.17.
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.
Coding Ninjas Resources
5.
Conclusion
Last Updated: Jun 12, 2024
Easy

JSP Interview Questions and Answers

Author Kanak Rana
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM
jsp interview questions

Introduction

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.

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

So let's look into the JSP interview questions that will help you crack interviews.

JSP Interview Questions for Freshers

1. What are Java Server Pages (JSP)?

Ans. 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?

Ans. 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.

What are the life-cycle methods for a JSP?

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

S.No.Life cycle methodsExplanation
1.

Translation of JSP page to Servlet

The JSP life cycle begins with this phase. 
2.Compilation of JSP pageNow, 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. InitializationAfter 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 CleanupThe 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.

Ans. 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?

Ans. 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?

jsp servlet

Ans. 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?

Ans. 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 NameJava TypeDescription
requestjavax.servlet.http.HttpServletRequestThe request object requests information like a parameter, header information, server name, etc.
sessionjavax.servlet.http.HttpSessionThis is used to get, set, and remove attributes to session scope and get session information.
responsejavax.servlet.http.HttpServletResponseThe response is an instance of a class that represents the response that can be given to the client
pageContextjavax.servlet.jsp.PageContextThis is used to get, set, and remove the attributes from a particular scope.
applicationjavax.servlet.ServletContextThis is used to get the context information and attributes in JSP.
outjavax.servlet.jsp.JspWriterThis implicit object is used to write the data to the buffer and send output to the client in response.
Configjavax.servlet.ServletConfigConfig is used to get the initialization parameter in web.xml
pagejava.lang.ObjectThis implicit variable holds the currently executed servlet object for the corresponding JSP.
Exceptionjava. lang.ThrowableException, the implicit object of the throwable class, is used for exception handling in JSP.

 

 

 

10. Explain how to use JSP in a loop.

Ans. 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?

Ans. 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?

Ans. 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.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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%>

 

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" %>


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:

.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 divDivision
% or modModulo (remainder)
== or eqTest for equality
!= or neTest for inequality
< or ltTest for less than
> or gtTest for greater than
<= or leTest for less than or equal
>= or geTest for greater than or equal
&& or ANDTest for logical AND
|| or ORTest for logical OR
! or notUnary Boolean complement
EmptyTest 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.
     

Must Read FP&A Interview Questions,Html interview questions

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 and went through questions and answers in detail. There are a total of 28 JSP interview questions that will help you to crack interview rounds. Refer to other interview questions related blogs like Java collection interview questionsAngular 2 Interview QuestionsBootstrap interview questions, Excel Interview Questions.

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

Refer to our guided paths on Coding Ninjas Studio 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.

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja!

Previous article
Top 40+ jQuery Interview Questions and Answers (2024)
Next article
Serialization in Java Interview Questions
Live masterclass