This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing exception in MVC model from servlet to JSP
 
Similar Threads
Disadvantages of servlet?
Difference MVC2 vs MVC ???
Is this MVC?
In MVC1 the controller is JSP where as in MVC2 the controller is servlet
Servlet, JSP, MySQL webb app