Suppose I want to build a web application including displaying sports information including baseball, soccer, golf, etc. Each sport has its own web page but they share some similarity, for example, if user goes to a soccer page, user can pick team name, player name, etc. This kind of thing is common for every type sport, but soccer page may need to retrieve something special like soccer tournament info, and golf may need to display golf grass field information, etc. Such thing is unique for each sport. So, I can organize my web application in the following two ways --- 1. create ROOT/soccer. Put .jsp .htm for soccer here. Then I have /WEB-INF/classes/, inside this folder I have servlet/, bean/, etc. Similarly I creat ROOT/golf. Put golf related .jsp, .htm here and then create WEB-INF/classes/, inside that I create servlet/, bean/, etc. Similarly I create ROOT/baseball/, and so on. The good thing is it looks clean for each web application and it's self-contained. The problem is inside the servlet/, each sub-application's servlet may share LOT of common things and just have few lines difference. So, it seems I am doing lot of "copy and paste" and spread the similar code around various applications. Kind of waste ? 2. Another way is to create ROOT/, inside that I have ROOT/image/ where all of the images for soccer, golf are put, in ROOT/servlet/ I create a servlet which handles cases for all of the sports but certainly this servlet gets to be fat because lot of special cases for different sport needs to be considered, then I create ROOT/bean/soccer, ROOT/bean/golf/ to put beans. The advantage is I don't need to spread lot of similar codes in many locations when they only have little difference. The disadvantage is I may have to make a servlet too fat and too central that every application relies on. Which way makes more sense ? Please share your thoughts. thanks, steve
You can have separate folders for your JSPs, but still have a single Servlet Front Controller. The Servlet need not become "fat" as it can simply delegate to an appropriate Helper object for that sport. If the sport Helper objects share common methods, you can either implement a common Interface or subclass them from a common Abstract class. The Abstract class is preferable if there is any implementation shared between them. Hope that helps
You are modeling your application into two layers. First is the presentation which has jsps/htmls etc. which is fine. Then you have your controller which is the Servlet. That is also fine but better not to use it to do the business functions. Just use it as a controller. Introduce another layer of calsses with a proper inheritance hierarchy in such a way you don't re-code the same thing. Controller Servlet can call these business layer classes to get the work done.
Thanks tiny ad, for helping me escape the terrible comfort of this chair.