Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One or multiple servlets per webapp?

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?

like image 720
Mnementh Avatar asked Nov 07 '08 13:11

Mnementh


People also ask

How many servlets are there per Web application?

There can be any number of servlets in a web application.It is not like that there should be only one servlet in a web application. If not so wat is there use of Servlet Context then. . But in general you can have any number of servlets in a web application.

Can you have multiple servlets?

Your web app can contain multiple servlets!

How many servlets are there?

There are two main types of servlets: Generic servlets extend javax. servlet. GenericServlet.

Can servlet handle multiple requests?

A Java servlet container / web server is typically multithreaded. That means, that multiple requests to the same servlet may be executed at the same time.


1 Answers

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.

like image 188
Vincent Ramdhanie Avatar answered Sep 18 '22 19:09

Vincent Ramdhanie