Select Category 
 

JSP Interview Questions Answers

JSP Interview Question - 1 : -

What are stored procedures? How is it useful?

JSP Interview Answer - 1 : -

A stored procedure is a set of statements/commands which reside in the database. The stored procedure is pre-compiled and saves the database the effort of parsing and compiling sql statements every time a query is run. Each database has its own stored procedure language, usually a variant of C with a SQL preproceesor. Newer versions of db’s support writing stored procedures in Java and Perl too. Before the advent of 3-tier/n-tier architecture it was pretty common for stored procs to implement the business logic( A lot of systems still do it). The biggest advantage is of course speed. Also certain kind of data manipulations are not achieved in SQL. Stored procs provide a mechanism to do these manipulations. Stored procs are also useful when you want to do Batch updates/exports/houseKeeping kind of stuff on the db. The overhead of a JDBC Connection may be significant in these cases.
 

JSP Interview Question - 2 : -

In the Servlet 2.4 specification SingleThreadModel has been deprecated, why?

JSP Interview Answer - 2 : -

Because it is not practical to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.
 

JSP Interview Question - 3 : -

How does JSP handle runtime exceptions?

JSP Interview Answer - 3 : -

Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.
 

JSP Interview Question - 4 : -

How can a servlet refresh automatically if some new data has entered the database?

JSP Interview Answer - 4 : -

You can use a client-side Refresh or Server Push.
 

JSP Interview Question - 5 : -

How do I perform browser redirection from a JSP page?

JSP Interview Answer - 5 : -

You can use the response implicit object to redirect the browser to a different resource, as:
response.sendRedirect("http://exammaterial.com");
You can also physically alter the Location HTTP header attribute, as shown below:
You can also use the:
Also note that you can only use this before any output has been sent to the client. I beleve this is the case with the response.sendRedirect() method as well. If you want to pass any paramateres then you can pass using >
 

JSP Interview Question - 6 : -

How many JSP scripting elements are there and what are they?

JSP Interview Answer - 6 : -

There are three scripting language elements: declarations, scriptlets, expressions.
 

JSP Interview Question - 7 : -

Can we implement an interface in a JSP?

JSP Interview Answer - 7 : -

No
 

JSP Interview Question - 8 : -

How do you connect to the database from JSP?

JSP Interview Answer - 8 : -

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 following way.
 

JSP Interview Question - 9 : -

What are the two kinds of comments in JSP and what's the difference between them ?

JSP Interview Answer - 9 : -

<%-- JSP Comment --%>
<!-- HTML Comment -->
 

JSP Interview Question - 10 : -

Can you make use of a ServletOutputStream object from within a JSP page?

JSP Interview Answer - 10 : -

No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients.
A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not.
A page author can always disable the default buffering for any page using a page directive as:
 

JSP Interview Question - 11 : -

Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?

JSP Interview Answer - 11 : -

You can pass the HttpSession as parameter to an EJB method, only if all objects in session are serializable. This has to be consider as "passed-by-value", that means that it's read-only in the EJB.
If anything is altered from inside the EJB, it won't be reflected back to the HttpSession of the Servlet Container.The "pass-byreference" can be used between EJBs Remote Interfaces, as they are remote references.
While it IS possible to pass an HttpSession as a parameter to an EJB object, it is considered to be "bad practice" in terms of object oriented design. This is because you are creating an unnecessary coupling between back-end objects (ejbs) and front-end objects (HttpSession). Create a higher-level of abstraction for your ejb's api. Rather than passing the whole, fat, HttpSession (which carries with it a bunch of http semantics), create a class that acts as a value object (or structure) that holds all the data you need to pass back and forth between front-end/back-end.
Consider the case where your ejb needs to support a non-http-based client. This higher level of abstraction will be flexible enough to support it.
 

JSP Interview Question - 12 : -

The code in a finally clause will never fail to execute, right?

JSP Interview Answer - 12 : -

Using System.exit(1); in try block will not allow finally code to execute.
 

JSP Interview Question - 13 : -

How can my application get to know when a HttpSession is removed?

JSP Interview Answer - 13 : -

Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound() method.
Create an instance of that class and put that instance in HttpSession.
 

JSP Interview Question - 14 : -

Is there a way to reference the "this" variable within a JSP page?

JSP Interview Answer - 14 : -

Yes, there is. Under JSP 1.0, the page implicit object is equivalent to "this", and returns a reference to the Servlet generated by the JSP page.
 

JSP Interview Question - 15 : -

How does a servlet communicate with a JSP page?

JSP Interview Answer - 15 : -

The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser. The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.
public void doPost (HttpServletRequest request, HttpServletResponse response)
{
try {
govi.FormBean f = new govi.FormBean();
String id = request.getParameter("id");
f.setName(request.getParameter("name"));
f.setAddr(request.getParameter("addr"));
f.setAge(request.getParameter("age"));

//use the id to compute
//additional bean properties like info
//maybe perform a db query, etc.
// . . .

f.setPersonalizationInfo(info);
request.setAttribute("fBean",f);
getServletConfig().getServletContext().getRequestDispatcher
("/jsp/Bean1.jsp").forward(request, response);
} catch (Exception ex) {
. . .
}
}

The JSP page Bean1.jsp can then process fBean, a
fter first extracting it from the default request
scope via the useBean action.

jsp:useBean id="fBean" class="govi.FormBean" scope="request"/ jsp:getProperty
name="fBean" property="name" / jsp:getProperty name="fBean" property="addr"
/ jsp:getProperty name="fBean" property="age" / jsp:getProperty name="fBean"
property="personalizationInfo" /