This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
1. if I ONLY type <html:error/> in each of my JSP pages without defining <global-exception> in the xml file, will I still be able to see all the error messages ?
2. Is it true that the only thing <global-exception> setting can buy me is to allow me to dispatch to customized different error pages for different exceptions (the exception can be simple ServletException) ? Then in this case, what's usage of <html:errors/> since the error page will be used anyway ?
3. In javax servlet API we have a servlet.log() method, since we use struts, can we still use this "servlet.log()" method ? and how ? I thought we can't because we usually don't touch servlet by ourselves any more..
1. If you only put <html:errors /> in your page without defining global exceptions, the only mesages that will appear are ones that you add using the ActionMessages add() method for that specific acion. Struts will not put any exceptions there, nor forward back to your page if an exception occurs. Any unhandled exceptions will result in Struts using sending a generic error page to the browser.
2. If you want the error page to show a generic message such as "something went wrong", then there's no need to use <html:errors> in your error page. If you want to inform the user of the specific exception that was thrown, then use <html:errors /> to show the specific exception that ocurred. Most web developers will agree, however, that it's not generally a good idea to send java exception messages to users of a website. It give the impression of an amateurish site, and often makes the user worry that there's more going wrong than there actually is.
3. The struts Action class defines a getServlet() method that will return the servlet that called the action. You can then call the log() method on the servlet. There are more sophisticated logging mechanisms available through the apache.commons.logging package that comes with Struts.
And in generalerror.jsp I do not need to retrieve any specific error type, etc as the "key" will output corresponding error msg for me. Is that right ?
One more question --- Since I use both "Global exception" and <html:errors/>, when SQLException rises, will it follow the global exception's generalerror.jsp or follow <html:errors/> to a white page ? any conflict here ?
Plus, no matter what I define, for a specifc Action, if I just want to treat SQLException in "that" Action class differently, I can still choose to catch it in the class and handle it differently, right ? In this case, the global-exception defined for SQLException will be used for all the SQLExceptions occured in other Action classes, right ?
Joined: Feb 15, 2005
The way it works is that you code a <html:errors/> tag inside the generalerror.jsp. When the Struts ActionServlet catches an exception that you haven't caught in your Action's execute() method, it will forward to the generalerror.jsp page, and the error message you define by the key attribute will be rendered by the <html:errors/> tag.
Struts Exception handling only applies to un-caught exceptions, so naturally if you want to handle an exception before Struts does, just include it in a catch block in your execute() method. That way one action can handle an SQLException while another doesn't handle it and passes it on to Struts to handle. [ July 01, 2005: Message edited by: Merrill Higginson ]