Type:
Class

AbstractServlet allows HTTP server modules to be reused across multiple servers and allows encapsulation of functionality.

By default a servlet will respond to GET, HEAD (through an alias to GET) and OPTIONS requests.

By default a new servlet is initialized for every request. A servlet instance can be reused by overriding ::get_instance in the AbstractServlet subclass.

A Simple Servlet

1
2
3
4
5
6
7
8
9
10
11
12
13
class Simple < WEBrick::HTTPServlet::AbstractServlet
  def do_GET request, response
    status, content_type, body = do_stuff_with request
 
    response.status = status
    response['Content-Type'] = content_type
    response.body = body
  end
 
  def do_stuff_with request
    return 200, 'text/plain', 'you got a page'
  end
end

This servlet can be mounted on a server at a given path:

1
server.mount '/simple', Simple

Servlet Configuration

Servlets can be configured via initialize. The first argument is the HTTP server the servlet is being initialized for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Configurable < Simple
  def initialize server, color, size
    super server
    @color = color
    @size = size
  end
 
  def do_stuff_with request
    content = "<p " \
              %q{style="color: #{@color}; font-size: #{@size}"} \
              ">Hello, World!"
 
    return 200, "text/html", content
  end
end

This servlet must be provided two arguments at mount time:

1
server.mount '/configurable', Configurable, 'red', '2em'
do_HEAD
  • References/Ruby on Rails/Ruby/Classes/WEBrick/WEBrick::HTTPServlet/WEBrick::HTTPServlet::AbstractServlet

do_HEAD(req, res) Instance Public methods Dispatches to

2025-01-10 15:47:30
service
  • References/Ruby on Rails/Ruby/Classes/WEBrick/WEBrick::HTTPServlet/WEBrick::HTTPServlet::AbstractServlet

service(req, res) Instance Public methods Dispatches to a do_ method

2025-01-10 15:47:30
do_OPTIONS
  • References/Ruby on Rails/Ruby/Classes/WEBrick/WEBrick::HTTPServlet/WEBrick::HTTPServlet::AbstractServlet

do_OPTIONS(req, res) Instance Public methods Returns the allowed HTTP request

2025-01-10 15:47:30
get_instance
  • References/Ruby on Rails/Ruby/Classes/WEBrick/WEBrick::HTTPServlet/WEBrick::HTTPServlet::AbstractServlet

get_instance(server, *options) Class Public methods Factory for servlet instances

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/WEBrick/WEBrick::HTTPServlet/WEBrick::HTTPServlet::AbstractServlet

new(server, *options) Class Public methods Initializes a new servlet for server

2025-01-10 15:47:30
do_GET
  • References/Ruby on Rails/Ruby/Classes/WEBrick/WEBrick::HTTPServlet/WEBrick::HTTPServlet::AbstractServlet

do_GET(req, res) Instance Public methods Raises a NotFound exception

2025-01-10 15:47:30