[mod_python] using callable classes instead of plain functions in handlers

Daniel Popowich dpopowich at comcast.net
Mon Oct 4 10:34:16 EDT 2004


> > I just built a very comprehensive content management system with
> > mpservlets that does what you want.  ONE servlet manages the whole
> > site.  It does this via PATH_INFO.  If you're not familiar with it you
> > should find out about it because it can be a very powerful tool.
> > Briefly...
> > 
> > If you have a URI, /some/dir/target, and a request comes in for
> > /some/dir/target/x/y/z then /some/dir/target will get the request with
> > PATH_INFO set to x/y/z.  Think of it as extraneous path information
> > that was passed along with the request.  Many systems use this to
> > create "clean" urls.
> > 
> > Now, if you have a servlet, /some/dir/myservlet, and a request comes
> > in for /some/dir/myservlet/this/file, then myservlet will have an
> > instance variable, path_info, set to ['this', 'file'].  (Aside: if a
> > request has no PATH_INFO then the path_info instance variable is set
> > to an empty list.)  Using this mechanism you can manage a site with a
> > few servlets using path_info as a lookup into a database or a
> > filesystem.
> 
> Thanks for the reply, Dan. I commend you in your support efforts.
> 
> If you use servlets the way you have described what do you see as the
> the differences and/or benefits servlets has over the publisher handler,
> which effectively does the same thing?

The difference, in a nutshell: object-oriented programming vs.
functional programming.

I prefer OO programming: by loading Servlet and HTMLPage with common
functionality (automatic form processing, session management, HTML
generation) that's less code I have to write per handler.  By
enforcing a lifecycle to requests (see
http://lnx1.blue-fox.com/~dpopowich/mpstutorial/lifecycle) my code is
now better organized, easier to read and easier to maintain.

And just recently someone was asking about integrating PSP with
servlets.  I suggested subclassing Servlet directly to create a
PSPServlet class.  Now they can have all the goodies in Servlet
automatically.  BTW, as an aside: while I was developing mpservlets I
had only one class, HTMLPage, but realized that was an error in
design because there was a lot of functionality that was generic and
not specific to serving requests for HTML.  I took all the non-HTML
code and put it in a superclass, what is now Servlet (thus the
module's name).  This is an example of the power of OO programming
(if you get the object model correct!!!) because now someone can use
Servlet directly without the bother of HTML generation.

Sure, you can do all that with functions, too, by building up a
library of functionality in a common module, but then the discussion
becomes functional vs. oo programming.  IMHO, that's a religious war
not suited for this list.  :-)

Cheers,

Daniel Popowich
-----------------------------------------------
http://home.comcast.net/~d.popowich/mpservlets/




More information about the Mod_python mailing list