I am going to document my progress through a personal challenge so to help others and myself learn about steps A-Z in creating a web app from scratch.
The Acronym Challenge is going to be a posting game (I use this term loosely) where an acronym will be posted and people submit entries that match the acronym. For example, TCP = tangy chocolate popsicles. Then the entries can be voted on. A set number of top scoring entries will be displayed, etc, etc. The entire system will be anonymous to start with and may or may not incorporate login functionality. I want it to be easy to use.
It’s going to be written in Java because I want to learn more about J2EE and Java in general. My goal is to write it from scratch without using frameworks even though Struts looks very useful. I’m using the Eclipse IDE and Tomcat 4.1.x as my web app server (more on that later). Database support is going to have to be limited to MySQL because that’s all I know. Eclipse does the packaging a deployment for me with the Lomboz J2EE plugin. Eclipse also takes care of a lot of the Javadoc work. I’m sparsely using CVS to track changes and keep a backup copy even though I’m the only one on my team. :)
Originally, I had planned on using JBoss instead of Tomcat because of its hot-deploy features. Unfortunately, I found that this didn’t work very cleanly and my deploy time was too long. I had also toyed with Cactus, but I don’t quite understand how to externally instantiate a servlet. So I ordered a book that’s not out yet. Hopefully, it will shed some light on JUnit as well.
I decided that I needed a controller. It’s a necessary part of the MVC model. The controller will handle all the actions that a user or internal process can generate. When a user posts an acronym response, the controller will forward the request to a “Post servlet” or maybe post.jsp. Post.jsp may show the user that the post was successful and then forward them back to the default display or main page.
The focus of my efforts so far has been on the controller. I have a controller that gets an initialization parameter from web.xml for a “command map” that externalizes the mappings of commands to business logic:
- User clicks on action that goes to http://server/controller/login
- The controller loads a file I call commandMap.properties that contains key/value pairs like “myCommand=login.jsp”
- I grab the end of the URL with getPathInfo() and strip off the beginning slash with a utility class
- The login.jsp page will be displayed (not functional yet)
Here’s a utility class that I use to strip off the beginning character from the path that’s submitted. I’ll continue to expand as I need it. Use it like this:
String pathInfo = UrlParser.stripLeadingCharacter(req.getPathInfo());
I also made a utility class called Debug which prints out info easily. I’m under Linux so I can just tail this file (it’s hardcoded – very bad) and observe whatever: