initializing a servlet in WEBRick

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default initializing a servlet in WEBRick

    I am writing a web application where all the requests are served by the
    same object, which needs initialization. This is being done this way:

    <<initialize some stuff from the command line parameters>>
    <<initialize the driver of the application from that stuff>>
    server.mount_proc(path) { |req, res| driver.process(req, res) }

    That seems to work fine, but I wonder if the driver could be a subclass
    of HTTPServlet::AbstractServlet so that one would just say

    server.mount(path, DriverServlet)

    and have one instance serving all the pages and initialized as needed. I
    have tried to figure it out from the sources but don't see how would it
    work.

    -- fxn


    Xavier Noria Guest

  2. Similar Questions and Discussions

    1. Webrick doc
      I am dumb. Can anyone point to documentation of Webrick? Yes, it is built-in in 1.8, but how do I read/study about it? Tips pls. Thanks in...
    2. CGI and Webrick
      Hey all, I've just started poking at three Ruby tools I haven't yet used: DBI, Webrick, and CGI, and have a few questions: * Is there good...
    3. webrick
      I'm looking for a way to implement a standard webrick HTTP server but with a special feature : before serve a HTML page it replace all %foo%...
    4. Webrick and CGI question
      How do I tell Webrick to use ruby to execute a cgi program? The following code example just shows me the script and not the result of executing the...
    5. WeBrick,mod_ruby
      It appears as if WeBrick has become the standard for a "servlet model" of web development. Is this correct? Is notice there are a lot of...
  3. #2

    Default Re: initializing a servlet in WEBRick

    Hi,

    In message <200309071203.04502.fxn@hashref.com>,
    `Xavier Noria <fxn@hashref.com>' wrote:
    > I am writing a web application where all the requests are served by the
    > same object, which needs initialization. This is being done this way:
    >
    > <<initialize some stuff from the command line parameters>>
    > <<initialize the driver of the application from that stuff>>
    > server.mount_proc(path) { |req, res| driver.process(req, res) }
    >
    > That seems to work fine, but I wonder if the driver could be a subclass
    > of HTTPServlet::AbstractServlet so that one would just say
    >
    > server.mount(path, DriverServlet)
    >
    > and have one instance serving all the pages and initialized as needed. I
    > have tried to figure it out from the sources but don't see how would it
    > work.
    If it's explained simply, servelets are activated in the
    following sequence:

    << accept connection and receive HTTP request >>
    << find a ServletClass associated with path of URL >>

    # the third and more arguments of mount() will be passed to options.
    servlet = ServletClass.get_instance(server, *options)
    servlet.service(req, res)

    << send response >>

    If it can respond to get_instance and service, any kind of
    object can be mounted as a servelet.

    AbstractServlet::get_instance just calls new() and instance
    of servelet will be created for every request. Conversely
    singleton servelet can be up by overriding of get_instance.

    The following sample implements get_instance as a instance
    method and initialize a servlet out of server before it's
    started.

    require 'webrick'

    class AccessCounter < WEBrick::HTTPServlet::AbstractServlet
    def initialize(server)
    @mutex = Mutex.new # service should be MT-safe
    @count = 0
    end
    def get_instance(server)
    self # always returns same instance
    end
    def service(req, res)
    @mutex.synchronize{ super }
    end
    def do_GET(req, res)
    res.body = "<html><body>@count=<b>#{@count+=1}</b></body></html>"
    res['content-type'] = "text/html"
    end
    end

    svr = WEBrick::HTTPServer.new(:Port => 10080)
    counter = AccessCounter.new(svr)
    svr.mount("/", counter)
    svr.start

    --
    gotoyuzo

    GOTOU Yuuzou Guest

  4. #3

    Default Re: initializing a servlet in WEBRick

    On Sunday 07 September 2003 15:43, GOTOU Yuuzou wrote:
    > If it's explained simply, servelets are activated in the
    > following sequence:
    >
    > << accept connection and receive HTTP request >>
    > << find a ServletClass associated with path of URL >>
    >
    > # the third and more arguments of mount() will be passed to
    > options. servlet = ServletClass.get_instance(server, *options)
    > servlet.service(req, res)
    >
    > << send response >>
    >
    > If it can respond to get_instance and service, any kind of
    > object can be mounted as a servelet.
    << cut >>

    That's very helpful, thank you very much GOTOU.

    The code shouldn't depend on the implementation of WEBrick however, do
    you think we can rely on the outlined sequence

    * << accept connection and receive HTTP request >>
    * << find a ServletClass associated with path of URL >>

    * # the third and more arguments of mount() will be passed to options.
    * servlet = ServletClass.get_instance(server, *options)
    * servlet.service(req, res)

    * << send response >>

    ?

    -- fxn

    PS: I am sorry for the typo in the subject.


    Xavier Noria Guest

  5. #4

    Default Re: initializing a servlet in WEBRick

    In message <200309071717.08089.fxn@hashref.com>,
    `Xavier Noria <fxn@hashref.com>' wrote:
    > On Sunday 07 September 2003 15:43, GOTOU Yuuzou wrote:
    > > If it can respond to get_instance and service, any kind of
    > > object can be mounted as a servelet.
    >
    > << cut >>
    >
    > That's very helpful, thank you very much GOTOU.
    >
    > The code shouldn't depend on the implementation of WEBrick however, do
    > you think we can rely on the outlined sequence
    >
    > * << accept connection and receive HTTP request >>
    > * << find a ServletClass associated with path of URL >>
    >
    > * # the third and more arguments of mount() will be passed to options.
    > * servlet = ServletClass.get_instance(server, *options)
    > * servlet.service(req, res)
    >
    > * << send response >>
    Yes, requirement of get_instance and service is the spec
    of WEBrick::HTTPServer. I guess it will not be changed.
    # at least in Ruby-1.8..

    By the way, the code of servlet activation is described in
    HTTPServer#service. We can rewrite it to build specific
    purpose server or realize another idea about uri-to-service
    mapping.

    require "webrick"
    class NotFoundServer < WEBrick::HTTPServer
    def service(req, res)
    # returns 404 against every request.
    raise WEBrick::HTTPStatus::NotFound
    end
    end
    NotFoundServer.new(:Port=>10080).start

    --
    gotoyuzo

    GOTOU Yuuzou Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139