Over years of moderating at The JavaRanch, I’ve seen one type of question spring up on a weekly basis: that asked by people who need help with JDBC code inside of Java Server Pages (JSPs). As much as we may want to help this individual fix their particular problem, the overriding thought of “STOP WHAT YOU’RE DOING” often prevents us from doing so. The purpose of this post is to explain why putting JDBC code inside a JSP file is akin to shooting yourself in the foot. With a shotgun. While not wearing shoes.
1. You cannot reuse the code
First and foremost is the issue of code reusability. While importing java classes is quite common, importing code from a JSP is not. While you can write JSP functions, although I never recommend doing so for reasons I won’t get into now, you’re basically writing code that you cannot be used anywhere else, particularly in non-JSP java classes. The most common counter response to this is “Well, I don’t need to use it anywhere else”. Yes, you do. Whether its just reusing code for making the connection to database or the code for performing a query and reading the results, it will be used again at some point in the future in a way you have not thought of yet. Unless you are being paid by the line and prefer this sort of thing, it’s a bad move, and I guarantee your code base will be much larger than someone who put all their JDBC code into normal Java classes. Larger code base means more difficulty to maintain and more headaches for you down the road.
2. You are mixing business logic with the presentation layer
Probably the most overlooked issue for inexperienced developers is the fact that you’re mixing the business/data layers with the presentation layer. I’ll put it another way, if your boss comes in one morning and says we’re throwing out the JSP front end and replacing it with a web service, Java Swing, Flash, or some other interface, there is virtually no way for you to reuse the database code without going through every line of every JSP file by hand. If the database code had been placed in plain java files, then you would have a path for packaging the JDBC code into a single JAR and making it available as a service to a different front-end client such as a web service, Flash, etc.
In enterprise development, the presentation JSP layer and the database are often separated by multiple layers of indirection such as described by the commonly used three-tier architecture pattern. Those who are just starting out programming often do not know why mixing these layers is bad, but I promise you if you stay with software development you’ll understand one day.
3. But it’s just this once!
Often times, JDBC code enters JSPs by developer lying to themselves saying “Well, it’s just this once” or “I just need to test something”. Instead of being removed when the developer is done ‘testing’, the code often persists for a long time afterward. Furthermore, putting your JDBC code inside of reusable Java classes makes testing go faster! Spending 10 minutes setting up a single reusable Java JDBC class will save you hours down the road. Then, if you want to test more than one JSP page with JDBC logic, you already have your Java class file to start with. Proponents of test-driven development tend to understand this better than anyone.
4. It’s really hard to maintain
Code maintenance is another topic that new developers do not fully appreciate since they have not spent years maintaining the same code base. Unless you write the most beautiful JDBC code imaginable, its very difficult to read through huge JSP files looking for bugs and/or making enhancements. It’s a lot easier if all the JDBC access is restricted to a set of files much smaller in size than the JSP code base.
5. It’s a really bad practice
If after reading this article you still do not fully understand why you should not put JDBC code inside of JSPs, let me simplify the issue by saying “Just Don’t Do It”. Whether or not developers understand the reasons against doing so is not as important as stopping them from doing so in the first place. In short, you create code someone else (possibly yourself) will have the misfortune of maintaining down the road.
In my mind a common reason for this anti-practice is that beginning JSP books do this in their examples. Similarly for college courses. We wrote JDBC code in JSPs in the web development class I took. Of course I know better now – but how is a new developer to know?
I agree with Jeanne. Even Daniel Liang wrote the same those codes in his book.
Another guy said its bad & world renowned java expert say its good.
I’m confused.
Nicely said. I believe that you should also mention the whole MVC pattern.
Completely agree with it. Jeanne is spot on as well, I’ve seen this so many times in examples. People start off doing it wrong and just carry on. Many of the times I’ve seen this people do it to “try things out” and never go back and fix it up. It simply grows bigger over time. It’s not just JDBC code however, this goes for any code.
I think you are missing how it works. Most of the time its school assignment, so not even why of your points matters. Teachers like dirty/hardcore code, because it makes them feel smart. Plus its not school job to teach you what you will need in real job.
I didnt know one could write JSPs in a PHP-like style.
Not only all of that, but, what happens when, for some reason, your database is not available? Your page now does not render. You get an error. Or worse, the page just hangs and never displays anything!
Believe me, it happens! It happened to me. So embarrassing!
Take my advice kids, be smart. Don’t start!
Just say NO!
I think there’s a confusion here between “student code” and “professional software development”, the latter of which is what this blog is about. The general rule of thumb for students is “if it compiles it must work” and rarely have I seen anything more substantial in my many years a college teaching assistant. I’m pleased students/teachers use this blog to learn more about Java/J2EE, but I hope they start following some of these design patterns now, if they plan to go into software development down the road.
Scott,
While my comment was directed at “intro to JSP for professionals”, I think what is taught in school does matter too. If professors accept “if it compiles, it must work”, why should students do anything more?
“Plus its not school job to teach you what you will need in real job.” – On some level yes, on some level no. We don’t get to say we won’t learn arithmetic or writing because it is “just school.” These are basic skills to build on.
OMG I was using a high profile application at work and I got a JDBC error from the client (and about 10 other errors from porky client heavy code)! I immediately sent a message to the development team commenting on the poor architecture choices. It seems like I talk about this until I am blue in the face at work. I stumbled across this article and could not agree more! Thanx for capturing it all soooo well!