views:

1654

answers:

3

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?

+1  A: 

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
+3  A: 

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
A: 

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
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
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
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`.
Harry Pham
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