Servlet Tutorial Part 4 - Object Oriented Servlet Coding, September 2002
The Lines Are Drawn Here
Formatting HTML output with server-side Java has two distinct camps. On one hand you have the avid JSP people who put all of their HTML in the JSP files, completely separating it from their Java (Actually it's still mixed though). On the other side we have the servlet people, myself included, who don't like the idea of a separate JSP file for every page on their site. There are ways around this in JSP but we're just stuck in our ways. One thing is overwhelmingly agreed on though........Your Java files are NOT the place for HTML! There are several ways to accomplish this, most of which we will explore in this tutorial.
The other important concept to grasp when building servlets is the idea of small modular
code that can be reused over and over. Not only does this lend itself to the Java
philosophy of reusability, it also makes debugging your code a thousand times easier. I live by a rule given to me by a friend when building servlets. If your servlet is more than 100 lines
(125 max) it's too big and needs to be broken down into objects. If your methods are more than a screen on your computer they are WAY too big and should be broken down. Optimally your
methods and objects should do one thing well so don't try to cram 20 processes into an object.
So let's format this little project of ours so the HTML output is a touch nicer but doing it the wrong way to show you what NOT to do. Click here to see the newly formatted output and then click here to see the java source code that outputs it. Pretty much a real mess if you ask me. We also have a few ways to deal with this and a few ways to add some functionality along with our reusability.
String Interfaces
The simplest way for handling output formatting is by building a String interface. This allows you to set up HTML lines that you will use over and over in your websites and get them out of your servlets. It's not exactly the best way but it does serve the purpose. Also by creating String constants these memory hogs, (the Strings that is) are only created once on the server instead of every time your objects are created. So here's the code with an interface implemented and here's the interface. The new sections that use the interface variables are in bold text. Now for those of you unsure how the heck this works here's the quick explanation. When you implement the interface it's just like having access to variables within your own class so all you need to do is an out.print with the variable.
So any ways, back to the formatting. Now this may have dropped a lot of Strings out of the servlet but to be very honest the code is still one giant mess. Secondly we have almost no functionality in these Strings....there has to be a better way, and by jove there is. So let's look at building some objects that will not only clean this puppy up but give ourselves some USEFUL classes that we can reuse over and over. So flip the page, move to the next lesson, let's quit screwing around & get down to some REAL SERIOUS coding
Chris Marksbury, ChrisMarksbury.com | More Testimonials >>