aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Study kit-error attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Study kit-error attributes" Watch "Study kit-error attributes" New topic
Author

Study kit-error attributes

Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
)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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Study kit-error attributes