Forums Register Login

Servlet-DB design issue

+Pie Number of slices to send: Send
Hi
I don't know if this the proper place for this post, since its a design issue at the first place, but it is JSP/Servelt specific. So please pardon me.

In web applications we mostly need to get some data from a storage source, typically a database and the init a bean to manuplate or dispaly these data, and then may save them back to the DB. For example, in most occassions we have site members, so we need to define a member class to reprsent a site registerd member.
I would create a MemberBean with set/get functions for use in my JSP pages. But as you know we need to access the database in order to fetch the member data from the db or to save them back. Puting the database realted code into the bean is out of question IMHO since I think we should keep the bean as small and simple as possible.
My question is what approche do you suggest, and why?
1- To separate all the member behaviors into a class (i.e Memeber.class) both db-related behaviors like updateEmail() or other member behaviors like login(). And then call these methods inside servlets.
2- Same as 1 but separate the db releated methods in a class like MemberDBAccess.class and all member specific behaviors in a Member.class
3- The same as 2) but insted of making a class for data access to do all the database related actions. We do them in a servelt. For example UpdateMemberServlet and inside this servlet we'll write all the code needed to update the member record in the database after we pass the new values form a JSP page.

I hope that I made this clear.
Your input is much appreciated.
+Pie Number of slices to send: Send
By far the most common method (as documented in my book, and in about a dozen other Servlet books, not to mention in "Core J2EE Patterns") is to create a class to encapsulate the database access for a Member (called a Data Access Object, or DAO), which will work in conjunction with a Member class that implements Member-specific function.
This should only be called from a Controller -- which is either a Servlet or a class called from a Servlet (the Front Controller pattern). In this way, all of the database access has been handled (and all possible errors relatng to that dealt with) before the JSP is called, so that you both know exactly WHAT JSP to display, and so that you can properly display any errors within the JSP that you need.
Kyle
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 762 times.
Similar Threads
Closing Connection Which way ????
jsp and java beans question
best approach to send data from a selected row of a table
DAO or BMP
Releasing objects on multiple requests
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:35:18.