I am looking at such previous questions as:
https://stackoverflow.com/questions/98334/creating-a-java-servlet-web-application
How many actions should a servlet perform?
There are answers in these threads that contradict each other; some people say use a servlet for each page, and other say use only ONE servlet for your entire app.
I have the same problem. So, how to I decide what my servlets will be? If I use a single (or a few) “Front Controllers,” how do I parse the requests to delegate them to other objects? For example, if a single page has 3 different forms on it, how do I tell the difference between their requests? How do I tell the difference between forms and requests from different pages? Assign multiple servlet-mappings for each form + page? Look at the parameter names? URL-encode a “request-type” parameter?
So many ways to do things…
P.S. I’d rather NOT use a framework like Struts – I want to know the best way to do this using the Servlet API. I’m using Tomcat7.
Front controller is just the gateway into your app for HTTP requests. But that usually acts as a traffic cop to route HTTP requests to classes that know how to handle them.
Spring, for example, has a DispatcherServlet that maps Controllers to requests (usually by URL). The Controller used to be an interface, but now Controller POJOs can also be annotated.
The Controller interface has a single method: handleRequest:
It takes in HTTP request and response and returns a ModelAndView.