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