This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am just starting out with some basic servlets.
I want to connect them up with a MySQL student database i also have running on my computer. The database is up and running fine and i have tested the connection.
I have a .xhtml page with radio buttons to add, delete or display students in the database. Currently i have a servlet handling the response and redirecting to three different pages.
A few questions after trying to achieve this.
Should i use multiple servlets for this? I have one that handles the request from the first form, with the radio buttons deciding which page to redirect to.
I was thinking i could then use one for the delete and one for the add form? This would just involve putting their entries into the Web.xml file?
Also what is the best way to connect the servlet with the database? I have a slimmed down java class that i used with my MySQL database. It connects with the database and adds, deletes, displays results using sql statements.
In my servlet i use the methods within this class to perform the operations, is this the best way to connect?
If it is then i encounter a problem when trying to display the results. My operations class method creates a result set and uses a println to display each row in the database, how would i get this to output form the servlet?
In many ways, doing the DB access in the control layer, automatically complicate things. When the controllers only have to worry about control, rather than (all too often, usurping business and model logic), they become almost trivial.
If you want to continue the way you are going, I'd just follow high-level guidelines: do what increases clarity, don't do any logic in the JSPs, and so on. I don't have specific recommendations except for trying to adhere to MVC as much as possible even if you're only doing VC. (You're sort of asking "what's the best practice way to apply this poor practice?")
I think the one servlet to handle multiple operations is fine as long as
- the operations are trivial
- the operations are logically related.
In this case for CRUD operations on your business object, they are definitely logically related.
You say you created a seperate class which invoked the database, so your servlet would pretty much be just gather request parameters, and invoke that Data access class.
With what you have described here, I see no problem with keeping it as one servlet.
> If it is then i encounter a problem when trying to display the results. My operations class method creates a result set and uses a println to display each row in the database, how would i get this to output form the servlet?
The standard approach would be to have your operations class method return a List of objects being the result (rather than println)
You then set that list as a request attribute, to make it available to your JSP.
Your JSP then iterates over the list of objects to display them.
That way you seperate your display logic (the JSP) from where the data is coming from (the database).