Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet design  RSS feed

 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an AppointmentManager servlet that manages adding of new appointment. do you think it's a good idea to use the servlet for editing of appointment also? In the code it would be something like:

if (action.equals("add") {

}

if (action.equals("edit") {

}

I could make a new servlet for editing but I am afraid I might end up with too many servlets/controllers.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wilson Gordon:
I could make a new servlet for editing but I am afraid I might end up with too many servlets/controllers.


Define "too many"? 2? 12? 200?

This is not a criteria you should use to constrain your design. Your decision should be based on concrete criteria such as the amount of code reuse.
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, so far I have 16 servlets, each is usually named "SomethingManager", where something is a class name. So far, in 5 or 6 of those servlets, I have codes for handling both adding and editing of that class objects.

Also, because adding or editing may have different required form fields, I usually have required fields checking and getting of form parameters in each of the if(action.equals("x")) blocks. When require fields checking fails, each if block forwards to different pages. (either the adding or editing form page).

So currently there aren't really much code reuse, other than declarations of some variables that both if blocks use.
[ May 02, 2007: Message edited by: Wilson Gordon ]
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

Originally posted by Wilson Gordon:
Also, because adding or editing may have different required form fields


Why are the form fields different? Usually, the fields you can edit are pretty much the same as the ones you create. A common expression to designate such kind of operations is CRUD (create, retrieve, update, delete). So, are your sure you cannot use the same form fields?

Now, back to your question. I tend to prefer putting all CRUD operations in one class. Particularly, I like to use the DispatchAction of the Struts framework (or similar ones from other frameworks). However, this is not a definite rule. For example, if your use case is complex or behave differently according to the specified operation, you might consider doing some kind of separation.
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leandro Melo:
Why are the form fields different? Usually, the fields you can edit are pretty much the same as the ones you create. A common expression to designate such kind of operations is CRUD (create, retrieve, update, delete). So, are your sure you cannot use the same form fields?

Well, it's just one or two field differences. For example, to create an appointment, person ID is a required field. But to edit an appointment, person ID is not necessary since you have appointment ID.

I think it's possible to reuse more of the codes by having more "action" checks and do things differently based on that (ex. forward to different page) . But I feel having two big if (action.equals("x")) blocks that don't depend on each other or share the same codes could be better, since it's easier to update.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I feel having two big if (action.equals("x")) blocks that don't depend on each other or share the same codes could be better, since it's easier to update.


If they're going to be this disparate, they should be separate actions.

If you are concerned about the number of servlets, investigate the Front Controller pattern. This article may be helpful.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to the above recommendation - I use either Struts or Webwork.
Seriously, if you're not yet familiar with them, and are interested in design, it's really, *really* worth it to at least have a look.

Webwork/Struts mean a single servelt, invoking various java classes, usually called Action classes (16 Action classes in your case).
Of course, this just postpones the dilemma - you still need to decide whether to put your add/edit code in a single Action class or not.

As stated in previous responses, it depends on your requirements... definite case of "how long is a rope".
*Personally*, in most of my IT applications, I found it convenient to have a single class for both Add and Edit (possibly with 2 different *methods* "add()", "edit()", sharing some common private auxiliary methods).
I've tried coding both approaches, a couple of times, and the single class felt better to *me*, despite the drawbacks you've mentioned.
But than again, you may have different requirements, or different design taste!
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice.
 
NareshA WaswaniA
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you should not have any business logic things in the servlet. have a different class, put the add and edit functions over there and depeneding on the action from the client, call the apprpriate method. It's very simple. Na dit does not make any sene that you chould have any other class or servlet for the edit action.

since both of them are doing logically the same process of manageing the managers actions....

So its good to have the 2 actions of add and edit in the same class but not inm the servlet.

+91-9986461501
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm... You mean adding static add() and edit() methods to the Appointment.java model class?

Something like:


(I use JDO and pm is a PersistenceManager instance).
Or should the add() and edit() methods be methods of the AppointmentManager servlet class?
[ May 05, 2007: Message edited by: Wilson Gordon ]
 
NareshA WaswaniA
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok its like this...

first of all, there should not be any business logic checks in the servlet. for instance the field checks and others.

the add and edit methods are not static. they are instance methods. in the servlet, you can create one object of the model class (which contains add and edit methods) in the servlet and then call the functions.

or you can also follow the singelton design pattern for your model class as it does not make any sense to have more than one objects of the model class. but yes make sure that they are thread safe else you would be in trouble because the server side programmig is multithreaded.
 
Wilson Gordon
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info.

I should change my servlet design then. I have been doing the required fields checking (by calling a utility function), as well as constructing and storing the objects inside the servlets.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I did an online editing program I ended up creating a monster XML file that defined all the fields and their requirements and even included a chunk of HTML defining the form entry field in the desired format. This permitted some generalization in creating the entry form and in checking the returned results.

Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!