This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes passing exception in MVC model from servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing exception in MVC model from servlet to JSP" Watch "passing exception in MVC model from servlet to JSP" New topic
Author

passing exception in MVC model from servlet to JSP

Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
Hi all,
I'm using IBM implementation of MVC, and I want to handle exception thrown in the model part as follows:
1- Any exception thrown in the model part(business logic) is logged in files, and thrown to the caller function and so on.
2- Untill it reaches the Handler, which detect that exception was thrown, and redirect to errorpage.
3- I want to display the error message of the exception in the JSP, which has already the directive <%@ page isErrorPage="true" %>
I already done 1, 2, but I don't know how to pass the exception from Servlet to JSP.
anyone can help


Ashraf Fouad
SCJP 1.2, SCBCD 5
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
Your servlet could place a bean with the error message as an attribute and place it within the request:
__________________________________________________
//msg is a string with the exception message
ErrorControlBean ecb = new ErrorControlBean();
ecb.setMessage(msg);
request.setAttribute("errorBean", ecb);
// do whatever else necessary
getServletConfig().getServletContext().getRequestDispatcher("/jsp/errorpage.jsp").forward(request, response);
__________________________________________________
then your errorpage.jsp can extract the bean and its properties:
__________________________________________________
<jsp:useBean id="errorBean" class="ErrorControlBean"
scope="request"/>
<jsp:getProperty name="errorBean" property="message" />
__________________________________________________

--HTH
Juanjo
[ June 17, 2002: Message edited by: Juanjo Bazan ]
Ashraf Fouad
Ranch Hand

Joined: Oct 07, 2001
Posts: 80
yes this will work, but I wanted to use the implicit object in the error which is defined as error page
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
If you set an attribute in the request with a key of "javax.servlet.jsp.jspException", then it will be available through the implicit 'exception' JSP variable.
For example,
request.setAttribute("javax.servlet.jsp.jspException", exception);
Regards,
Erick
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
If you set an attribute in the request with a key of "javax.servlet.jsp.jspException", then it will be available through the implicit 'exception' JSP variable.
For example,
request.setAttribute("javax.servlet.jsp.jspException", exception);
Regards,
Erick
 
 
subject: passing exception in MVC model from servlet to JSP