[ACCEPTED]-One or multiple servlets per webapp?-web-applications

Accepted answer
Score: 12

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

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

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

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

Score: 10

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

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

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

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

Score: 3

It depends.

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

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

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

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

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

More Related questions