Two Laptop Bag*
The moose likes JSP and the fly likes Bean that throws exceptions are not handled in JSP[solved] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Bean that throws exceptions are not handled in JSP[solved]" Watch "Bean that throws exceptions are not handled in JSP[solved]" New topic
Author

Bean that throws exceptions are not handled in JSP[solved]

Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
I have a clarification to be made...
Assume there is a bean like this...


You can see the method moveNext() throws an exception but it is not catched/handled in JSP...I have a few book examples like the above where they throw Exceptions but not caught in JSP like



Is this how programmers normally code or there is a bad practice with the book examples..pls clarify...
[ January 04, 2006: Message edited by: Raghavan Chockalingam ]

Raghavan
SCJP 6
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What book is that from?

These days, it's considered bad form to do any real processing in a JSP.
Best practices dictate that all non-view code be factored into Servlets or beans. Once the processing is done, context should then be forwarded to the JSP for formatting and display only.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61307
    
  66

Originally posted by Ben Souther:

These days, it's considered bad form to do any real processing in a JSP.


I quote this for emphasis.

And I'll state it more strongly: in this day and time, if you are doing processing in your JSP -- either directly with scriptlets, or indirectly through calling processing beans from the JSP -- you are doing it wrong.

With the advent of JSP 2.0 there is no longer any excuse for badly structured web applications that do not follow the controller-to-view model.

When you structure your web application correctly, a whole host of sticky and icky problems (like the one you mention) magically just go away.
[ January 03, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
The title of the book is "JSP Weekend crash course" but in "Beginning JSP" from Wrox they are catching and throwing errors for the same method....why one has to do both of them???
They have something like...
Raghavan Chockalingam
Ranch Hand

Joined: Dec 20, 2005
Posts: 77
Bean and Bear, I have written bean something like this to make operations on a Forums_tbl in my database. I would make calls to this method from the JSP page...do you think I have made errors somewhere...I mean to say that I have followed any bad practice??? Please forgive places where I have not handled exceptions cos earlier I was following some book and I am going to change them now....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61307
    
  66

Originally posted by Raghavan Chockalingam:
The title of the book is "JSP Weekend crash course" but in "Beginning JSP" from Wrox they are catching and throwing errors for the same method....why one has to do both of them???
They have something like...


Sometimes you see SQLExceptions propogate outward because:
a) They should be handled by the application-defined error handlers, not the code
b) closing a connection or other resource can throw a SQLException that the author doesn't want to deal with
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61307
    
  66

If you are really interrested in following modern-day best practices, you should look up the terms "MVC" and "Model 2". These outline a web application structure in which all processing takes place under the control of a servlet controller prior to forwarding to a JSP page to render the view.

In that way, any issues are ususally dealt with under the control of Java code and by the time the JSP gains control, there's nothing left to do but display the data.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Raghavan Chockalingam:
The title of the book is "JSP Weekend crash course" but in "Beginning JSP" from Wrox
[/CODE]


It's important to look at the date on a JSP book.
Servlets have seen little change over the last five years but JSP has changed drastically.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Bear Bibeault:
If you are really interrested in following modern-day best practices, you should look up the terms "MVC" and "Model 2". These outline a web application structure in which all processing takes place under the control of a servlet controller prior to forwarding to a JSP page to render the view.

In that way, any issues are ususally dealt with under the control of Java code and by the time the JSP gains control, there's nothing left to do but display the data.


In addition, you might want to look at container managed connection pooling.
Tomcat implements this with Jakarta DBCP
http://jakarta.apache.org/commons/dbcp/
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
 
GeeCON Prague 2014
 
subject: Bean that throws exceptions are not handled in JSP[solved]