Thursday, June 24, 2010

JSP CONTINUES...1

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:
To give the bean "request scope", you will have to place it within the request object as:
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