It's been a pretty significant 72 hours. I was talking about it with my wife, and realized that I've been pushing in this direction, being able to build java web apps, for the past 5 years. Through a handful of forks in the road, I got distracted. PHP got most of my attention, and most recently, Actionscript and Flash. But at long last, I've begun building a java web application. It's a content management system that I've decided to call "Dynamo" It's not the most original name, but I don't care. I like it.
I figure that it'd be a good idea to document the things that I've done to actually make this happen. In the most recent post, I was getting frustrated with the Eclipse plugin for Tomcat, provided by Sysdeo.com. I've since decided that the Web Tools Platform (WTP) plug in is much better suited. However, I will say that it's MUCH more heavy duty than the Tomcat plugin provided by sysdeo. WTP is made to build full fledged J2EE compliant web applications, and it shows. Interestingly enough, they released version 1.0 the same day that I found out about it.
A few notes about WTP should be noted.
First, this has most to do with my inexperience in using Eclipse, but it took me a while to figure out how to actually "build" a class. It turns out that by default, in Eclipse, this happens for you automatically. In the "Project" menu, there's an option named "Build Automatically". And guess what it does? Exactly! It builds stuff for you as soon as you save your java class. How Cool!
I ran into a bit of a problem at first because I was building a servlet, but in the browser, I was getting an error. This simply had to do with my bad java. Once I fixed the bad code, and re-launched the app, it worked. It took me a while to figure out what was going on because everything just worked without me thinking about it. I imagine this is how it must have felt when automatic transmissions were introduced in automobiles. "How do you make it go if you can't make it SHIFT!?!" I felt this way about compiling servlets.
What's really nice about WTP is that it places all of the classes in the correct places. Servlets that exist in the [Project]/src/ directory are built and placed in the WEB-INF/classes directory where they belong. You can also define any directory as a source directory. This gives you the ability to keep the source separated from the build. I like that.
Also of note is that it might be a good idea to have a plain vanilla, just-unzipped-and-left-alone, version of Tomcat installed for WTP to use. It seems to wrangle it somehow, taking over the Container entirely. I don't even know where the tested web applications are deployed from yet. Even in the unzipped-and-managed-by-eclipse/WTP installation of Tomcat, the developing web apps don't show up. Odd. I wonder where they go?
Another gotcha for me happened when trying to add a 3rd party driver (for the database). By default, the WTP creates a Web Application Library for these kinds of things, but I couldn't figure out a way to add this through the IDE. So, what I did was simply add it manually to the WEB-INF/lib directory in the project. I don't think that it was the right thing to do, but I couldn't figure out how to do it otherwise. However, once I did that, my application worked. I even tried to add another library, outside of the default web application library, but somehow, it wasn't recognized when deploying. Strange.
Although figuring out how to make Eclipse build, test and deploy web applications was a big deal, the biggest deal of the weekend happened when I was able to successfully build a page that connected to the database, and pull out content. I know that in itself is no big deal, but whenever you're working with a new technology, and things connect and work the way they should, it feels great!
I do have to say that it was a bit tricky figuring out how to get the database connection and query working though. I followed the unenlightening instructions at mysql.com, but something was missing. Other than the fact that my driver wasn't being loaded, I couldn't quite figure out how to go from making the connection to making a query and pulling out the results.
Making a database connection turns out to be a 2 step process.
First, define the driver.
Class.forName("com.mysql.jdbc.Driver").newInstance();The second step is to make the connection.
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=monty&password=greatsqldb");Now, where should you do this in a web application? The answer, is simple. You do this wherever you want to connect to the database and make a query. I chose to do this by making a connection in the ServletContext, so it was created once, and globally available to all of the servlets in the application. In the long run, I don't know if this is the best thing to do in an MVC style web application. Servlets usually play the role of a controller, and it doesn't really make sense to ask a controller to pass along a Connection reference whenever communication has to happen with the database. This is like asking your boss everytime you have to go to the bathroom. He really doesn't need to know about it. So I will probably move the connection logic to the Model Layer. However, as I delve into Hibernate and Struts, I think most of the answers will be provided for me.
Now, as I write this, something odd has happened to my Eclipse and WTP installation. I no longer have the option to create a "Dynamic Web Application" and the "server" project doesn't notice my Tomcat installation anymore. Harrumph. The joys of open source flexibility, eh?
I've managed to fix it with a fresh installation of Eclipse 3.1 with WTP bundled. Something I do certainly like about Eclipse is the ability to just unzip and run it. The fact that there are no installers makes it super easy to experiment without causing irepparable harm. You can find the Web Tools Platform bundled with Eclipse on the downloads page. Look for the All-in-one. That's what you want.
Now that I feel empowered enough to build webapps in a development environment, my next goal is to jump head first into the deep end. I plan on creating a simlpe CRUD application for a database table using Struts. And after that, maybe in conjunction, I plan on doing this with Hibernate to manage the data. And after that, Probably in tandem with the others, is learning about JSTL, or perhaps a templating technology like Tiles and/or Velocity. Although, after looking briefly, I think that JSTL, and perhaps some custom tags for specific presentation features, might be enough to do the job. I'm excited to start playing around.