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:
- 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 similar to java? All serlvet must inherit from HTTPServlet: AbstractServlet and implement the do_GET or do_POST methods. In this line of code:
- 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:
- ﹤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﹥
Note that we use the POST method for submission. Let's look at the erb template:
- ﹤html﹥
- ﹤head﹥﹤/head﹥
- ﹤body﹥
- hello,﹤%=name%﹥
- ﹤/body﹥
- ﹤/html﹥
The name is the variable to be bound according to the parameters submitted by the user. Finally, modify HelloServlet:
- Require'Webrick'
- Require'Net/http'
- Include WEBrick
- ClassHelloServlet <HTTPServlet: AbstractServlet
- DefDo_GET (req, resp)
- Do_POST (req, resp)
- End
- DefDo_POST (req, resp)
- Name = req. query ["Name"]
- # Reading template files
- 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 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.
- Java Servlet API documentation
- Integrate JSP and PHP in Apache
- Java Servlets (JSP) Development Environment
- Configuration of JSP, Servlet, and Bean in Tomcat
- How to Improve Servlet and JSP Application Efficiency