jsp interview questions and answers

This JSP interview questions and answers are asked in the various J2EE interview and focus on view part of 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 is 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. 

 

Difference Between include Directive and include Action of JSP:

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.



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.

 

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.

 

what are the implicit Object

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.

 

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

 

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 implemented this two in jsp so making this difference container use _ in jspService() method and shows that we cant override this method.

 

How can you pass information form one jsp to included jsp:

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

Example:

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



what is the need of 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.



Difference between sendredirect and forward in Servlet ?

One of the classical Interview Question from Servlet and JSP. This question is as old as Vector vs ArrayList in core Java. Anyway see difference between sendredirect and forward to answer this Servlet Interview question.



How do remove variable using <c:set> tag from JSTL ?

This is one of the tricky Servlet JSP question. Many people assumes that <c:set> can only add or set variables in a particular scope but you can also remove any variable from any scope using JSTL <c:set> tag. See How to use <c:set> JSTL tag in JSP for exact way to remove any variable from any scope in JSP page.



What is difference between Web Server and Application Server ?

This is rather simple Servlet JSP question to answer. If you have used EJB then you should know that , Web Server doesn't contain EJB container and EJB can not be deployed on that. Application Server is used to deploy and run EJB in J2EE environment. See 5 difference between Application and Web Server to see more differences.

 

Sources:

servlet tutorials

 

Jsp interview questions and answers