How I organized KeyTopic technically

mt2 asks for advice on how to organize a java web application.
I don’t know if this entirely according to the MVC (Model View Controller) pattern, but anyway, it works very well for KeyTopic. Do with it what you want.

I separated application in roughly three layers:

  1. The Model: The database
  2. The Logic: The servlet and action classes
  3. The Presentation: Handled by the JSPs with JSTL

There are three important kinds of classes I use:

  1. Action classes. Every action (Board Index, Topic List, Topic View, Login, Post etc.) has it’s own class which is derived from the abstract Action class. Such a class has at least 3 methods:
  • getDispatch() which returns the JSP to dispatch the request to for the presentation
  • getRedirect() the URL to redirect to one of those has to be null obviously
  • run(), which is does the actual work
  1. Each class extending Action has some fields to it’s disposal such as the HttpServletRequest and HttpServletResponse objects, settings object, a database connection it can use, and the list of paramaters (more on that later)
  2. Java beans. These are object representations of everything that lives inside of the application, such as boards, categories, topics, messages, users etc. each of those has a class representation and properties reflecting the ones in the database (username, userid, title, isAdmin, number of posts) and some utility methods such as canAccessBoard(Board b), canReply(Topic t) etc.
  3. Cache objects. Those are singletons, singletons, in short, are classes that only have one instance, application wide. I use three: Settings, CategoryProvider and BoardProvider. Settings contain all the settings set in the database and in the web.xml file. CategoryProvider contains all categories and BoardProvider contains all boards. That means that those don’t have to be reloaded from the database on every request.

The main servlet
All request starting with /KeyTopic are routed to one single servlet, in my case millhouse.keytopic.KeyTopic. What this servlet does is inspect the URL that it was called from (for example /KeyTopic/topic/22). It splits this part of the URL on the slashes and checks what the 2nd element of the array is (‘topic’ in the example). It looks this up in the actions registry (a Hashtable with action => ActionClass mappings) and loads and instatiates the action class that belongs to it dynamically.
The action class object then gets a database connection it can use, gets a couple of other data passed to it (such as the luser: the user doing the request) and the run() method is then invoked. After that getDispatch() and getRedirect() are inspected and appropriate action is taken (i.e. the user is either redirected or gets to see a HTML page generated by the appropriate JSP).

Action classes
What does an Action class do? First it inspects the paramaters it gets passed (both throug the URL [ /topic/22 ] and through request variables), it validates this input (important!) and then takes appropriate action. It could query the database for example. Everything that has to be represented to the user in some way has to be put in Java beans. For example for a topic view, a JSP gets an array of Message java beans it can show. All the beans that a JSP gets access to have to be put in the HttpServletRequest object (request.setAttribute(“name”, object)).

JSP
A JSP only does the presentation, there’s no Java code in the JSPs so you can hire designers to edit those files. The JSP represents the java beans it got appropriately. I used the JSTL (Java Standard Tag Library), which provides you with tags to do simple logic such as ifs, foreach-es etc. I added some new tags to KT code parsing ([b]bla[/b] => bla) and some other minor tags.

Where to go from here?
This brief explanation is in no way a tutorial or all you need to know. I have a couple of books on Java Servlets and JSP. I recommend those two: