This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
The best practice is not to connect to a database from within a JSP.
It is better to build your business logic (including database access) in plain old Java objects 'POJOs' that can be accessed from any interface. Then use a servlet to interact with those classes. One all of this interaction is done and you've got your data in a simple datastructure (an arraylist of beans for example) forward to the JSP.
A search on the web for "MVC" will bring up plenty of information for you to explore.
Never connect to database directly from jsp. Create dao for data access. Create services and let services use dao to perform data base operations. Let servlets use services and put the data in appropriate scope and forward to jsp. Let the jsp display data.
I want to say, never put business logic in jsp pages, use jsp only for displaying data. let servlets or other helper classes do the actual processing, connect to database and make data available to jsp for display.
It applies to pure jsp/servlet based application as well as to applications developed with other web frameworks like struts,webwork etc in this case it will be action classes insted of servlets which access services, also the way you make data available to jsp will differ.
If you must access data from jsp, use custom tags or helper classes. [ November 16, 2007: Message edited by: sudhir nim ]
question about performance. Does it take any longer to pass from the dao to the beans and onto the jsp rather than just doing it all from the jsp using a resultset? I'm just curious to any performance gains or losses from one method to the other