The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Servlets.. kinda a bit javaish for us isn't it?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Servlets.. kinda a bit javaish for us isn't it? Posted: Jan 25, 2007 4:21 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Servlets.. kinda a bit javaish for us isn't it?
Feed Title: Michael Lucas-Smith
Feed URL: http://www.michaellucassmith.com/site.atom
Feed Description: Smalltalk and my misinterpretations of life
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Michael Lucas-Smith

Advertisement

So it's nearly midnight here and I'm working on a http server idea. I'm using HttpServerServlets which is part of my streaming HTTP/1.1 server and every time I want a new 'servlet' I have to make a subclass of the class Servlet.

This is pretty much bespoke as to the way servlets were designed for Java. This is also pretty much the way VW and VA offer you the ability to do servlets. Subclass the servlet class and implement a do*Method: method and you're away. Pretty neat, but... often each class has one method in it to return some sort of dynamically generated page.

So I got thinking about pragmas again - or as Travis wants to call them: "tags". I thought, what if I could just make one class and put all these adhoc response methods in there using tags. So I've come up with a couple of tags you can put in to a subclass of HttpServerTags.TagServer.

static_content


^'Hello World'

In this simple example we say that we respond to a GET request with path of /test.txt and our content type is text/plain. When we request the page, we get back a string of Hello World. Pretty simple and to the point. Let's get a bit more interesting:

streaming_content: request stream: stream



600 timesRepeat: [
(Delay forMilliseconds: 6) wait.
stream asStringStream nextPutAll: Timestamp now printString; cr.
stream flush]

In this example, we respond to /streaming.txt and it will disable any compression so that we can see a streaming response in our web browser immediately. It will send us the next timestamp for the next 10 seconds.

Pretty neat isn't it - we get past a lot of the boring setup we'd normally have to do to make a response such as setting up contentType: on the response object ourselves. Let's get more interesting.

session_info: request writer: writer



writer html: [:html | html body div text: request session printString].

In this example, we say we're outputting html - this takes care of the content type for us, but more interestingly, it also sets us up with my new XMLWriter object that lets us quickly dump out some xml without having to write it all ourselves. The XMLWriter itself is a stream, so you cannot go back and add attributes to a tag you've already added children to or add more children to a prior sibling etc.

Instead of using we can just as easily also use which does the same thing, except it sets the content-type to be text/xml.

So in the example above, we get our session set up for us as well, simply by putting the session pragma in there.

One more example - replying with files. Because of the way we do the matching on get:, post:, put:, delete: pragmas, they actually match as a prefix to the uri path, so we can do the following:

scripts: request

self respondFromDisk: request

This picks up the disk response behaviour which does last-modifies, 404 not founds, etc for you as well. I'm pretty pleased with the way this Servlet replacement turned out. Give it a go and let me know what you think too. The package that enables this is called HttpServerTags and is now available in Public Store.

Read: Servlets.. kinda a bit javaish for us isn't it?

Topic: Something the Enterprisey won't grok Previous Topic   Next Topic Topic: SUnitToo Coverage

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use