Dabbling in Java Web Programming

A bunch of new projects at work are requiring that I become familiar with Java programming, web programming and GWT in particular. My co-worker gave me this tutorial to try out, as the principles are similar to what we're using in the new projects.

So after setting up my environment (that's another post), I followed the tutorial and then began modifying the example code to see how I could change it. (The first thing that surprised me was that Java doesn't automatically sort stuff -- you need to do that separately (that's where SortedMap came in handy)):

package com.sonatype.maven.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, NoSuchElementException {

		PrintWriter out = response.getWriter();
		out.println("SimpleServlet Executed");
		out.println("Character Encoding is " + response.getCharacterEncoding());
		out.println("ContentType is " + response.getContentType());

		out.println("nHere is a list of all query string vars:n");

		Enumeration e = request.getParameterNames();
		SortedMap sm = new TreeMap();

		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = request.getParameter(name);
			sm.put(name, value);
		}

		for (Iterator i = sm.entrySet().iterator(); i.hasNext();) {
			out.println(i.next());
		}

		out.println("nHere are the vars combined into a sentence:n");

		for (Iterator i = sm.values().iterator(); i.hasNext();) {
			out.print(i.next() + " ");
		}
		out.flush();
		out.close();
	}
}

So basically, after starting the Jetty Servlet container I was able to visit a URL like this:

http://localhost:8080/simple-webapp/simple?var1=Hello&var2=world&var3=of&var4=java!

And got an output like this:

SimpleServlet Executed
Character Encoding is ISO-8859-1
ContentType is null

Here is a list of all query string vars:
var1=Hello
var2=world
var3=of
var4=java!

Here are the vars combined into a sentence:

Hello world of java!

Java is cool stuff and I'm going to be diving into it a lot over the next few months. I'm reading an O'Reilly book called Head First Java which uses an innovative new method of teaching:

The latest research in cognitive science, neurobiology, and educational psychology shows that learning at the deeper levels takes a lot more than text on a page. Actively combining words and pictures not only helps in understanding the subject, but in remembering it. According to some studies, an engaging, entertaining, image-rich, conversational approach actually teaches the subject better. Head First Java puts these theories into practice with a vengeance. Chock full of mind stretching exercises, memorable analogies, and stories, humor and attitude that aren't just pasted-on distractions but that are used to drive home key points and make ideas come alive, the Head First approach is as effective as it is unique.

The method is definitely new to me, but it makes a lot of sense. I've started committing an hour each day to reading the book and doing examples/puzzles.