JSP Interview Questions with Answers Page II
From freshersonline.com
1. How to Retrieve Warnings?
SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop
the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned.
A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement
objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see
the first warning reported on the calling object
SQLWarning warning = stmt.getWarnings();
if (warning != null)
{
while (warning != null) {
System.out.println(\"Message: \" + warning.getMessage());
System.out.println(\"SQLState: \" + warning.getSQLState());
System.out.print(\"Vendor error code: \"); System.out.println(warning.getErrorCode()); warning = warning.getNextWarning();
}
}
2. How many JSP scripting elements are there and what are they?
There are three scripting language elements: declarations, scriptlets, expressions.
3. In the Servlet 2.4 specification SingleThreadModel has been deprecated, why?
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.
4. What are stored procedures? How is it useful?
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 everytime 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.
5. How do I include static files within a JSP page?
Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during
the translation phase. Do note that you should always supply a relative URL for the file attribute. Although you can also include
static resources using the action, this is not advisable as the inclusion is then performed for each and every request.
6. Why does JComponent have add() and remove() methods but Component does not?
because JComponent is a subclass of Container, and can contain other components and jcomponents. How can I implement a thread-safe
JSP page? - You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the
directive <%@ page isThreadSafe="false" % > within your JSP page.
7. How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached
by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the
browser. You need both the statements to take care of some of the older browser versions.
8. How do you restrict page errors display in the JSP page?
You first set "Errorpage" attribute of PAGE directory to the name of the error page (ie Errorpage="error.jsp")in your jsp page .
Then in the error jsp page set "isErrorpage=TRUE". When an error occur in your jsp page it will automatically call the error page.
9. What JSP lifecycle methods can I override?
You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods
within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for
the JSP page. It is good programming practice to free any allocated resources within jspDestroy().
The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as
JSP declarations.
10. How do I perform browser redirection from a JSP page?
You can use the response implicit object to redirect the browser to a different resource, as:
response.sendRedirect("http://www.freshersonline.com/path/error.html");
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 >
11. How does JSP handle run-time exceptions?
You can use the errorPage attribute of the page directive to have uncaught runtime exceptions automatically forwarded to an error
processing page.For example:
redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp,
if you indicate that it is an error-processing page, via the directive:
the Throwable object describing the exception may be accessed within the error page via the exception implicit object.
Note: You must always use a relative URL as the value for the errorPage attribute.
12.How do I use comments within a JSP page?
You can use "JSP-style" comments to selectively block out code while debugging or simply to comment your scriptlets. JSP comments
are not visible at the client.
For example:
--%>
You can also use HTML-style comments anywhere within your JSP page. These comments are visible at the client. For example:
Of course, you can also use comments supported by your JSP scripting language within your scriptlets.
13. 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?
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.
14. How can I implement a thread-safe JSP page?
You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page
isThreadSafe="false" % > within your JSP page.
15. How can I declare methods within my JSP page?
You can declare methods for use within your JSP page as declarations. The methods can then be invoked within any other methods you
declare, or within JSP scriptlets and expressions.
Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from
within JSP methods. However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare.
For example:
file1.jsp:
file2.jsp
<%test(out);% >
