The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Anatomy of a Smalltalk Servlet

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
Anatomy of a Smalltalk Servlet Posted: Oct 24, 2005 11:36 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Anatomy of a Smalltalk Servlet
Feed Title: Cincom Smalltalk Blog - Smalltalk with Rants
Feed URL: http://www.cincomsmalltalk.com/rssBlog/rssBlogView.xml
Feed Description: James Robertson comments on Cincom Smalltalk, the Smalltalk development community, and IT trends and issues in general.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Cincom Smalltalk Blog - Smalltalk with Rants

Advertisement

In VisualWorks, the Web Toolkit makes it pretty easy to create ASP/JSP style applications, complete with support for servlets. The way you get a servlet invoked is the same whether you use Smalltalk or anything else; you simply name the servlet appropriately at the client side, in the web page.

Take this blog, for example - there's a login page for the site bloggers to use if they want to post from the web form or use the online admin tools - here's the snippet from the SSP (Smalltalk Server Page):



<form action="servlet/LoginServlet" method="post" name="blogLoginForm">

Looks pretty normal, doesn't it? That's the point - you don't have to do anything special from the client side to support the web toolkit. You can even use JSP tags - I have no experience with that, but there are examples that ship with the product. How do things look on the server? This form is very basic - two fields, one for username, one for password. Here's the servlet definition:


Smalltalk.Blog defineClass: #LoginServlet
	superclass: #{Blog.AbstractServlet}
	indexedType: #none
	private: false
	instanceVariableNames: ''
	classInstanceVariableNames: ''
	imports: ''
	category: 'Blog'

AbstractServlet is a simple subclass of SingleThreadModelServlet, with some code specific to the needs of the blog server. There are two servlet classes in VW - the one I just mentioned, and HttpServlet. What's the difference? HttpServlet passes the request and response as arguments into the API methods, while SingleThreadModelServlet passes them in as instance variables - i.e., it maintains them as state information. Which one is right depends on your usage.

The entry point for a post servlet is going to be #doPost: or #doPost:response: - the latter for the stateless model. Here's how that looks in the LoginServlet:


doPost
	| model userOrNil |
	model := BlogUser new.
	model appkey: self appkey.
	model getInputFrom: request parameters.
	userOrNil := model validate.
	userOrNil notNil 
		ifTrue: [self session initialize.
				self session at: 'currentUser' put: userOrNil.
				self determineDestinationFor: userOrNil]
		ifFalse: [self redirectTo: (self handler linkNamed: 'blogLogin')]

Basically, I grab the input, ask it to validate (which checks the input against the valid users of this blog), and then determine where login should take them. In general, there are two types of users - people who can post, and people who can post and have admin rights. The method #determineDestinationFor: redirects valid users to the appropriate page, based on their access rights.

And that's pretty much it - the heart of this servlet is that one method, with the supporting code being simple lookup stuff. The bottom line - getting a Smalltalk servlet working is pretty easy - and, since it's Smalltalk, you can debug the code during test. In fact, when I was refining the MetaWebLog API servlet, I did just that - one of the bloggers on the site tested a client against my test server, and I had breakpoints in to see what I was being sent (and more importantly, what I was expected to send back). The fact that the servlet was just another Smalltalk object - rather than a file artifact that would have to be loaded, flushed, and reloaded repeatedly - made the whole things a lot easier to deal with.

Read: Anatomy of a Smalltalk Servlet

Topic: Wednesday Too Previous Topic   Next Topic Topic: Mass Comment Deletion

Sponsored Links



Google
  Web Artima.com   

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