Ruby寫Servlet的小例子
Ruby也能寫servlet?是的,沒開玩笑,而且挺方便的,因為Ruby的標準庫就自帶了一個webrick,webrick本身又有一個serlvet容器,隨時隨地啟動一個web server,實在是很方便。先看個最簡單的例子,輸出hello到瀏覽器:
require 'webrick'
require 'net/http'
include WEBrick
class HelloServlet < HTTPServlet::AbstractServlet
def hello(resp)
resp["Content-Type"]="text/html;charset=utf-8"
resp.body="hello,ruby servlet"
end
private :hello
def do_GET(req,resp)
hello(resp)
end
def do_POST(req,resp)
hello(resp)
end
end
if $0==__FILE__
server=HTTPServer.new(:Port=>3000)
server.mount("/hello",HelloServlet)
trap("INT"){ server.shutdown }
server.start
end
是不是跟java很像?所有的serlvet都要繼承自HTTPServlet::AbstractServlet,並實現do_GET或者do_POST方法。在這行代碼:require 'net/http'
include WEBrick
class HelloServlet < HTTPServlet::AbstractServlet
def hello(resp)
resp["Content-Type"]="text/html;charset=utf-8"
resp.body="hello,ruby servlet"
end
private :hello
def do_GET(req,resp)
hello(resp)
end
def do_POST(req,resp)
hello(resp)
end
end
if $0==__FILE__
server=HTTPServer.new(:Port=>3000)
server.mount("/hello",HelloServlet)
trap("INT"){ server.shutdown }
server.start
end
server=HTTPServer.new(:Port=>3000)
我們啟動了一個HTTP Server,端口是3000,然後將HelloServlet掛載到/hello這個路徑上,因此,執行這個腳本後,可以通過https://localhost:3000/hello調用HelloServlet,簡單地隻是顯示字符串"hello,ruby servlet"。
這個簡單的例子沒有任何交互,並且顯示的html也是寫死在腳本中,顯然更好的方式應該通過模板來提供,可以使用Ruby標準庫的erb模板。再給個有簡單交互的例子,現在要求用戶輸入姓名,然後提交給HelloServlet,顯示"hello,某某某"。嗯,來個最簡單的提交頁麵:
<html>
<body>
<center>
<form action="https://localhost:3000/hello" method="post">
<input type="text" name="name" size=10/><br/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
</center>
</body>
</html>
<body>
<center>
<form action="https://localhost:3000/hello" method="post">
<input type="text" name="name" size=10/><br/><br/>
<input type="submit" name="submit" value="submit"/>
</form>
</center>
</body>
</html>
注意到,我們采用POST方法提交。再看看erb模板:
<html>
<head></head>
<body>
hello,<%=name%>
</body>
</html>
其中的name是我們將要綁定的變量,根據用戶提交的參數。最後,修改下HelloServlet:<head></head>
<body>
hello,<%=name%>
</body>
</html>
require 'webrick'
require 'net/http'
include WEBrick
class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req,resp)
do_POST(req,resp)
end
def do_POST(req,resp)
name=req.query["name"]
#讀取模板文件
template=IO.read(File.dirname(__FILE__)+"/hello.html")
message=ERB.new(template)
resp["Content-Type"]="text/html;charset=utf-8"
resp.body=message.result(binding)
end
end
if $0==__FILE__
server=HTTPServer.new(:Port=>3000)
server.mount("/hello",HelloServlet)
trap("INT"){ server.shutdown }
server.start
end
require 'net/http'
include WEBrick
class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req,resp)
do_POST(req,resp)
end
def do_POST(req,resp)
name=req.query["name"]
#讀取模板文件
template=IO.read(File.dirname(__FILE__)+"/hello.html")
message=ERB.new(template)
resp["Content-Type"]="text/html;charset=utf-8"
resp.body=message.result(binding)
end
end
if $0==__FILE__
server=HTTPServer.new(:Port=>3000)
server.mount("/hello",HelloServlet)
trap("INT"){ server.shutdown }
server.start
end
與前一個例子相比,不同點有二,一是通過req.query["name"]獲得用戶提交的參數name,二是resp的body是由模板產生,而不是寫死在代碼中。在一些臨時報表、臨時數據的展示上,可以充分利用Ruby的這些標準庫來快速實現。
文章轉自莊周夢蝶 ,原文發布時間2008-07-23
最後更新:2017-05-17 18:31:29