Originally posted by Bryce Martin:
Should I have a separate servlet to handle my database call?
I wouldn't handle database calls from the servlet.
Ideally, your model objects should be able to work independently from the servlets and JSPs. For me, making sure they do, greatly speeds up, both the development and, later,
testing and debugging of these classes.
They speed up development becuase I can give them a main method and make sure the database access and business logic is right from the command line before I start with the servlets and JSPs. It will pay dividends later on too, because you it will be really easy to write tests that can be run from
jUnit without having to simulate user interaction from a browser.
A simple rule of thumb (if you don't have separate DAO layer) is not to import javax.servlet... and java.sql (or javax.sql) from within the same classes.
For instance, suppose you need a method that returns a list of your myclass objects. If your model object (call it "myModel") was independent of the servlet and JSP stuff, you could give it a main method that looked something like:
Now, you can hammer out all of your business logic right at the
command line (or
IDE) before worrying about how to integrate it
with the web stuff.
If you have a test database with mock data, you can write tests
that call this method right from your build script to test all
of this logic every time you compile your app. Lastly (and this
has been a big time saver for me) when the pages stop working
from the browser, as they inevitably will), you can quickly
determine whether a change in the database tier or in the web
tier is causing the problem.
[ November 20, 2007: Message edited by: Ben Souther ]