# Multiple scripts accessing one WEBrick session?

**URL:** <https://www.ruby-forum.com/t/multiple-scripts-accessing-one-webrick-session/102424>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/multiple-scripts-accessing-one-webrick-session/102424 "2006-08-15T14:17:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@Naren](https://www.ruby-forum.com/u/Naren)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/multiple-scripts-accessing-one-webrick-session/102424/1 "2006-08-15T14:17:04Z")

</div>

I want to set up a a machine as a servlet container using WEBrick. But  
I want to be able to add several servlets, hopefully from separate Ruby  
scripts. So here is the sample to start one servlet right,

====================  
require “webrick”

server = WEBrick::HTTPServer.new(:Port =\> 8080, :DocumentRoot =\>  
“C:\WWW”)  
trap(“INT”) {server.shutdown}

class Servlet\_A \< WEBrick::HTTPServlet::AbstractServlet  
HEAD = “My Page”  
def do\_GET(req, res)  
var1 = req.query[‘var1’]  
var2 = req.query[‘var2’]  
result = DoStuff.new( var1, var2 )  
res.body = “#{HEAD}

## My Page
#{result}”  
end  
end
# server.mount("/pl", PLServlet) server.start

Now I want to have another script that will have the implementation for  
Servlet\_B. But I want to mount that on the same server.

Any ideas on how to do that? Can it be done?

TIA  
Naren

---

<div class="post-metadata">

**Author:** ![Naren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@Naren](https://www.ruby-forum.com/u/Naren)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/multiple-scripts-accessing-one-webrick-session/102424/2 "2006-08-15T14:17:56Z")

</div>

Hi,

On 8/11/06, Naren [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Naren  
> For one-file version:  
> Just add your servlets and respective server.mount(url, servlet)  
> linesto the script.

For more file:  
version 1: create files per servlet, and require them into main script  
(that will still contain those mount lines)

version 2:  
file “base\_servlet.rb”:  
require ‘webrick’  
class MyBaseServlet \< WEBrick::HTTPServlet::AbstractServlet  
class \<\< self  
attr\_accessor :url  
end  
end

for each servlet:

require ‘base\_servlet’  
class ServletA \< BaseServlet  
self.url =‘/pl’  
… the rest of the code  
end

file ‘server.rb’  
require “webrick”  
#(1)  
server = WEBrick::HTTPServer.new(:Port =\> 8080, :DocumentRoot =\>  
“C:\WWW”)  
trap(“INT”) {server.shutdown}

ObjectSpace.each\_object(Class) do |servlet|  
server.mount(servlet.url, servlet) if servlet \< BaseServlet  
end  
server.start

and either require all the servlets at (1) or start the server with  
ruby -rservlet\_a -rservlet\_b … server.rb

J.

---

<div class="post-metadata">

**Author:** ![Naren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/73ab20/32.png) [@Naren](https://www.ruby-forum.com/u/Naren)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/multiple-scripts-accessing-one-webrick-session/102424/3 "2006-08-15T14:18:01Z")

</div>

Thanks Jan, I am quite new with Ruby, so I am grateful for your example  
code. In addition to answering my question, I also got some generally  
Ruby programming style tips out of it. 🙂 What I was hoping I could do  
was to mount and unmount servlets independent of each other… while  
the other ones were still up and running. Your current solution  
(version 2) will work for now, cause I only have 3 or 4 servlets now.  
But overall I think I might have around 20, then I would like to have  
independent control. I guess I might look into other containers than  
WEBrick at that time.

Thanks again.

Naren
