• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Study kit-error attributes

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic