• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet-DB design issue  RSS feed

 
Abu Anas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!