You are on page 1of 5

JSP INTERVIEW QUESTIONS AND ANSWERS

1) Briefly explain about Java Server Pages technology?


JavaServer Pages (JSP) technology provides a simplified, fast way to
create web pages that display dynamically-generated content. The JSP
specification, developed through an industry-wide initiative led by Sun
Microsystems, defines the interaction between the server and the JSP
page, and describes the format and syntax of the page.
2)
What
is
a
JSP
Page?
A JSP page is a text document that contains two types of text: static data,
which can be expressed in any text-based format (such as
HTML,WML,XML,etc), and JSP elements, which construct dynamic
content.JSP is a technology that lets you mix static content with
dynamically-generated
content.
3) Why do I need JSP technology if I already have servlets?
JSP pages are compiled into servlets, so theoretically you could write
servlets to support your web-based applications. However, JSP technology
was designed to simplify the process of creating pages by separating web
presentation from web content. In many applications, the response sent to
the client is a combination of template data and dynamically-generated
data. In this situation, it is much easier to work with JSP pages than to do
everything
with
servlets.
4)
How
are
the
JSP
requests
handled?
The following sequence of events happens on arrival of jsp request:
a. Browser requests a page with .jsp file extension in webserver.
b.
Webserver
reads
the
request.
c. Using jsp compiler,webserver converts the jsp into a servlet class that
implement the javax.servletjsp.jsp page interface.the jsp file compiles only
when the page is first requested or when the jsp file has been changed.
d. The generated jsp page servlet class is invoked to handle the browser
request.
e. The response is sent to the client by the generated servlet.
5)
What
are
the
advantages
of
JSP?
Ans : The following are the advantages of using JSP:
a. JSP pages easily combine static templates, including HTML or XML
fragments,
with
code
that
generates
dynamic
content.
b. JSP pages are compiled dynamically into servlets when requested, so
page authors can easily make updates to presentation code. JSP pages
can
also
be
precompiled
if
desired.

c. JSP tags for invoking JavaBeans components manage these


components completely, shielding the page author from the complexity of
application
logic.
d. Developers can offer customized JSP tag libraries that page authors
access
using
an
XML-like
syntax.
e. Web authors can change and edit the fixed template portions of pages
without affecting the application logic. Similarly, developers can make logic
changes at the component level without editing the individual pages that
use
the
logic.
6)
How
is
a
JSP
page
invoked
and
compiled?
Pages built using JSP technology are typically implemented using a
translation phase that is performed once, the first time the page is called.
The page is compiled into a Java Servlet class and remains in server
memory, so subsequent calls to the page have very fast response times.
7)
What
are
Directives?
Directives are instructions that are processed by the JSP engine when the
page is compiled to a servlet. Directives are used to set page-level
instructions, insert data from external files, and specify custom tag
libraries. Directives are defined between < %@ and % >.
< %@ page language=="java" imports=="java.util.*" % >
<
%@
include
file=="banner.html"
%
>
8) What are the different types of directives available in JSP?
Ans : The following are the different types of directives:
a. include directive : used to include a file and merges the content of the
file
with
the
current
page
b. page directive : used to define page specific attributes like scripting
language,
error
page,
buffer,
thread
safety,
etc
c. taglib : used to declare a custom tag library which is used in the page.
9)
What
are
JSP
actions?
JSP actions are executed when a JSP page is requested. Action are
inserted in the jsp page using XML syntax to control the behavior of the
servlet engine. Using action, we can dynamically insert a file, reuse bean
components, forward the user to another page, or generate HTML for the
Java plugin. Some of the available actions are as follows:
<jsp:include> - include a file at the time the page is requested.
<jsp:useBean>
find
or
instantiate
a
JavaBean.
<jsp:setProperty>
set
the
property
of
a
JavaBean.
<jsp:getProperty> - insert the property of a JavaBean into the output.
<jsp:forward> - forward the requester to a new page.

<jsp:plugin> - generate browser-specific code that makes an OBJECT or


EMBED
tag
for
the
Java
plugin.
10)
What
are
Scriptlets?
Scriptlets are blocks of programming language code (usually java)
embedded within a JSP page. Scriptlet code is inserted into the servlet
generated from the page. Scriptlet code is defined between <% and %>
11)
What
are
Decalarations?
Declarations are similar to variable declarations in Java.Variables are
defined for subsequent use in expressions or scriptlets. Declarations are
defined
between
<%!
and
%>.
<
%!
int
i=0;
%>
12) How to declare instance or global variables in jsp?
Instance variables should be declared inside the declaration part. The
variables declared with JSP declaration element will be shared by all
requests
to
the
jsp
page.
< %@ page language=="java" contentType="text/html"% >
<
%!
int
i=0;
%>
13)
What
are
Expressions?
Expressions are variables or constants that are inserted into the data
returned by the web server. Expressions are defined between <% = and %
>
<
%=
scorer.getScore()
%>
14) What is meant by implicit objects? And what are they?
Ans : Implicit objects are those objects which are avaiable by default.
These objects are instances of classesdefined by the JSP specification.
These objects could be used within the jsp page without being declared.
The
1.
2.
3.
4.
5.
6.
7.
8.
9.

following

are

the
implicit
application
page
request
response
session
exception
out
config
pageContext

jsp

objects:

15) How do I use JavaBeans components (beans) from a JSP page?


The JSP specification includes standard tags for bean use and
manipulation. The <jsp:useBean> tag creates an instance of a specific
JavaBean class. If the instance already exists, it is retrieved. Otherwise, a
new instance of the bean is created. The <jsp:setProperty> and
<jsp:getProperty> tags let you manipulate properties of a specific bean.
16) What is the difference between <jsp:include> and <%@include:>
Both
are
used
to
insert
files
into
a
JSP
page.
<%@include:> is a directive which statically inserts the file at the time the
JSP
page
is
translated
into
a
servlet.
<jsp:include> is an action which dynamically inserts the file at the time
the
page
is
requested.
17) What is the difference between forward and sendRedirect?
Both requestDispatcher.forward() and response.sendRedirect() is used to
redirect
to
new
url.
forward is an internal redirection of user request within the web container
to a new URL without the knowledge of the user(browser). The request
object
and
the
http
headers
remain
intact.
sendRedirect is normally an external redirection of user request outside
the web container. sendRedirect sends response header back to the
browser with the new URL. The browser send the request to the new URL
with fresh http headers. sendRedirect is slower than forward because it
involves
extra
server
call.
18) Can I create XML pages using JSP technology?
Yes, the JSP specification does support creation of XML documents. For
simple XML generation, the XML tags may be included as static template
portions of the JSP page. Dynamic generation of XML tags occurs through
bean components or custom tags that generate XML output.
19) How is Java Server Pages different from Active Server Pages?
JSP is a community driven specification whereas ASP is a similar
proprietary technology from Microsoft. In JSP, the dynamic part is written
in Java, not Visual Basic or other MS-specific language. JSP is portable to
other operating systems and non-Microsoft Web servers whereas it is not
possible
with
ASP
20) Can't Javascript be used to generate dynamic content rather than

JSP?
JavaScript can be used to generate dynamic content on the client browser.
But it can handle only handles situations where the dynamic information is
based on the client's environment. It will not able to harness server side
information directly.

You might also like