The Servlet example is implemented by Ruby.

Source: Internet
Author: User

Ruby can also write servlet? Yes, it's not a joke, and it's quite convenient, because the Ruby standard library comes with a webrick, and webrick itself has a serlvet container that can start a web server anytime and anywhere.

Let's take a look at the simplest example and output hello to the browser:

 
 
  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 

Is it similar to java? All serlvet must inherit from HTTPServlet: AbstractServlet and implement the do_GET or do_POST methods. In this line of code:

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

We started an HTTP Server with port 3000 and mounted HelloServlet to the/hello path. Therefore, after executing this script, you can use http: // localhost: 3000/hello calls HelloServlet, simply displaying the string "hello, ruby servlet ".

This simple example does not have any interaction, and the html displayed is also written to the script. Obviously, it is better to use the template to provide it. You can use the erb template of the Ruby standard library. Let's give an example of simple interaction. Now, you need to enter your name and submit it to HelloServlet to display "hello, xx ". Well, here is the simplest page to submit:

 
 
  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﹥ 

Note that we use the POST method for submission. Let's look at the erb template:

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

The name is the variable to be bound according to the parameters submitted by the user. Finally, modify HelloServlet:

 
 
  1. Require'Webrick' 
  2. Require'Net/http' 
  3. Include WEBrick
  4. ClassHelloServlet <HTTPServlet: AbstractServlet
  5.  DefDo_GET (req, resp)
  6. Do_POST (req, resp)
  7.  End 
  8.  DefDo_POST (req, resp)
  9. Name = req. query ["Name"]
  10. # Reading template files 
  11. Template =IO. Read (File. Dirname (_ FILE _) +"/Hello.html")
  12. Message = ERB.New(Template)
  13. Resp ["Content-Type"] ="Text/html; charset = UTF-8" 
  14. Resp. body = message. result (binding)
  15.  End 
  16. End 
  17. If $0==__ FILE __
  18. Server = HTTPServer.New(Port => 3000)
  19. Server. mount ("/Hello", HelloServlet)
  20. Trap ("INT") {Server. shutdown}
  21. Server. start
  22. End 

Compared with the previous example, there are two differences. One is through req. query ["name"] obtains the name of the parameter submitted by the user. Second, the resp body is generated by the template, rather than being written to the code. You can use Ruby's standard libraries to quickly display temporary reports and data.

  1. Java Servlet API documentation
  2. Integrate JSP and PHP in Apache
  3. Java Servlets (JSP) Development Environment
  4. Configuration of JSP, Servlet, and Bean in Tomcat
  5. How to Improve Servlet and JSP Application Efficiency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.