• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Study kit-error attributes

 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
)The following example is from study kit

we use a RequestDispatcher to include or forward a request to another
resource. We can also use it to forward a request to an error page. The following code
for the doPost() method illustrates this approach:
public void doPost(HttpServletRequest req,
HttpServletResponse res)
{
String command = req.getParameter("command");
if("debit".equals(command))
{
double amount =
Double.parseDouble(req.getParameter("amount"));
String accountId =
req.getSession().getAttribute("accountId");
try
{
double newBalance = debit(accountId, amount);
}
catch(InsufficientFundsException isfe)
{
req.setAttribute("javax.servlet.error.exception", isfe);
req.setAttribute("javax.servlet.error.request_uri",
req.getRequestURI());
req.setAttribute("javax.servlet.error.servlet_name",
req.getServletName());
RequestDispatcher rd = req.getRequestDispatcher(
"/servlet/BusinessLogicExceptionHandlerSerlvet");
rd.forward(req, res);
return;
}
//generate HTML page showing new balance.
}
else
{
//do something else
}
}

Do we need to explicitly set these attributes in the catch block above.Wont it be set automatically by the container?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They will be set by the container is the error has been handled by the container. For example, an uncaught exception. Here, you explicitly forward to the error servlet, so the container will do nothing else than forwarding the request.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic