July 5, 2010

Your First Cup of Coffee

A friend of mine asked me to provide a simple getting started guide for a first time Java programmer and it so happened it were other people that also wanted the same thing, so all of you out there here it comes, a simple learning by example guide in Java.

The example is a simple Web application that do simple Create, Retrieve, Update and Delete (CRUD) operation against a MySQL database.

Before starting writing you need to install the following on your system.
Installing programs on Windows is a pain and I will leave that as an exercise and instead show you how that this is done on a Linux platform, Ubuntu (ubuntu.com).

That's it! That took 3 minutes on a modest Internet connection. Are you still stuck on downloading the files on your Windows machine? And thereafter looking forward to reboot you system after each installation?

The Eclipse Ubuntu installation bundle is sadly quite old and therefore it is better to download the zip file manually and extracted it anywhere of you liking and run eclipse.exe (eclipse) in the eclipse folder.

Now lets create your database. Start the MySQL Query Browser and right click in the Schemata dockable window and then select Create Schema.... Enter simpledb.
Now double click on the newly created schema and then right click again to create a new table.

From here you can take several paths, either create a new project with maven archetype 'simple-webapp' (http://www.sonatype.com/books/mvnex-book/reference/web-sect-creating-project.html) or you can install the m2eclipse plugin (http://m2eclipse.sonatype.org/installing-m2eclipse.html) and create your project from Eclipse. Here, I will hopefully, choose the easiest way and do it manually, since we only will be needing two source files, where one will be empty – web.xml and index.jsp.

Now create you Maven Project Object Model file pom.xml in first-coffee/pom.xml

Now create your first-coffee/src/main/webapp/WEB-INF/web.xml

And finally your dynamic web page (first-coffee/src/main/webapp/index.jsp), where all your work be done.

To test your application, simply start the Maven built in web server, from your first-coffee directory.

Now open your web browser and enter the address http://localhost:8080/first-coffee/.

If you want to continue to further evolve this simple example, you can leave the jetty server running and simply refresh you web server, after saving your changes.

No comments: