TM.
TM.

Reputation: 111017

Java Servlets: Performance

I am working on a web application in Java which gets data from servlets via AJAX calls.

This application features several page elements which get new data from the server at fairly rapid intervals.

With a lot of users, the demand on the server has a potential to get fairly high, so I am curious:

Which approach offers the best performance:

Many servlets (one for each type of data request)?

Or:

a single servlet that can handle all of the requests?

Upvotes: 4

Views: 1623

Answers (6)

anjanb
anjanb

Reputation: 13857

I'm sure you know that you can have multiple instances of the same servlet as long as you register different nodes in the web.xml file for your app -- ie, assuming you want to do that.

Other than that, from what I'm understanding, you might benefit from comet architecture -- http://en.wikipedia.org/wiki/Comet_(programming).
There are already some implementations of Comet on some servlet containers -- here's one look at how to use Ajax and Comet -- http://www.ibm.com/developerworks/java/library/j-jettydwr/. You should study before deciding on your architecture.

BR,
~A

Upvotes: 0

Eric Wendelin
Eric Wendelin

Reputation: 44349

Like Tony said, there really isn't a reason to use more than one servlet, unless you need to break up a complex Java Servlet class or perhaps implement an intercepting filter.

Upvotes: 0

Nrj
Nrj

Reputation: 6831

There is as such no performance enhancements in case you use multiple servlets since for each servlet request is handled in a separate thread, provided it is not single threaded.

But keeping modularity and separation of code, you can have multiple servlets.

Upvotes: 1

cdeszaq
cdeszaq

Reputation: 31280

One possible reason to have multiple services is that if you need to expand to multiple servers to handle the load in the future, it is easier to move a seperate service to it's own server than to do it "behind the scenes" if everything is comming out of one service.

That being said, there is extra maintinence overhead if you have multiple servlets, so it is a matter of balancing future flexibility with lower maintainability.

Upvotes: 1

S.Lott
S.Lott

Reputation: 391818

The struts framework uses one servlet for everything in your app. Your stuff plugs into that one servlet. If it works for them, it will probably work for you.

Upvotes: 2

Tony BenBrahim
Tony BenBrahim

Reputation: 7290

There is no performance reason to have more than one servlet. In a web application, only a single instance of a servlet class is instantitated, no matter how many requests. Requests are not serialized, they are handled concurrently, hence the need for your servlet to be thread safe.

Upvotes: 11

Related Questions