日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Servlet例子由Ruby來(lái)實(shí)現(xiàn)

Ruby也能寫(xiě)servlet?是的,沒(méi)開(kāi)玩笑,而且挺方便的,因?yàn)镽uby的標(biāo)準(zhǔn)庫(kù)就自帶了一個(gè)webrick,webrick本身又有一個(gè)serlvet容器,隨時(shí)隨地啟動(dòng)一個(gè)web server,實(shí)在是很方便。

雙塔ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!

先看個(gè)最簡(jiǎn)單的例子,輸出hello到瀏覽器:

 
 
 
  1. require 'webrick' 
  2. require 'net/http' 
  3. include WEBrick  
  4.     
  5. class HelloServlet < HTTPServlet::AbstractServlet  
  6.  def hello(resp)  
  7.   resp["Content-Type"]="text/html;charset=utf-8" 
  8.   resp.body="hello,ruby servlet" 
  9.  end 
  10.  private :hello 
  11.  def do_GET(req,resp)  
  12.   hello(resp)  
  13.  end 
  14.  def do_POST(req,resp)  
  15.   hello(resp)  
  16.  end 
  17. end 
  18. if $0==__FILE__  
  19.  server=HTTPServer.new(:Port=>3000)  
  20.  server.mount("/hello",HelloServlet)  
  21.  trap("INT"){ server.shutdown }  
  22.  server.start  
  23. end 

是不是跟java很像?所有的serlvet都要繼承自HTTPServlet::AbstractServlet,并實(shí)現(xiàn)do_GET或者do_POST方法。在這行代碼:

 
 
 
  1.   server=HTTPServer.new(:Port=>3000) 

我們啟動(dòng)了一個(gè)HTTP Server,端口是3000,然后將HelloServlet掛載到/hello這個(gè)路徑上,因此,執(zhí)行這個(gè)腳本后,可以通過(guò)http://localhost:3000/hello調(diào)用HelloServlet,簡(jiǎn)單地只是顯示字符串"hello,ruby servlet"。

這個(gè)簡(jiǎn)單的例子沒(méi)有任何交互,并且顯示的html也是寫(xiě)死在腳本中,顯然更好的方式應(yīng)該通過(guò)模板來(lái)提供,可以使用Ruby標(biāo)準(zhǔn)庫(kù)的erb模板。再給個(gè)有簡(jiǎn)單交互的例子,現(xiàn)在要求用戶輸入姓名,然后提交給HelloServlet,顯示"hello,某某某"。嗯,來(lái)個(gè)最簡(jiǎn)單的提交頁(yè)面:

 
 
 
  1. ﹤html﹥  
  2. ﹤body﹥  
  3. ﹤center﹥  
  4.   ﹤form action="http://localhost:3000/hello" method="post"﹥  
  5.    ﹤input type="text" name="name" size=10/﹥﹤br/﹥﹤br/﹥  
  6.    ﹤input type="submit" name="submit" value="submit"/﹥  
  7.   ﹤/form﹥  
  8.   ﹤/center﹥  
  9. ﹤/body﹥  
  10. ﹤/html﹥ 

注意到,我們采用POST方法提交。再看看erb模板:

 
 
 
  1. ﹤html﹥  
  2.   ﹤head﹥﹤/head﹥  
  3.   ﹤body﹥  
  4.    hello,﹤%=name%﹥  
  5.   ﹤/body﹥  
  6. ﹤/html﹥ 

其中的name是我們將要綁定的變量,根據(jù)用戶提交的參數(shù)。***,修改下HelloServlet:

 
 
 
  1. require 'webrick' 
  2. require 'net/http' 
  3. include WEBrick  
  4.     
  5. class HelloServlet < HTTPServlet::AbstractServlet  
  6.  def do_GET(req,resp)  
  7.   do_POST(req,resp)  
  8.  end 
  9.  def do_POST(req,resp)  
  10.   name=req.query["name"]  
  11.   #讀取模板文件  
  12.   template=IO.read(File.dirname(__FILE__)+"/hello.html")  
  13.   message=ERB.new(template)  
  14.   resp["Content-Type"]="text/html;charset=utf-8" 
  15.   resp.body=message.result(binding)  
  16.  end 
  17. end 
  18. if $0==__FILE__  
  19.  server=HTTPServer.new(:Port=>3000)  
  20.  server.mount("/hello",HelloServlet)  
  21.  trap("INT"){ server.shutdown }  
  22.  server.start  
  23. end 

與前一個(gè)例子相比,不同點(diǎn)有二,一是通過(guò)req.query["name"]獲得用戶提交的參數(shù)name,二是resp的body是由模板產(chǎn)生,而不是寫(xiě)死在代碼中。在一些臨時(shí)報(bào)表、臨時(shí)數(shù)據(jù)的展示上,可以充分利用Ruby的這些標(biāo)準(zhǔn)庫(kù)來(lái)快速實(shí)現(xiàn)。


網(wǎng)頁(yè)名稱:Servlet例子由Ruby來(lái)實(shí)現(xiàn)
URL網(wǎng)址:http://www.5511xx.com/article/dpecoee.html