« Servlets+JSP design question | Main | links for 2007-08-16 »

Servlets+JSP design answers

Earlier I asked: "I’m wondering how would one produce a URL space for a blog style archive, using Servlets+JSP, and do so in a way that isn’t a CGI/RPC explicit call?"

I got some answers, all good:

Sam Ruby: "Perhaps via URL Rewrite Filters?" and pointed at a Servlet Filter that does URL rewriting, called UrlRewriterFilter.

Stefan Tilkov (who started all this ;) suggsted decomposing the URL in the Servlet::

    // do something fancy with the path, like decomposing it into 
    // parts, retrieving the entry from the DB, creating an entry object,
    // and setting it as a request attribute
    // simulated here :-)
and said "any decent Java programmer could not help but create at least a small library to help with this, but that’s a far cry from a full-featured open source over-hyped Web framework".

Michael Strasser: "/st/2007/08/15/java_web_frameworks.html: This string is available to the servlet using request.getPathInfo(). The servlet tokenises the string and works with the information."

I would encourage anyone to think for a bit about out how that parsing/mapping might work. And then take a look at this: dajngo url dispatching.

Hugh Winkler: "It is a micro framework. You subclass RestServlet and declare some URL patterns to match, and handlers for them. The base class parses the URI, sets attributes in the ServletRequest object based on the URI pattern, and invokes your handlers.". Hugh is as smart as paint; if you follow the link, he gets into a good bit of detail.

There are three interesting conclusions for me. First, the Servlets spec can't support what Harry Fuecks calls "Parameterized REST URLs", and strictly speaking, you need more than Servlets+JSP. Second, that more seems to be a way to declare mappings, since ultimately you'll want to be able to state what URL maps to what JSP/code outside Servlet code. Third, the API surface area to support this kind of feature seems to be very manageable. To be honest, I was worried I was missing something, but that's I was thinking when i wrote the post - it can't be "done", without introducing a "micro" or "full" framework, for URL mapping support. A Servlets+JSP setup isn't powerful enough to quantify over the kind of URL spaces that are become normal (sets of resources). The Servlets spec is designed to let you quantify over scripts (showing that CGI is foundational to Servlets). Web.xml lets you match a pattern to a servlet, and the matching ('url-pattern') is basic:

  /blog
  /blog/entry*
  /*
  *.html
  *.jsp

that sort of thing. JSPs by default in Tomcat map to a single servlet and what you can do there is pass query params. By the way, Resin supports Perl5 regex mappings in web.xml but it's a non-standard extension.

Sam placed some long bets recently. Here's some short ones, say two years. First, web frameworks will come to adopt declarative mapping files, either regex based a la Django and SpringMVC, or URI Template based. Second, JSR311 will be a foundation technology for Java based web systems - it supports URI Templates. Incidentally, the URL the JCP gives out is http://jcp.org/en/jsr/detail?id=311, but never mind. Paul Sandoz has started the RI for JSR311, called Jersey. Things start to look like this:

1    // The Java class will be hosted at the URI path "/helloworld"
 2    @UriTemplate("/helloworld")
 3    public class HelloWorldResource {
 4    
 5        // The Java method will process HTTP GET requests
 6        @HttpMethod("GET") 
 7        // The Java method will produce content identified by the MIME Media
 8        // type "text/plain"
 9        @ProduceMime("text/plain")
10        public String getClichedMessage() {
11            // Return some cliched textual content
12            return "Hello World";
13        }
14    }

which fits in nicely with modern POJO/annotation style development. I can't wait to see this kind of thing integrated into the Servlet containers



August 15, 2007 08:55 PM