Part Five - Real Object Oriented Servlet Coding, September 2002
Functional, Reusable Objects
Let's take a look at the first line of the output formatting. It's just a standard opening HTML, HEAD, TITLE line. Now obviously you're going to need one of these in every page
you output but what about that pesky title? You don't want the same title on every page of your site now do you. What if we want to change the style sheet for one page or add a
JavaScript link line in the head, or maybe meta tags? Well in this case I would make a small object to handle all of these scenarios. So let's build our first object for this puppy. This is
where you need to start thinking out of the box, so to speak. You only want to do this once & you want to be able to use this object over & over.
Click Here to see what I came up. It could use a little tweaking
but hey, I came up with it in a half hour. We now
replace that first HTML line in our servlet with these new lines Click Here and look for the bold lines.
Now to a novice this looks like a lot of work to print one stinking line out but if you look closely at the HtmlTop class we have now added the ability to add a title, externally linked JavaScript files & stylesheets, and we can even include meta tags if we so desire. We have built a useful, functional class that we can now use with any website we feel like! This is the basis of object oriented code, write it once, use it all over the place! The constructor allows you to enter any of the information you need & then triggers the appropriate lines to be printed when the doHtmlTop method is called. You could technically do everything in the constructor but I like to create methods for anything I'm outputting.
OK, let's look at some of the rest of this code & see if we can tidy it up some, maybe get a few more reusable objects out of the deal. How about that pesky body tag in the HTML, if I wanted to add a BgColor or something else Id have to hard code that in too. What about an object that would allow me to get that line out while adding some functionality for possible future use........hmm, makes ya think doesn't it? Here's what I came up with for this little dilemma. Click Here to see our new HtmlBody class. Now we have the control to reuse this object ANYWHERE, on ANY SITE, while being able to specify what we need in our body tag. It's all there for the taking, just pass it the parameters you want to use & it works.....Now were getting somewhere. As with out HtmlTop class we just create an instance of it, passing the needed parameters and then call the doBodyTag method like this. Now I'm going to venture on and do the rest of these HTML objects without boring you with the details, go grab a cup of coffee or something, Ill be with you in a flash!
Gee, that was one fast cup of coffee but I did get them done so here are our new classes:
- Html Table Class Here's a reusable class for creating your Html tables, all the options you would want....just fill in the constructor.
- Html TR Class Another class that lets you create your <TR> tags with any parameters you want, once again passing the variables you need in the constructor.
- Html TD Class And another class that lets you set up your <TD> tags with any parameters you need, passing the variables you need in the constructor.
- Our Error Class I've taken that error message out of the servlet and put its in its own class.
So here's our finished servlet (Well, sorta)...We have removed all of the HTML (I really went a bit overboard on this stupid table) and have at the same time created some classes that we can now use for any website over and over again. BUT, My right eye has started twitching about the size of this monster and I swear my blood pressure has gone up a few notches....nope this won't do, we have to break this thing down even more.
OK, so we are getting there but we really don't want all of this code in our servlet. We have two very distinct things we are doing that are really causing my twitches. First one is handling the request checking and the second is all of the html class nonsense so let's get them out and into their own objects. The tricky thing with handling the request is the fact that we need to return either the variables from the form or the error Vector.......hmmm, Yup I've got it....since we are using a Vector we will just add another element so we can then check it's size when we get it back. If the size is 5 we have error, if it's only 4 we SHOULD have good data that we can then handle. Then we just bang through the Vector getting our variables back and passing them to the HtmlOutput class. So FINALLY here is our finished servlet. Here is the new RequestHandler Class that gets all that nasty testing out of our servlet. Here's the HtmlOutput Class that REALLY removes that HTML handling from the servlet! I have also added one line to our error code to remove that error Boolean. FirstCodeError Class. Finally I can look at this thing without getting twitchy but what happened to our StringBuffer idea? You may be curious why I didn't do it this way the first time. Well the StringBuffer has one little side effect that I'm not too thrilled with....your HTML source code is one line! The only way to combat this would be to add new line chars to your out.printlns and personally I don't want to add anything more to this than I have to and I like my HTML source as readable as possible, especially when I have a formatting bug! I'm afraid you will need to judge the way you do this one servlet at a time, judging the possibility of IO Blocking and Netscape bugs for yourself. The StringBuffer IS the best way. To show you the way to only print once per run let's now get all the printlns out. We also have to change the HTML classes a touch to handle this other way so here they are:
Displaying Text
One more thing we need to see here is how to read big blocks of text, such as this article. There is absolutely NO reason to ever include text like this in any of your servlets, or objects so let me show you how I do it & you can take it from there. Click Here to see it work. Click Here to see the code. All of the text is kept in simple .txt files on the server and because of this can be edited "on the fly" via your favorite FTP program. The other beauty of this is that you can have non-programmers writing the articles and they never have to go anywhere near your code! Lastly you NEVER have to recompile a servlet or class due to an article changing......now that's functionality. Well hope you had fun with this, we'll move on to another subject as soon as I can catch my breath!
Lissa Lee, bathandbodycare.com | More Testimonials >>