JSP Interview Questions and Answers
How do I use a scriptlet to initialize a
newly instantiated bean?
A jsp:useBean action may optionally have a body. If the body is
specified, its contents will be automatically invoked when the specified
bean is instantiated. Typically, the body will contain scriptlets or
jsp:setProperty tags to initialize the newly instantiated bean, although
you are not restricted to using those alone.
The following example shows the "today" property of the Foo bean
initialized to the current date when it is instantiated. Note that here,
we make use of a JSP expression within the jsp:setProperty action.
How can I set a cookie and delete a cookie from within a JSP page?
A cookie, mycookie, can be deleted using the following scriptlet:
How do you connect to the database from JSP?
A Connection to a database can be established from a jsp page by writing
the code to establish a connection using a jsp scriptlets.
Further then you can use the resultset object "res" to read data in the
What is the page directive is used to prevent a JSP page from
automatically creating a session?
<%@ page session="false">
How do you delete a Cookie within a JSP?
Cookie mycook = new Cookie("name","value");
Cookie killmycook = new Cookie("mycook","value");
Can we implement an interface in a JSP?
What is the difference between ServletContext and PageContext?
ServletContext: Gives the information about the container
PageContext: Gives the information about the Request
What is the difference in using request.getRequestDispatcher() and
request.getRequestDispatcher(path): In order to create it we need to
give the relative path of the resource context.getRequestDispatcher(path):
In order to create it we need to give the absolute path of the resource.
How to pass information from JSP to included JSP?
Using <%jsp:param> tag.
How is JSP include directive different from JSP include action. ?
When a JSP include directive is used, the included file's code is added
into the added JSP page at page translation time, this happens before
the JSP page is translated into a servlet. While if any page is included
using action tag, the page's output is returned back to the added page.
This happens at runtime.
Can we override the jspInit(), _jspService() and jspDestroy() methods?
We can override jspinit() and jspDestroy() methods but not _jspService().
Why is _jspService() method starting with an '_' while other life cycle
methods do not?
_jspService() method will be written by the container hence any methods
which are not to be overridden by the end user are typically written
starting with an '_'. This is the reason why we don't override _jspService()
method in any JSP page.
What happens when a page is statically included in another JSP page?
An include directive tells the JSP engine to include the contents of
another file (HTML, JSP, etc.) in the current page. This process of
including a file is also called as static include.
A JSP page, include.jsp, has a instance variable "int a", now this page
is statically included in another JSP page, index.jsp, which has a
instance variable "int a" declared. What happens when the index.jsp page
is requested by the client?
Compilation error, as two variables with same name can't be declared.
This happens because, when a page is included statically, entire code of
included page becomes part of the new page. at this time there are two
declarations of variable 'a'. Hence compilation error.
Page Numbers : 1