In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Ruby to implement Servlet. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Ruby can also write servlet? No kidding, and it's convenient, because Ruby's standard library comes with a webrick,webrick and a serlvet container, and it's convenient to start a web server anytime, anywhere.
Take a look at the simplest example and output hello to the browser:
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
Is it a lot like java? All serlvet inherits from HTTPServlet::AbstractServlet and implements do_GET or do_POST methods. In this line of code:
Server=HTTPServer.new (: Port= > 3000)
We started a HTTP Server on port 3000, and then mounted the HelloServlet to the path / hello, so after executing the script, we can call HelloServlet through http://localhost:3000/hello and simply display the string "hello,ruby servlet".
This simple example has no interaction, and the displayed html is also written in a script, so obviously a better way to provide it is through a template, using the erb template of the Ruby standard library. To give another example of a simple interaction, the user is now asked to enter a name and submit it to HelloServlet, showing "hello, so-and-so". Well, let's make the simplest submission page:
< html > < body > < center > < form action= "http://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 >
Notice that we use the POST method to submit. Take a look at the erb template:
< html > < head > < / head > < body > hello, <% = name% > < / body > < / html >
Where name is the variable we are going to bind, according to the parameters submitted by the user. *, modify the HelloServlet:
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
Compared with the previous example, there are two differences, one is that the parameter name submitted by the user is obtained through req.query ["name"], and the other is that the body of resp is generated by the template rather than written in the code. In the display of some temporary reports and temporary data, we can make full use of these standard libraries of Ruby to achieve it quickly.
Thank you for reading! This is the end of this article on "how to use Ruby to achieve Servlet". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.