posted 18 years ago
Not necessarily.
Sure. It can be done like that (and I have seen several books which seem to suggest such an approach) but it's a clumsy and not particularly scaleable way to tackle the problem.
The job of such a "controller servlet" is essentially to translate HTTP requests to method calls. This can be done using if or case but can also be done using plenty of other approaches.
One common approach is to use a Map to "regsister" objects to request names or URL fragments. The controller just extracts the important bit of the URL, looks up a handler object in the Map, and calls that object to do stuff. Typically no more than 10 lines of code or so.
Another popular approach is to use the "Chain of command" pattern where the controller simply hands off the request to a list of handlers, and each one gets to look at it and decide what to do with it (typically the options are handle it and end, handle it and continue with other handlers, and do nothing).
Another approach is to use reflection. The controller again extracts the important bit of the request URL, and then uses the reflection API to try and find a class or method that has the same name, and then call it. This approach can allow applications to be changed and configured without recompilation.
I'm sure there are many more approaches to this common problem.