JSP Interview Questions

JSP Interview Questions | Freshers & Experienced

  • Priyag Chaudhary
  • 06th Dec, 2021

Key Features of Jsp

Below are few major features of Jsp

Easy to code

Tracking the User

Easier to connect


Extension to Servlet



Less code than Servlet

Practice Best JSP Interview Questions & Answers;

Q1. What is JSP?

JSP(JavaServer Pages) is a server side technology to create dynamic web application. It is used to insert Java code into HTML/XML page to create an advance version of Servlet. JSP requires a servlet container to convert it into the servlet application and deploy it. In JavaServer Pages, the JavaBean component acts as a model, and Java Servlet acts as the controller. So, JSP can be viewed as MVC(Model, View, Controller) architecture. The JSP technology was developed by Sun Microsystems in 1999 to give developers an alternative to PHP and ASP.net in developing web applications.

Q2. What are advantages of using JSP?

Advantages of using JSP are,

  • With JSP, you can combine the static HTML, XML fragments with the dynamic Java code to create dynamic content.
  • JSP is a tag-based language that is easy to write and read.
  • The presentation code can be easily converted as the JSP page is dynamically converted into servlet when requested.
  • Developers can create their own tags with JSP.
  • The presentation code(HTML) is separated from the logic code(Java). So, the presentation can be changed without affecting the logic and vice versa.

Q3. What are the advantages of JSP over Pure Servlets?

JSP has many advantages over JSP. They are,

  1. JSP doesn’t require extensive knowledge of JAVA as it is tag-based. Whereas, servlet requires extensive knowledge of JAVA.
  2. The implicit objects can be used in JSP without any additional code. In the servlet, additional code is required to use the implicit objects.
  3. Modification done in JSP is compiled and reloaded automatically. In the servlet, manual loading should be done to apply the changes.
  4. JSP is easy to learn and apply than servlet.
  5. JSP has built-in and customized user tags. Servlet is not a tag-based language.

Q4. Explain lifecycle of a JSP.

JSP lifecycle is defined as follows,

JSP Compilation – If JSP is modified recently, then the JSP engine compiles the JSP page. In the compilation process, the JSP does the following,

  • Parsing the JSP
  • Converting JSP into the servlet
  • Compiling the Servlet

JSP Initialization – Here, the JSP container loads the JSP. To perform JSP requests, JspInit() method is overridden. In the jspInit() method initialization code is like database connection, open files, and creating lookup code is performed.

JSP Execution – In this phase, the requests to the JSP are executed by _jspService() method. JSP engine loads the _jspService() method to generate the response based on the request.

JSP Cleanup – The JSP is removed from the container by the jspDestroy() method in this phase. Here, the database connection is released, and the opened files are closed, etc.

Q5. What is a scriptlet in JSP and what is its syntax?

JSP Technology can be thought of as an addition to Servlet. With JSP, you can create a Servlet application that has more advantages than native Servlet applications like using implicit objects, predefined tags, custom tags, expression language, and many more.

//syntax for JSP Scriptlet 
<% code fragment %> 
<% out.println(“Scriptlet example”); %>

Q6. What are JSP Directives?

JSP directives are used to provide information about the JSP pages to the JSP container. For translating JSP to Servlet, directives are used to pass special information about the JSP page to the server container for handling some aspects of JSP processing. JSP has three types of directive tags to provide different information about the JSP page to the container. The directive can have number of attributes that is defined as key/valur pair.

//syntax for a directive
<%@ directive attribute = "value" %>

Q7. What are the types of directive tags?

There are three types of JSP directive

Page directive

It is used to provide information about the JSP page to the web container. It defines characteristics about a page like the scripting language, buffer size, etc about a JSP page.It has a list of attributes like language ,extends, import, contentType, info, session, buffer, isErrorPage, pageEncoding, autoFlush, etc.

<%@ page attribute = "value" %>

Include directive

It is used to include a file to the JSP page for page translation. The included file can be HTML, JSP, etc. The included file is in the URL form. The included file is then merged with the JSP page during page translation.

<%@ include file = "relative url" >

Taglib directive

This directive is used to create custom tags to implement custom behaviour. The custom tag is created by using the JSP standard tag libraries. The taglib library creates the custom tag and provides the library location and other resource to identify the custom tags.

<%@ taglib uri="uri" prefix = "prefixOfTag" >

Here, the uri is the location in the container that resolves the custom tag and prefix is used to identify the tag name.

Q8. What are JSP actions?

JSP action tag is one of the predefined tags that is used to control the flow between the pages. It mainly controls the behavior of the servlet engine. Some of the predefined action tags are,

Jsp:forward – used to forward requests and response.

Jsp:include – used to include other resources.

Jsp:useBean – used to create and locate bean.

Jsp:getProperty – used to set a bean value property.

Jsp:setProperty – used to get a bean value property.

Jsp:plugin – used to embed other components.

Jsp:param – used to set the parameter value.

Jsp:fallback – used to print a message for the plugin.

Q9. What are JSP literals?

In JSP, literals are just a value like a number or a value of a string. These values can be printed on the browser by using the out implicit object.

<% out.print(“the value of the integer is”); 
       out.print(7); %> 

Q10. What is a page directive?

JSP page directive is used to provide information about the JSP page to the server. It has various attributes that are applied to the entire JSP page.

<% page attribute = “value” %> 

Q11. What are various attributes Of page directive?

Various attributes in page directives are,

Import – used to include class, interface into the JSP page.

ContentType – it defines the MIME type of HTTP response.

Extends – it defines the parent class to be inherited.

Info – it is used to set the information about the JSP page.

Buffer – defines the buffer size to handle the output.

Language – it is used to specify the scripting language.

IsElIignored – it is used to ignore or include the EL in JSP.

IsThreadsafe – used to control the thread behavior of JSP.

ErrorPage – it is used to define the error page.

IsErrorPage – used to define if the current JSP page is an error or not.

Q12. What is an isThreadSafe attribute?

The isThreadSafe is one of the attributes of page directive that control the thread behavior of the JSP page. The default value of this attribute is true to make a multithreaded process. If you make it false,

Then the requests will be serialized. That is, a new request is passed only after finishing the current process.

<% page isThreadSafe = “true” %>

Q13. What is language attribute?

The language attribute in the page directive is used to specify what scripting language is used in the current page.

<% @page language = “value” %> 
/example <% @page language = “java” %> //it is used to specify if the current page has java scriptlet language.

Q14. What is session attribute?

The session attribute in the page directive is used to specify whether the current page uses an HTTP session. If the value of the attribute is true, then the page has access to the JSP session attribute or not if the value is false.

<% @page session = “true” %> 

Q15. What is the function of <jsp:include> action?

The JSP include tag is used to include other resource like HTML, Servlet, etc into the current JSP page. Other parameters can also be passed while including the resource using the include tag.

<jsp:include page =”URL_of_Resource”/> 
//syntax with passing parameters 
<jsp:include page="_URL_Of_Resource">  
<jsp:param ... />  
<jsp:param ... />  

Q16. What is <jsp:useBean> action?

The action tag in JSP is used to access the JavaBeans component. To interact with the JavaBean component, the bean object should be declared first. The tag is used to declare and initialize the bean object.

//sytax of useBean tag
<jsp:useBean id = "beanName" class = "className" scope = "page|request|session|application">

The id is the name of the bean, class is the specified class name, and scope is the place where the bean can be accessed. The scope has four values with page as default, request, session, and application.

Q17. What is <jsp:forward> Action?

The JSP forward is a type of action tag that is used to forward a request to another resource. The other resource can be a JSP page, HTML, or Servlet.

<jsp:forward page=”resource_file”> //here mention the resource file in the page attribute. 
The forward tag can be used with the parameters. 
<jsp:forw ard page="resource_file">  
<jsp:param ... />  
<jsp:param ... />  

Q18. What is <jsp:plugin> Action?

The plugin tag is used to insert java components such as applet or Javabean into the JSP page to be executed. If the specified component is not present, then this tag downloads the component and executes it. This tag can be used in conjunction with the fallback tag to display the error message if the component fails. It inserts the or tag as needed. The plugin tag can also be used with parameter tags.

<jsp:plugin type = "applet" codebase = "dirname" code = "Applet.class" width = "150" height = "150"> 
  <jsp:param name = "fontcolor" value = "red" /> 
     Error with the plugin 
Here, the applet class is inserted into the JSP page. If there is some problem, then the error message is displayed. 

Q19. What are JSP implicit objects?

The implicit object is a Java object that is provided by the JSP container to the developers that can be accessed in any JSP page without being explicitly declared. The many JSP implicit objects are,

  • Request – it is the HttpservletRequest object that is associated with the request.
  • Response – it is the HttpservletResponse object that is associated with the response.
  • Out – It is used to send output to the client.
  • Session – It is the HttpSession object associated with the response.
  • Config– It is the ServletConfig object associated with the JSP page.
  • Application– It is associated with the application context.
  • PageConquest – It is used to encapsulate user-specific features.
  • Page – It is used to call methods defined by the translated servlet class.
  • Exception – it is used to access the exception data by the designated JSP page.

Q20. What is a request object?

The request object in JSP is derived from the HttpServletRequest object. A type of implicit object that is created for each request by the web container. It is used to get information such as server name, server port, header information, remote address, content type, and character encoding.

   String name=request.getParameter("name");   
    out.print("welcome "+name);   

In the above example, the parameter name value is acquired by the request object.

Q21. What is a response object?

The JSP implicit response object is a type of HttpServletResponse. The webcontainer creates this object for JSP response. It is used to redirect, manipulate, or add response.

<% response.sendRedirect(“https://bing.com”); %>

Q22. What is the difference between JspWriter and PrintWriter?

JSPWriter is nothing but the buffered version of the PrintWriter. The JSP pages use JSPWriter to write content as the JSP pages are buffered. Once the buffer is flushed, the content is written using PrintWriter. The PrintWriter class comes from the java.io.package whereas the JSPWriter comes from the javax.servlet.jsp package. The PrintWriter is used for servlet programming whereas the JSPWriter is used for JSP to write content as it an implicit object. JSPWriter can be used to throw IO exception whereas the PrintWriter does not.

Q23. What is the session Object?

The JSP session object uses the HttpSession Interface to track the client session between the client requests. This object gives the methods to set, get, or remove the session information. By default, the session tracking is enabled as the HttpSession object gets instantiated for each client. To disable the tracking, set the value for session attribute as false,

<%@ page session = “false” %> //to disable the tracking 
//example for a session 
session.setAttribute("user",name); //setting the value in session in one JSP file. 
String name=(String)session.getAttribute("user"); //acquiring the value of that session in another jsp file. 

Q24. What is an application Object?

When the application is deployed on the server, the application implicit object by instantiating the ServletContext type. With this object, you get the initialization parameter from the configuration file, and you can get, set, or remove the attribute from the application scope.

String driver=application.getInitParameter("name"); //here the name value is procured from the configuration file.

Q25. How to read form data using JSP?

JSP gives the developers four methods from the request object to get the value from the form. getParameter() - it is used to get value from the form.

 <li><p><b>First Name:</b> 
  <%= request.getParameter("first_name")%> 

getParameterValues() - it is used to get multiple values like from the checkbox in the form.

getParameterNames() - it is used to get a list of all parameters.

getInputStream() - it is used to read binary data from the client.

Q26. How can you delete a session data?

To remove a particular attribute in the session object, use the public void removeAttribute(String name) method. Pass the key associated with the attribute to this method to remove that attribute.

To delete the whole session, use public void invalidate() method. 

Q27. How do you implement hit counter in JSP?

The hit counter is used to calculate the number of visits on your web site. The Hit Counter is attached to the index.jsp page to calculate the hits. To implement hit counter, the getAttribute() and setAttribute() function from the Application implicit object is used as it is created when the JSP page is created and destroyed when the JSP page is destroyed.

        Integer Count = (Integer)application.getAttribute("Counter"); //get the value of the count from the keyword. 
        if( hitsCount ==null || hitsCount == 0 ) { 
           /* First visit */ 
           Count = 1; 
        } else { 
           /* return visit */ 
           Count += 1; 
         application.setAttribute("Counter", Count); //sets the value of the count using the Counter keyword. 

Q28. What is auto refresh feature?

JSP provides a simple method to refresh your website regularly. The method used is setInHeader(). You pass the seconds in the integer to this method to indicate the time interval to refresh the site. After the interval hits, it sends the “Refresh” header to the browser to refresh the site. This method is useful when your site needs to refresh automatically without the user pressing the refresh button.

//syntax for setInHeader() 
public void setIntHeader(String header, int headerValue) 

Q29. How do you implement the auto refresh in JSP?

To implement the auto refresh feature in JSP, use the setInHeader() method. Pass the time interval value to this method for refreshing automatically when the time hits.

<%  response.setIntHeader("Refresh", 5); %> //it refreshes the website for every 5 seconds.

About Author :

  • Author of JSP Interview Questions

    Priyag Chaudhary

    Web Developer from the Noida area. I have rich experience in PHP, Angular Js, React, Javascript and Node. If you any query or project on these programming you can drop your inquiry in the comment section.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.