JSP Interview Questions and Answers - Freshers & Experienced!

Dear Readers, Welcome to JSP Interview questions with answers and explanation. These solved JSP Programming questions will help you prepare for technical interviews and online selection tests during campus placement for freshers and job interviews for professionals.

After reading these tricky JSP questions, you can easily attempt the objective type and multiple choice type questions on JSP Programming.

What is the query used to retrieve all the table names on SQL Server (Query analyzer)? Is it possible implement an interface in a JSP? Give all the different scope values for <jsp:useBean> tag.

- The query that we have to use is :
select * from information _ schema.tables
- An interface cannot be implemented in jsp.
- <jsp:useBean> tag is used for using any java object in a jsp page.
- The scope values for <jsp:useBean> tag are listed below :
1. Page
2. Request
3. Session
4. Application

Explain the difference between ServletContext and PageContext. How to create request.getRequestDispatcher () and context.getRequestDispatcher()?

- The ServletContext : Provides the information about the container.
- PageContext : Provides the information about the Request.
- To create request.getRequestDispatcher(path) we have to provide the relative path of the resource. But to create context.getRequestDispatcher (path) we have to provide the absolute path of the resource.

How to pass the information from the the JSP to the included JSP? Explain the difference between directive include and jsp include.

- We can pass the information from the JSP to the included JSP by the Using <%jsp:param> tag.
- During passing the information <%@ include> may be used for including the static resources.
- During translation time the JSP include is used for including the dynamic programming.
- Directive include is used for including the non-JSP file contents or static contents during runtime.

Explain the difference between RequestDispatcher and sendRedirect? How JSP handles runtime exceptions?

- RequestDispatcher redirects the server-side by requesting and responding the objects. But sendRedirect redirects the Client-side with new request and responding the objects.
- JSP handles runtime exceptions by using the errorPage attribute of the page directive.
- By specifying ErrorPage = true the current page intended URL is redirected to the JSP.

How to delete a Cookie in a JSP?

- The following code explain how to delete a Cookie in a JSP :
Cookie mycook = new Cookie("name1","value1");
response.addCookie(mycook1);
Cookie killmycook = new Cookie("mycook1","value1");
killmycook . set MaxAge ( 0 );
killmycook . set Path (" / ");
killmycook . addCookie ( killmycook 1 );

How we will be able to intersect JSP and SSI #include?

- To intersect the JSP and the SSI #include we have to make class Cloneable.
- But still we will not be able to access the protected method clone.
- So we have to include a raw HTML, using the #include directive inside our .jsp file.
- Code :
<!--#include file="data.inc"-->
- The server will be able to evaluate various JSP codes inside the included file by using the following code :
<%@ include="data.inc" %>
But, the <!–#include file="data.inc"–> is mainly used for including the non-JSP files.

Explain the types of JSP actions.

- JSP actions are mainly XML tags which directs the server for using existing components or for controlling the behavior of the JSP engine.
- JSP Actions mainly comprises of a typical (XML-based) prefix of "jsp" followed by a colon( : ), following by the action name & followed by one or more attribute parameters.
- The six JSP Actions are:
1. <jsp:include/>
2. <jsp:forward/>
3. <jsp:plugin/>
4. <jsp:usebean/>
5. <jsp:setProperty/>
6. <jsp:getProperty/>

Give the basic differences between <jsp:include page = ... > and <%@ include file = ... >?

- Both the tag helps to include the information from one page to another.
<jsp:include page = ... >
- This is a kind of a function call from one jsp to another jsp.
- It is executed whenever the client accesses the client page.
- This approach is useful for modularizing of the web applications.
<%@ include file = ... >:
- The content of the included file is textually embedded in the page that has <%@ include file=".."> directive.
- By this the included file will be changed and the changed content can not be included in the output.

Explain the differences between <jsp:forward page = ... > and response.sendRedirect(url).

<jsp:forward page = ... >
- The <jsp:forward> element propagates the request object containing the client request information from one JSP file to another file.
- The target file might be an HTML file, another JSP file, or any servlet, but it must be in the same application context.
response.sendRedirect(url)
- The sendRedirect helps to send HTTP temporary redirect response to the browser, and the browser helps to create a new request for going to the redirected page.
- The response.sendRedirect helps to kill the session variables.

Explain the advantages of JSP.

- JSP is an embedded Java code in HTML pages.
- JSP is Platform independent.
- JSP creates database-driven Web applications.
- JSP enables Server-side programming capabilities.

How to embed the Java code in the HTML pages.

The code below explains it :
< % @ page language = " java " %>
<HTML>
<HEAD><TITLE>RESULT PAGE</TITLE></HEAD>
<BODY>
<%

PrintWriter print = request . get Writer ( ) ;
print . println ( " Welcome buddy !!!" );

%>
</BODY>
</HTML>

What are the implicit Objects available?

- Implicit objects are valid for only JSP pages.
- The web container creates it.
- It comprises the information related to a specific request, page & application.
- The JSP implicit objects are tabulated below :
1. Variable
2. Class
3. Description
- application(javax.servlet.ServletContext) : The context for the JSP page's servlet and any Web components contained in the same application.
- config(javax.servlet.ServletConfig) : Initialization information for the JSP page's servlet.
- exception(java.lang.Throwable) : Accessible only from an error page.
- out(javax.servlet.jsp.JspWriter) : The output stream.
- page(java.lang.Object) : The instance of the JSP page's servlet processing the current request. Not typically used by JSP page authors.
- pageContext(javax.servlet.jsp.PageContext) : The context for the JSP page. Provides a single API to manage the various scoped attributes.
- request(Subtype of javax.servlet.ServletRequest) : The request triggering the execution of the JSP page.
- response(Subtype of javax.servlet.ServletResponse) : The response to be returned to the client. Not typically used by JSP page authors.
- session(javax.servlet.http.HttpSession) : The session object for the client.

What do you mean by JSP o/p Comments?

- JSP Output Comments are visible in HTML source file.
- Code :
<!-- This file displays the user login screen -->
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %> -->

What is expression in the JSP? What is a JSP Scriplet?

- The expression is a tag used for inserting Java values directly into the output.
- The Syntax is given as:
<%= expression %>
- The expression is explained by the code below :
<%
   String userName=null;
   userName=request.getParameter("userName");
%>
- An expression tag comprises of scripting language expressions that is evaluated & converted to a String, and is being inserted where the expression appears in the JSP file.
- The expression tag following displays time on the output:
<%=new java.util.Date()%>
Scriplet :- JSP Scriplet is a jsp tag which is used for enclosing a java code in the JSP pages.
- Scriplet begins with <% tag and ends with %> tag.

What is JSP Declaratives?

- JSP Decleratives are the JSP tags which are used for declaring the variables.
- Declaratives are stored in the <%! %> tag and ends with a semi-colon( ; ).
- The variables and functions reside within the declaration tag and can be used anywhere in the JSP.
- The code of Declaratives :
<%@page contentType="text/html" %>
<html>
<body>

<%!
int cnt=0;
private int getCount()
{
   //increment cnt and return the value
   cnt++;
   return cnt;
}
%>

<p>Values of Cnt are:</p>

<p><%=getCount()%></p>

</body>
</html>

Explain the life-cycle methods of JSP?

- The life-cycle methods of the JSP are detailed below :

jspInit() :
- The container calls the jspInit() for initializing the servlet instance.
- It is called before any other method, and is called only once for a servlet instance.

jspService() :
- The container calls the _jspservice() for each request and passes the request and the response objects.
- jspService() method can not be overridden.

jspDestroy() :
- The container calls jspDestroy() when its instance is about to be destroyed.
What is JSP?
What is JSP? - JavaServerPages is server side component in a web application, used to dynamically generate HTML / XML documents...
Categories of JSP tags - Directives, Scripting elements, Actions
"Categories of JSP tags - Directives: The directive tags are used for simple java programming calls like importing packages, specifying the error handling pages or to handle session in a JSP page...
How a JSP is compiled into servlets by the container
How a JSP is compiled into servlets by the container - JSPs are compiled into JavaServlets by the JSP container...
Post your comment