Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, July 27, 2021

Top 10 JSP Interview Questions Answers for Java Programmer

These JSP interview questions and answers are asked in the various J2EE interview and focus on the view part of the MVC framework. JSP or Java Server Pages is a Java technology used to render a dynamic view. Whenever you go for any Java web developer interview there are always some interview questions on JSP. This article is a collection of some JSP interview questions which has asked in the various interview to my friends and colleagues. You can easily find answers to these JSP interview questions by google but I have included my version for quick reference.

I would love to hear back from you and please share some JSP interview questions asked to you guys on the interview; I can include those in the main post for everybody’s benefit.

This article is in continuation of my interview series of Spring interview Questions, Servlet Interview Questions, Java Interview Questions Answers, and Singleton pattern interview questions

JSP Interview Questions and Answers

Here is a collection of 10 most frequently asked interview questions on JSP, there was a lot on my kitty but I only included 10 to avoid getting post lengthy, also, my purpose was to prepare a list of questions that can be easily referenced before going for any JSP or Java interview. Not wasting any more time here are my top 10 JSP interview questions answers:

Question 1: Explain include Directive and include Action of JSP

Ans:  This is a very popular interview question on JSP, which has been asked from a long time and still asked in the various interview. This question is good to test some fundamental concepts like the translation of JSP and the difference between translation time and run-time kind of concept.

The syntax for include Directive is <%@ include file="fileName" %> which means we are including some file to our JSP Page when we use include directive contents of an included file will be added to calling JSP page at translation time means when the calling JSP is converted to a servlet ,all the contents are added to that page.

One important thing is that any JSP page has complied if we make any changes to that particular page but if we have changed the included file or JSP page the main calling JSP page will not execute again so the output will not be according to our expectation, this one is the main disadvantage of using the include directive that why it is mostly used to add static  resources, like Header and footer .

The syntax for include action is <jsp:include page=”relativeURL” /> it’s a runtime procedure means the result of the JSP page which is mentioned in relative URL is appended to calling JSP at runtime on their response object at the location where we have used this tag.  

So any changes made to an included page are being effected every time, this is the main advantage of this action but only a relative URL we can use here , because request and response object is passed between calling JSP and included JSP. IF you want to learn more then you can also check these Servlet and JSP online courses to learn more. 

JSP Interivew questions Answers

Question 2: Difference Between include Directive and include Action of JSP

This JSP interview question is a continuation of the earlier question I just made it a separate one to write an answer in clear tabular format.

Include Directive
Include Action
include directive is processed at the translation time
Include action is processed at the run time.
include directive can use relative or absolute path
Include action always use relative path
Include directive can only include contents of resource it will not process the dynamic resource
Include action process the dynamic resource and result will be added to calling JSP
We can not pass any other parameter
Here we can pass other parameter also using JSP:param
We cannot  pass any request or response object to calling jsp to included file or JSP or vice versa
In this case it’s possible.

Question 3: Is it possible for one JSP to extend another java class if yes how?

Ans: Yes it is possible we can extends another JSP using this <%@ include page extends="classname" %> it’s a perfectly correct because when JSP is converted to servlet its implements javax.servlet.jsp.HttpJspPage interface, so for jsp page its possible to extend another java class . This question can be tricky if you don’t know some basic fact J, though it's not advisable to write java code in jsp instead it's better to use expression language and tag library.

Question 4: What is < jsp:usebean >tag why it is used.

Ans: This was a very popular JSP interview question during early 2000, it has lost some of its shine but still asked now and then on interviews.

JSP Syntax
        scope="page | request | session | application"
            class="package.class"    type="package.class"


This tag is used to create an instance of java bean, first of all, it tries to find out the bean if bean instance already exists assign stores a reference to it in the variable. If we specified the type, gives the Bean that type.otherwise instantiates it from the class we specify, storing a reference to it in the new jsp:usebean is a simple way to create a java bean.
<jsp:useBean id="stock" scope="request" class="market.Stock" />
<jsp:setProperty name="bid" property="price" value="0.0" />
a <jsp:useBean> element contains a <jsp:setProperty> element that sets property values in the Bean,we have <jsp:getProperty>element also to get the value from the bean.

Explanation of Attribute

A variable that identifies the Bean in the scope we specify. If the Bean has already been created by another <jsp:useBean> element, the value of id must match the value of id used in the original <jsp:useBean> element.
scope="page | request | session | application"
The scope in which the Bean exists and the variable named in id is available. The default value is page. The meanings of the different scopes are shown below:
  • page – we can use the Bean within the JSP page with the <jsp:useBean> element
  • request – we can use the Bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another file.
  • session – we can use the Bean from any JSP page in the same session as the JSP page that created the Bean. The Bean exists across the entire session, and any page that participates in the session can use it..
  • application – we can use the Bean from any JSP page in the same application as the JSP page that created the Bean. The Bean exists across an entire JSP application, and any page in the application can use the Bean.
Instantiates a Bean from a class, using the new keyword and the class constructor. The class must not be abstract and must have a public, no-argument constructor.
If the Bean already exists in the scope, gives the Bean a data type other than the class from which it was instantiated. If you use type without class or beanName, no Bean is instantiated.

Question 5: How can one Jsp Communicate with Java file.

Ans:we have import tag <%@ page import="market.stock.*” %> like this we can import all the java file to our jsp and use them as a regular class another way is  servlet can send  the instance of the java class to our  jsp and we can retrieve that object from the request obj and use it in our page.

Question 6: what are the implicit Objects in JSP?

Ans: This is a fact based interview question what it checks is how much coding you do in JSP if you are doing it frequently you definitely know them. Implicit object is the object that is created by web container provides to a developer to access them in their program using JavaBeans and Servlets. These objects are called implicit objects because they are automatically instantiated.they are bydefault available in JSP page.

They are request, response, pageContext, session, and application, out, config, page, and exception.

Question 7: In JSP page how can we handle runtime exception?

Ans: This is another popular JSP interview question which has asked to check how candidate used to handle Error and Exception in JSP. We can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page.

Example: <%@ page errorPage="error.jsp" %>

It will redirect the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, will have to indicate that it is an error-processing page, using the directive: <%@ page isErrorPage="true" %>

Question 8: Why is _jspService() method starting with an '_' while other life cycle methods do not?

Ans: main JSP life cycle method are jspInit() jspDestroy() and _jspService() ,bydefault whatever content we write in our jsp page will go inside the _jspService() method by the container if again will try to override this method JSP compiler will give error but we can override other two life cycle method as we have implementing this two in jsp so making this difference container use _ in jspService() method and shows that we cant override this method.

Question 9: How can you pass information from one jsp to included jsp:

Ans: This JSP interview question is a little tricky and fact based. Using < Jsp: param> tag we can pass parameter from main jsp to included jsp page


<jsp:include page="newbid.jsp" flush="true">
<jsp:param name="price" value="123.7"/>
<jsp:param name="quantity" value="4"/>

Question 10: what is the need for a tag library?

Ans tag library is a collection of custom tags. Custom actions helps recurring tasks will be handled more easily they can be reused across more than one application and increase productivity. JSP tag libraries are used by Web application designers who can focus on presentation issues rather than being concerned with how to access databases and other enterprise services. Some of the popular tag libraries are Apache display tag library and String tag library. You can also check my post on display tag library example on Spring.

Please contribute any interview questions asked to you guys on JSP Interview or if you are looking for the answer of any JSP questions, I will try to help you.


Anonymous said...

JSP Questions are rather good and answers are better.

Anand Vijayakumar said...

@ Javin

Nice collection man. Some more JSP questions to help our java folks here. JSP Interview Questions


Anonymous said...

few more interview question on JSP
how to you use foreach loop to read values from session map in jsp.
What are different ways of managing session in JSP.

Another jsp interview question is what is difference between JSP and PHP

Manav Pandit said...

@Anonymous, In order to use foreach loop in JSP you need to import JSTL tag library. include jstl.jar and standard.jar and then use <@taglib> tag to import core tag library. is now accessible to you. foreach can iterate over any collection.

There are mainly three ways to manage session in JSP applications 1) using hidden html fields 2) using Cookie and 3) using URL rewriting. In Cookie small piece of information is stored in client machine but User can turn off this option and may be there is case of browser not supporting cookies, on all those cases URL rewriting is the best option to manage session in JSP Servlet application. JSTL core tag library provides tag which can encode URL for managing session.

Anonymous said...

One question was asked in j2ee interview regarding jsp->what can you do to prevent the recompilation of a jsp page again again in tomcat container?

Karthik AP said...

In an interview one question was askewd like this...

What does the WebContent Contains?

rohit said...

Can we call Servlet from Jsp ???

Unknown said...

We have two jsp pages
We write code in scriptlet tag on first jsp page how can I access that code in second jsp page.

Unknown said...

how to used doget and dopost method in jsp.

Post a Comment