JAVA JSP FREQUENTLY ASKED QUESTIONS AND ANSWERS-3
Question:
What is the difference between RequestDispatcher and sendRedirect?
Answer:
RequestDispatcher: server-side redirect with request and response objects.
sendRedirect : Client-side redirect with new request and
response objects.
Question:
How does JSP handle runtime exceptions?
Answer:
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.
Question:
How do you delete a Cookie within a JSP?
Answer:
Cookie mycook = new
Cookie("name","value"); response.addCookie(mycook); Cookie
killmycook = new Cookie("mycook","value");
killmycook.setMaxAge(0);
killmycook.setPath("/");
killmycook.addCookie(killmycook);
Question:
How do I mix JSP and SSI #include?
Answer:
If you're just including raw HTML, use the #include directive as usual inside
your .jsp file.
<!--#include file="data.inc"-->
But it's a little trickier if you want the server to
evaluate any JSP code that's inside the included file. Ronel Sumibcay
(ronel@LIVESOFTWARE.COM) says: If your data.inc file contains jsp code you will
have to use
<%@ vinclude="data.inc" %>
The <!--#include file="data.inc"--> is used
for including non-JSP files.
Question:
How can you store international / Unicode characters into a cookie?
Answer:
One way is, before storing the cookie URLEncode it.
URLEnocder.encoder(str);
And use URLDecoder.decode(str) when you get the stored
cookie.
Question:
What are the implicit objects?
Answer:
Implicit objects are objects that are created by the web container and contain
information related to a particular request, page, or application. They are:
request
response
pageContext
session
application
out
config
page
exception
Question:
Is JSP technology extensible?
Answer:
YES. JSP technology is extensible through the development of custom actions, or
tags, which are encapsulated in tag libraries
Question:
How can I implement a thread-safe JSP page? What are the advantages and
Disadvantages of using it?
Answer:
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.
With this, instead of a single instance of the servlet
generated for your JSP page loaded in memory, you will have N instances of the
servlet loaded and initialized, with the service method of each instance
effectively synchronized. You can typically control the number of instances (N)
that are instantiated for all servlets implementing SingleThreadModel through
the admin screen for your JSP engine.
More importantly, avoid using the tag for variables. If you
do use this tag, then you should set isThreadSafe to true, as mentioned above.
Otherwise, all requests to that page will access those variables, causing a
nasty race condition.
SingleThreadModel is not recommended for normal use. There
are many pitfalls, including the example above of not being able to use . You
should try really hard to make them thread-safe the old fashioned way: by
making them thread-safe
Question:
How does JSP handle run-time exceptions?
Answer:
You can use the errorPage attribute of the page directive to have uncaught
run-time exceptions automatically forwarded to an error processing page. For
example:
<%@ page errorPage="error.jsp" %>
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:
<%@ page isErrorPage="true" %>
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.
Question:
How do I prevent the output of my JSP or Servlet pages from being cached by the
browser?
Answer:
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.
<%
response.setHeader("Cache-Control","no-store");
//HTTP 1.1
response.setHeader("Pragma","no-cache");
//HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents
caching at the proxy server
%>