| Autore |
Messaggio |
a.chiasera
|
Thread
Postato:
2 novembre 2005
Titolo:
Manage errors in Servlets
|
|
|
I have some problems in managing errors in servlets. If an error occurs in a servlet (e.g. it is not possible to connect to the database or some data are not provided properly) I would like to forward the control to an error page (a jsp page) that shows a message to the user describing the error. In that way I can reuse the same error page from all the other servlets customized with the proper error message. The problem is that if I use the RequestDispatcher object and the forward method in some cases I get an error [java.lang.IllegalStateException: Cannot forward after response has been committed] due to the fact that I have already put some output in the response object of the servlet. To solve this problem I have used include instead of forward and to make sure that the execution ends in the case of an error I have put a "return;" after the forwarding operation in the servlet. I do not know if this is the right way to proceed. Thank you. Annamaria.
|
|
|
afogarolli
|
Postato:
3 novembre 2005
Titolo:
Re: Manage errors in Servlets
|
|
|
Hi! I can suggest you to use the servel controller (dispatcher) pattern that you can easly find on the web. We will see this pattern in depth in the future lectures.
Here is an example of code in a dispacher servlet: List stocklist= sm.getPortfolio(userId); request.setAttribute("portfolio", stocklist); request.getRequestDispatcher("/portfolio.jsp").forward(request, response);
...Use the jsp to render the output!!!
Put what you need in the request as an attribute and then pass everthing to the jsp (in your case error.jsp) in order to get all the information to handle the request. Bye
|
|
|
a.chiasera
|
Postato:
3 novembre 2005
Titolo:
Re: Manage errors in Servlets
|
|
|
Thank you for the suggestion! Bye, Annamaria.
|
|
|