File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How many servlets should I use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How many servlets should I use?" Watch "How many servlets should I use?" New topic
Author

How many servlets should I use?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm currently working on an application that will be executed through servlets and JSPs. It's a small application and will consist of only 3 pages, the first of which will be static HTML.
My question is, should I use 2 separate servlets to perform the logic for the other two dynamic pages, or should I use a single servlet? I had initially planned to use two servlets and each one could handle the logic itself. However, I've been thinking about using a single servlet (as a controller) that will really just "hand off" the logic processing to another class or set or classes.
Does anyone have any advice as to which approach would be better in terms of maintainability or extensibility? Perhaps there are even other aspects that I haven't considered yet. I'm sure the application could be built either way, I'm just curious if either one is inherently better than the other.
Thanks,
Corey


SCJP Tipline, etc.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I have grown very fond of the MVC approach. If you use a single servlet as a controller and have helper classes handle the logic, then it is very easy to expand as your needs change.


Matthew Phillips
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Matthew Phillips:
I have grown very fond of the MVC approach. If you use a single servlet as a controller and have helper classes handle the logic, then it is very easy to expand as your needs change.

Yes, I like the MVC approach, but how do you handle access to things like the session and the request. If, for example, I have a servlet which utilizes a class A, should class A really know that it is being used by a servlet and start putting things in the session or in the request? Or, would it perhaps be best to have an abtraction layer between the servlet and class A? That way, class A is unaware of what is using it (so it can be more loosely coupled), and the servlet can continue to ignore the business logic.
Any more opinions on that approach?
Thanks,
Corey
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I would pass the request and response on to the helper class. I would pull my web specific information from the request, session, etc and then pass it to a generic class that contains my business logic.
Bill Siggelkow
Ranch Hand

Joined: Jun 27, 2001
Posts: 57
It depends on which side of the "fence" class A is living in. Typically, you will have something like Action classes that bridge the gap between the servlet and the business layer. These action classes can move and transform data to/from the session and request to the business classes.
This is similar to the technique that is recommended for Struts.


Bill Siggelkow
Jade Cove Solutions
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Thanks, all. After talking through this (as simple as it is), I've come up with a better solution.
Corey
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
It looks like Chapter 12 of Professional Servlets covers just this topic. You can check it out at www.simongbrown.com.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Originally posted by Corey McGlone:
Thanks, all. After talking through this (as simple as it is), I've come up with a better solution.
Corey

Please share, if you don't mind. I would love to hear about other solutions.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Well, I had originally been planning on doing something like this:

In this architecture, each servlet would be responsible for generating the data for 1 page each. Each servlet would utilize another class (or set of classes) to perform the business logic. From this, either the servlets would have to be involved in the business logic or the business classes (A & B) would have to be aware of the servlet environment in dealing with the request and session. In addition, any time I'd like to add a new page, I'd have to add a new servlet.
However, with what we've discussed here, this is the architecture I'm planning on utilizing:

In this architecture, the servlet is responsible for nothing but directing traffic to the correct "Action Class." This makes it a more definitive controller in the MVC model.
Each Action Class will handle data from the request and session and abstract that layer away from the two lower classes (A & B). This way, classes A & B are unaware of the servlet environment and could as easily be used in a normal Java application as in a servlet based application.
This apprach increases the complexity of the system somewhat but, as far as extensibility and maintainability goes, I think it makes a lot more sense. Anyone have any comments on my "final" design?
Thanks all,
Corey
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
That was what I was referring to above, but you explained it much more clearly.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I've heard of something called "struts." What is it and how might that fit into this architecture? Would it even be useful in a case such as this?
Thanks again,
Corey
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Struts has taken care of a lot of the underlying structure so that you will be able to concetrate on business logic. Check out Thomas Paul's article and the the struts website.
Hema Menon
Ranch Hand

Joined: Oct 29, 2000
Posts: 569
Check out what Thomas Paul had to say about Struts
http://www.javaranch.com/newsletter/Mar2002/newslettermar2002.jsp#struts
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Thanks for the links. As useful as struts look, I think it would be overkill for an application of this size (only 3 pages and little hope of getting bigger).
Thanks,
Corey
Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Hi there,
I would also love to know whether Servlets or JSP is appropriate to process user's inputs?
Thanks.


Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Doug Wang:
I would also love to know whether Servlets or JSP is appropriate to process user's inputs?

In general, I try to keep any sort of logic out of the JSPs. If you're going to do any sort of processing, keep it within the servlets. It's easier to write it there and it's a lot easier to debug and maintain that a JSP that's filled with Java code.
In my opinion, a JSP should only be used to format data for display. All data should be readily available for the JSP so that it doesn't have to do any processing. Similarly, the JSP should bother processing any inputs from the user, the JSP should simply "pass the buck."
Corey
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
Hi McGlone,
Yeah its true if u are having a controller servelet, its easy to maintain and extend as u can do the following at the controller level
1) Authentication and Authorization
2) Session Tracking
3) Final Display
At the same time in my view, for a small application I won't go for this as it viable for large web applications , for small applications having 4-5 pages I would go for single servelet
and as regards to wang question of which architecture to use a jsp-Servlet combination, or a plain servelet, I guess that again depends on the type and nature of the project.Yeah JSP is meant for jst to format the display , a small amount of java code is ok , but as far as possible we should keep the buisness logic in a servelet or a helper class
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
Originally posted by Corey McGlone:

In general, I try to keep any sort of logic out of the JSPs. If you're going to do any sort of processing, keep it within the servlets. It's easier to write it there and it's a lot easier to debug and maintain that a JSP that's filled with Java code.
In my opinion, a JSP should only be used to format data for display. All data should be readily available for the JSP so that it doesn't have to do any processing. Similarly, the JSP should bother processing any inputs from the user, the JSP should simply "pass the buck."

Looks like you've approached the topic intelligently and came up with the "right" solution. I do the same - all intput/control/scripting logic is in Servlets, all data/business logic in generic 'model' classes, and JSPs only handle the 'view' of the data. I go a step further than some do, and do formatting of data in the servlets after it gets back from the model, like date and currency formatting. Then the JSP just has to retrieve a variable and display it.


CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
One other reason to keep logic out of the JSPs - they can be a real pain in the a$* to debug. The compiler errors you get are some of the most cryptic things I've seen since a Perl regex.
Corey
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
Originally posted by Corey McGlone:
As useful as struts look, I think it would be overkill for an application of this size (only 3 pages and little hope of getting bigger).

Hi Corey,
You might want to take a look at WebWork. Its a web application framework, that is similar to Struts, but not as big (if that makes sense ). There isn't as much API to learn as with struts, and IMHO is far superior. I'm using it for a couple of projects and just love it!!
<rant>
The project lead is Rickard Oberg, probably one of the best J2EE guys out there. It's constantly being improved and new features are getting added to it.
</rant>
Can you tell I really like it....
If you have any questions on it, let me know.
/rick
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi guys. I've been following this thread and came up with a question.
When you guys refer to "helper classes", are they servlets themselves?
What I have in mind is having a single entry point servlet. That servlet evaluates the request and forwards it to the corresponding servlet. Is that ok? So for instance in my index.jsp page I have links to my controller but with different parameters.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Andres Gonzalez:
When you guys refer to "helper classes", are they servlets themselves?

No. The "helper classes" would not be servlets. There's no need for them to be. The purpose of a servlet is so that you can communicate with a Java class from a browser. Once you've communicated to a servlet, that servlet can pass off the request to any Java class.
In the design that we're talking about here, the helper classes would be the "Action Classes." The purpose of these classes would be to remove and set values in the request/session. This allows the servlet to ignore any business logic and allowes the other model classes to remain unaware that they are in a servlet environment. This approach allows for greater decoupling, which promotes reusability. (Good things, my friend )
So, to sum up, the only reason to make use a servlet is if you'd like to communicate with it from a browser. (There are probably other reasons, but this is the main one.)
I hope that helps,
Corey
 
wood burning stoves
 
subject: How many servlets should I use?
 
Similar Threads
Difference between servlet and struts
struts advantages over other frameworks
JSP more efficient than Servlet ???
Specifying new J2EE project - opinions wanted
struts 2 design problem