Monday, January 12, 2009

Beyond web.xml

Another day paying the bills in Eclipse...
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("Hello World!");
}
}

So you've just written yourself a spiffy little servlet and you want to test it out. That's when it hits you... there is a long and arduous road ahead. You've got to remember how to deploy a War (how does that Ant script go again?). You need to configure Tomcat (Or wait 20 minutes for Weblogic to start). Then you need to write that dreaded web.xml file. This is a barren wasteland, riddled with fire and ash and dust. The very air you breathe is a poisonous fume. Not with ten thousand men could you do this. Tis folly!

Dag-nabbit! You aren't writing a big fancy industrial strength web app. All you want to do is run your little servlet in a local web browser and get on with your life! Jiminy Cricket, it's times like this that can turn a developer to Rails.

So what are you going to do? Bite the bullet and muddle through it all for the umpteenth time? Give up and write a command line app? Recall that the three chief virtues of a programmer are: Laziness, Impatience, and Hubris. Where there is a will, there is a way.

Grab yourself a Jetty jar and throw one of these in your app:
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

public class EmbeddedServletRunner {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
Context root = new Context(server,"/",Context.SESSIONS);
root.addServlet(new ServletHolder(new HelloWorldServlet()), "/hello");
server.start();
}
}
Run it. Point your browser to http://localhost:8080/hello and you're good to go.

6 comments:

Ethan Vizitei said...

Nice (jetty, that is). Not quite rails-a-riffic, but nice. ; )

Anonymous said...

Man.. We use Hudson for CI and getting tomcat running happily was definitely a chore.

I kinda liked the rails option, personally. What's the hold up on that? What's the argument against? Years of legacy code? Bah.

I guess Java's not really as bad as it used to be -- Ian and I have been hammering through a good amount of Java lately, building ANTLR grammars.

Cheers!

Unknown said...

rboyd - Nothing against Rails, I'm actively using it. I'm really excited about the merge with Merb!

There are various reasons why I might choose Java over Ruby for a one-off web app. I might need a library. I might need the performance for some computationally intensive task. I might be more experienced in it. I might work at a Java shop.

I'd also like to remind the community that not everything written in a language other than Ruby is "legacy code" ;)

Now parsing in Java... that's just crazy :P
I'd use Haskell for that.

Dan Watt said...

Now Ray, give credit where credit is due... Who came up with the quick and dirty ESR? :)

Unknown said...

Dan: Yeah, you were definitely the inspiration for that one -- though your version still requires the web.xml (unless you've got something I haven't seen).

Dan Watt said...

Actually, JRead was based on a 100% web-xml free Jetty.