29

I know, it depends on the webapp. But in the normal case, what do you do: one servlet, that serves different pages (like an standalone-application with changing content) or for every page a single servlet.

Take for instance a blog. There is the start-page with the most recent blog-entries, an article-view for displaying one blog-entry and an archive. Do you implement this with three different servlets, or one that is dispatching to the functions. At least a good part of the stuff is shared, like http-headers.

So, what are your experiences, what works best?

Mnementh
  • 50,487
  • 48
  • 148
  • 202

3 Answers3

13

Usually you will create a servlet per use case. Servlets acts like controllers for your application. When you identify an interaction from a user then implement a servlet to control that interaction.

That is, if you are using plain servlet/JSP to build the site. If you are using a framework like struts you will find that they implement the front controller pattern and use a single servlet that recieves all the requests and forwards these requests to action classes that implement the actual logic of the user request. this is much harder to do yourself but its a good practice...its the reason why so many people use these frameworks.

So the short answer is, you will create many servlets per webapp since each webapp will expose several use cases.

[EDIT] Re-reading your question it seems as if you are using the term site to mean page or view. Again, it depends on what is happening on that view. For instance, To display the newest blog entry, you can have a servlet that constructs the list of entries from the database for display. If the user clicks on an entry then another servlet can retrieve that single entry for viewing and so on. Mainly, each action is a use case therefore a different servlet.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • Is it something like mvc repository pattern? like in asp.net mvc, we aggregate similar function into one controller. so is servlet similar to this idea? – Timeless Feb 05 '15 at 03:06
  • 1
    @Timeless not really, I don't think so. Servlets are fairly low level building blocks and for many years app developers have been using more advanced frameworks that live on top of the servlet platform but provides better application architecture. Spring MVC for example is one such framework which is similar in many ways to ASP MVC. – Vincent Ramdhanie Feb 05 '15 at 14:38
  • Does it impact the overall performance and memory/CPU usage if the number of servlets rises? – Zied Orabi Nov 21 '21 at 22:11
10

Most web frameworks use a dispatcher servlet (ex: Spring MVC) that takes care of routing requests to appropriate classes/controllers.

When you start having lots of pages, this approach works best because you have a more user friendly way (in regard to web.xml) of declaring/managing a class that handles http requests and its url. Example (spring mvc again):

@Controller
public class MyController {
 @RequestMapping("/viewPosts")
 public void doViewPosts(HttpRequest r, HttpResponse res) {
  //...
 }
}

Besides, having a dispatcher servlet keeps your code flow centralized.

Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
3

It depends.

In my latest projects, I have implemented a single servlet that delegates to several servlet-like objects which are instantiated in a dependency injection fashion. For instance, I have something like this in my servlet (pseudo-code):

for(Handler handler : handlers) {
    if(handler.handle(request, response)) {
         return;
    }
}

where Handler is an interface with a boolean handle(request, response) method. I obtain my handlers from a container (be it Spring or something even more lightweight).

The reason for this is that I really like dependency injection, and it is difficult to achieve it in Servlets; and I really don't feel much at home with most frameworks that provide web-component dependency injection- I like the simplicity of servlets.

Were not for this, I would go with multiple servlets, although there's a trade-off; either you have an enormous web xml with lots (and lots) of servlet mappings or you have a very complex servlet (unless you use something like my d-i approach).

alex
  • 5,213
  • 1
  • 24
  • 33
  • 2
    That's a re-invention of the wheel. A particularily good wheel is the DispatcherServlet. See http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html for more details. Its Controller interface accepts a request and response like your "handlers". – MetroidFan2002 Nov 07 '08 at 18:29
  • 3
    I'm aware of Spring's MVC- it has obviously influenced my design. But it has a lot of features I don't need and is somewhat more complex. It has worked well for me, and I wanted to implement it myself. If your needs are well suited by Spring's MVC, by all means use it. – alex Nov 07 '08 at 21:37
  • Can you please explain a bit more about `I have implemented a single servlet that delegates to several servlet-like objects which are instantiated in a dependency injection fashion`. Some pseudo-code would be appreciated, like if you have 4 different servlets `B`, `C`, `D` and `E`, how do I add a servlet `A` that will dedicate jobs to `B`, `C`, `D` and `E`. – Thang Pham Apr 13 '10 at 19:36
  • My B, C, D and E are not servlets, are instances of an interface which looks pretty similar to Servlet. "A" is a servlet. I just map everything to A, A invokes the service method in one of the servlet-like objects. That's one of the things Spring's DispatcherServlet does, you can check their source code. – alex Apr 13 '10 at 20:54