Index: [Article Count Order] [Thread]

Date: Wed, 10 Jul 2002 11:43:19 +0900
From: "Norman Makoto Su" <normsu@slab.tnr.sharp.co.jp>
Subject: [webricken:108] Persistant Servlets
To: <webricken@notwork.org>
Message-Id: <007b01c227bb$8d13fc20$dd31200a@JAZZ>
X-Mail-Count: 00108

Hi,

In this example, "Count: 0" is printed each time the browser access the servlet,
/hello.

#!/usr/local/bin/ruby
require 'webrick'
include WEBrick

s = HTTPServer.new( :Port => 2000 )
class HelloServlet < HTTPServlet::AbstractServlet
  def initialize(server, *options)
    @i = 0
  end

  def do_GET(req, res)
    res.body = "<HTML>Count #{@i}</HTML>"
    res['Content-Type'] = "text/html"
    @i += 1
  end
end
s.mount("/hello", HelloServlet)
trap("INT"){ s.shutdown }
s.start

When using WEBrick, it seems that a new servlet instance is created each time a
client accesses it.  If so, is there anyway to prevent this (so that only one
instance of the servlet is created initially and all subsequent accesses are
referred to the same servlet)?  In other words, I want it to print out "Count
0", "Count 1", etc.

Thanks,

--
Norman Makoto Su