Java tools for small to medium websites

Since I stopped serving drinks and started building websites, I tried many different languages and frameworks, not because it was absolutely necessary, but because i like to try everything. After some years I think I finally have preferred language for the web, and it’s called Java. Probably it was my choice since the beginning, mainly because I always liked Java, and I know it since the beginning of the language itself, but now I have no doubts… I like Java, I wouldn’t marry Java but I dreamed with it sometimes.



I’ve seen some blog posts about why developers don’t use Java as a web development language. One of the top reasons is because there is a scripting language called PHP that makes everything look simple, and is supported for almost, if not all, web hosting companies. And now there is RoR or TurboGears and others, that makes the process of creating a full web page as short as 10 minutes. So, many people look to web development in Java as something that is meant to big projects and complicated stuff. But my view is a bit different, I have in my toolbox some cool stuff that helps me creating small to medium projects in no time using Java.



Database Connection



First of all, one of the things that scares people away is to put a Java webapp talking to a RDBM like MySql or Postgresql. We need to load the driver, make a connection create statements or prepared statments, we must use result sets to retrive the rows, etc, etc. Then there is the scaring world of connection pooling, that we must set up our selfs, and that can be a big headache… For that and a bit more I use a tool called Torque from the good people from Apache.



There is some configuration involved in our development machine, but nothing to complicated, we just need to download maven, set the JAVA_HOME and MAVEN_HOME environment variables, take a quick look at the examples and we’re ready to start developing applications using torque (it works on any kind of applications, not just web based ones). Torque makes the connection to the database, sets up a connection pool, builds the database, creates some java beans to use in our app, and does a foot massage in the end… Or not… Anyway, it’s a great tool, all we need is a properties file and a xml file, to do all that. And best of all, it’s cross RDBM, so, if we want to move from MySql to Oracle and then to Postgresql, and so on, all it takes is to change some lines in the properties file, and we’re set.



But don’t be fooled by it’s simplicity, use it in big projects as well, it won’t behave worst than Hibernate for example.



Templating System



In this matter, PHP by itself it’s not better than Java. RoR and TurboGears and their friends from the fellowship of the pre-cooked frameworks come with built in support for that, but those are not in the same level, since they are frameworks, and not the language itself. There are frameworks for Java and Php as well that do that and more, specially in Java… But my tool of election for my templating is SiteMesh.



Using SiteMesh involves putting some jar files in our WEB-INF/lib folder, and a small change in web.xml, namely, creating a page filter. Then we can use plain JSP, Velocity or Freemarker decorators as our templates. Because this post is about small projects, a simple JSP decorator, that fits for all pages usually is enough. But there are many options we can use with SiteMesh. We can have many decorators and have it chosen for each request based on the url, on request parameters, cookies, etc. Using JSP as the decorator language we can add some dynamic behavior to our template as well, so for example, a banner that randomly changes from request to request can be chosen directly on the decorator.



Once again, don’t be fooled and blah blah (yes, all this tools are great for great projects as well).



File uploads



Yes, file uploads are something that people who invented servlets and JSP. If we google for “uploading files with java” we see that ther is over 2.000.000 results, wath shows that there is lot of people asking how they can do that. Once again there are lots of free tools avaiable, and once again I have a favorite one and once again, it’s provided by our Apachian friends, and once again, it’s simple yet powerful. Actually is a sub-project from Jakarta Commons, called fileUpload (original uh?).



Setting up involves a couple of jar files on our WEB-INF/lib, and using it involves (as expected) some java coding, but not much, just take a look at the user guide on their site, and you’ll quickly learn the basic usage.



Handling images



Sometimes we need to manipulate some images even on a simple project, either for creating thumbnails of the uploaded images on the fly, or to create those cool anti-spam codes on forms. Java bundles with a great API for that, the Java 2D API. But if you’re running your application server on a server that is not running a graphical interface, it simply won’t work. Since Java 1.5 there is an option called headless java that prevents that issue, but in some hosts doesn’t allow us to change the java runtime parameters, or they run an older java version, so, in that cases I use a tool called PJAToolKit, a pure java library that doesn’t need any graphics resource on the system. All you need to do is to change java.awt system property to com.eteks.awt.PJAToolkit and you’re ready to roll. The rest works just like Java 2D.



The end



There are lots of alternatives for each of those tools, you just need to find the one that serves you better. I’m already used to that, and I have no reason of complaint. But what would be really cool, is a tool that makes all the job for us while we’re drinking a cold bear on our favorite pub in a hot day, but that would leaves without a job, I guess… Well, let’s keep it this way.