JAVA JSP FREQUENTLY ASKED QUESTIONS AND ANSWERS-2
Question:
Can I stop JSP execution while in the midst of processing a
request?
Answer:
Yes. Preemptive termination of request processing on an error condition is a
good way to maximize the throughput of a high-volume JSP engine. The trick
(asuming Java is your scripting language) is to use the return statement when
you want to terminate further processing. For example, consider:
<% if (request.getParameter("foo") != null) {
// generate some html or update bean property
} else {
/* output some error message or provide redirection back to
the input form after creating a memento bean updated with the 'valid' form
elements that were input. this bean can now be used by the previous form to
initialize the input elements that were valid then, return from the body of the
_jspService() method to terminate further processing */
return;
}
%>
Question:
How can I get to view any compilation/parsing errors at the client while
developing JSP pages?
Answer:
With JSWDK 1.0, set the following servlet initialization property within the
\WEB-INF\servlets.properties file for your application:
jsp.initparams=sendErrToClient=true
This will cause any compilation/parsing errors to be sent
as part of the response to the client.
Question:
Is there a way to reference the "this" variable within a JSP
page?
Answer:
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.
Question:
How do I instantiate a bean whose constructor accepts parameters using the
useBean tag?
Answer:
Consider the following bean: package bar;
public class FooBean {
public FooBean(SomeObj arg) {
...
}
//getters and setters here
}
The only way you can instantiate this bean within your JSP
page is to use a scriptlet. For example, the following snippet creates the bean
with session scope:
&l;% SomeObj x = new SomeObj(...);
bar.FooBean foobar = new FooBean(x);
session.putValue("foobar",foobar);
%> You can now access this bean within any other page
that is part of the same session as: &l;%
bar.FooBean foobar = session.getValue("foobar");
%>
To give the bean "application scope", you will
have to place it within the ServletContext as:
&l;%
application.setAttribute("foobar",foobar);
%>
To give the bean "request scope", you will have
to place it within the request object as:
&l;%
request.setAttribute("foobar",foobar);
%>
If you do not place the bean within the request, session or
application scope, the bean can be accessed only within the current JSP page
(page scope).
Once the bean is instantiated, it can be accessed in the
usual way:
jsp:getProperty name="foobar"
property="someProperty"/ jsp:setProperty name="foobar"
property="someProperty" value="someValue"/
Question:
Can I invoke a JSP error page from a servlet?
Answer:
Yes, you can invoke the JSP error page and pass the exception object to it from
within a servlet. The trick is to create a request dispatcher for the JSP error
page, and pass the exception object as a javax.servlet.jsp.jspException request
attribute. However, note that you can do this from only within controller
servlets. If your servlet opens an OutputStream or PrintWriter, the JSP engine
will throw the following translation error:
java.lang.IllegalStateException: Cannot forward as
OutputStream or Writer has already been obtained
The following code snippet demonstrates the invocation of a
JSP error page from within a controller servlet:
protected void sendErrorRedirect(HttpServletRequest
request, HttpServletResponse response, String errorPageURL, Throwable e) throws
ServletException, IOException {
request.setAttribute
("javax.servlet.jsp.jspException", e);
getServletConfig().getServletContext().
getRequestDispatcher(errorPageURL).forward(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
try {
// do something
} catch (Exception ex) {
try {
sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Question:
Can we implement an interface in a JSP?
Answer:
No
Question:
What is the difference between ServletContext and PageContext?
Answer:
ServletContext: Gives the information about the container
PageContext: Gives the information about the Request
Question:
What is the difference in using request.getRequestDispatcher() and
context.getRequestDispatcher()?
Answer:
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.
Question:
How to pass information from JSP to included JSP?
Answer:
Using <%jsp:param> tag.
Question:
What is the difference between directive include and jsp include?
Answer:
<%@ include> : Used to include static resources during translation time.
: Used to include dynamic content or static content during
runtime.