Professional Web Applications Themes

Getting html page fields with webrick - Ruby

Hi All, I am playing with Webrick and I tried to change a little the example given with the packet but when I fill a html form and send it the fields values are empty (using GET method, Webrick doesn't accept POST method). How to fix it? Thank you! jilani #!/usr/local/bin/ruby require 'webrick' include WEBrick s = HTTPServer.new(:Port => 2000, :DoentRoot => "/xitami/webpages") class HelloServlet < HTTPServlet::AbstractServlet def do_GET(req, res) name = req['fname'] email = req['email'] st = "<HTML>Hello #{fname} -- #{email} </HTML>" res['Content-Type'] = "text/html" res.body = st end end s.mount("/hello", HelloServlet) # HTTPServer#mount_proc(path){|req, res| ...} # You can ...

  1. #1

    Default Getting html page fields with webrick

    Hi All,

    I am playing with Webrick and I tried to change a little the example given
    with the packet but when I fill a html form and send it the fields values
    are empty (using GET method, Webrick doesn't accept POST method). How to
    fix it?
    Thank you!

    jilani

    #!/usr/local/bin/ruby
    require 'webrick'
    include WEBrick

    s = HTTPServer.new(:Port => 2000, :DoentRoot => "/xitami/webpages")

    class HelloServlet < HTTPServlet::AbstractServlet
    def do_GET(req, res)
    name = req['fname']
    email = req['email']
    st = "<HTML>Hello #{fname} -- #{email} </HTML>"
    res['Content-Type'] = "text/html"
    res.body = st
    end
    end
    s.mount("/hello", HelloServlet)


    # HTTPServer#mount_proc(path){|req, res| ...}
    # You can mount also a block by `mount_proc'.
    # This block is called when GET or POST.

    s.mount_proc("/hello/again"){|req, res|
    res.body = "<HTML>hello (again)</HTML>"
    res['Content-Type'] = "text/html"
    }

    trap("INT"){ s.shutdown }
    s.start

    it gives only "hello -- " as answer.

    ps.
    When I run the servlet, I get a warning:
    "WARN TCPServer Error: Address family not supported by protocol - socket
    (2)"

    Everything under Linux Slackware 9.0 and Ruby 1.8.0

    --
    [url]http://www.jilani.net[/url]


    JK Guest

  2. #2

    Default Re: Getting html page fields with webrick

    It seems to be a common problem.
    check the thread 'Integrated webserver?"

    I'd recommend to contact the author (???).


    -ronnie.

    il Fri, 22 Aug 2003 07:04:38 +0900, Joel VanderWerf
    <vjoelPATH.Berkeley.EDU> ha scritto::


    >>[2003-08-21 14:44:46] INFO WEBrick 1.3.1
    >>[2003-08-21 14:44:46] INFO ruby 1.8.0 (2003-08-04) [i686-linux]
    >>[2003-08-21 14:44:47] WARN TCPServer Error: Address family not
    >>supported by protocol - socket(2)
    >>
    >>Could it be because I'm trying to run it inside a 192.* network?
    >
    >
    dunno if it could possibly get confused, this it's working fine for
    me:

    require 'webrick'
    include WEBrick

    s = HTTPServer.new(
    :Port => 2000,
    :DoentRoot => "/"
    )

    s.mount("/my", HTTPServlet::FileHandler, 'C:\pubs',true)

    s.start


    it seems that you could try passing
    :BindAddress =>your_chosen_addr

    possibly you have multiple net interface and this confuses the script?

    or maybe you could contact the author to check if you discovered a bug




    JK wrote:
    > Hi All,
    >
    > I am playing with Webrick and I tried to change a little the example
    > given with the packet but when I fill a html form and send it the
    > fields values are empty (using GET method, Webrick doesn't accept POST
    > method). How to fix it?
    > Thank you!
    >
    > jilani
    >
    > #!/usr/local/bin/ruby
    > require 'webrick'
    > include WEBrick
    >
    > s = HTTPServer.new(:Port => 2000, :DoentRoot => "/xitami/webpages")
    >
    > class HelloServlet < HTTPServlet::AbstractServlet
    > def do_GET(req, res)
    > name = req['fname']
    > email = req['email']
    > st = "<HTML>Hello #{fname} -- #{email} </HTML>"
    > res['Content-Type'] = "text/html"
    > res.body = st
    > end
    > end
    > s.mount("/hello", HelloServlet)
    >
    >
    > # HTTPServer#mount_proc(path){|req, res| ...}
    > # You can mount also a block by `mount_proc'.
    > # This block is called when GET or POST.
    >
    > s.mount_proc("/hello/again"){|req, res|
    > res.body = "<HTML>hello (again)</HTML>"
    > res['Content-Type'] = "text/html"
    > }
    >
    > trap("INT"){ s.shutdown }
    > s.start
    >
    > it gives only "hello -- " as answer.
    >
    > ps.
    > When I run the servlet, I get a warning:
    > "WARN TCPServer Error: Address family not supported by protocol -
    > socket (2)"
    >
    > Everything under Linux Slackware 9.0 and Ruby 1.8.0
    >


    Bermejo, Rodrigo Guest

  3. #3

    Default Re: Getting html page fields with webrick

    > I am playing with Webrick and I tried to change a little the example given
    > with the packet but when I fill a html form and send it the fields values
    > are empty (using GET method, Webrick doesn't accept POST method). How to
    Webrick accepts POST method. Just define a method do_POST(req, res).

    [...]
    > name = req['fname']
    use req.query['fname'].

    HTTPRequest#[] returns values from the request header. For example:

    req['Connection'] # => keep-alive

    The values you are searching are in the array returned by HTTPRequest#query.


    Carlos Guest

  4. #4

    Default Re: Getting html page fields with webrick

    Hi,

    In message <oprubif80ymwmsstout.virgilio.it>,
    `JK <j.khaldivirgilio.it>' wrote:
    > I am playing with Webrick and I tried to change a little the example given
    > with the packet but when I fill a html form and send it the fields values
    > are empty (using GET method, Webrick doesn't accept POST method). How to
    > fix it?
    HTTPRequest#query returns a Hash of pd query.

    def do_GET(req, res)
    q = req.query
    fname = q['fname']
    email = q['email']
    st = "<HTML>Hello #{fname} -- #{email} </HTML>"
    res['Content-Type'] = "text/html"
    res.body = st
    end

    In addition, do_POST should be defined for POST request.

    --
    gotoyuzo

    GOTOU Yuuzou Guest

  5. #5

    Default Re: Getting html page fields with webrick

    > HTTPRequest#query returns a Hash of pd query.
    >
    > def do_GET(req, res)
    > q = req.query
    > fname = q['fname']
    > email = q['email']
    > st = "<HTML>Hello #{fname} -- #{email} </HTML>"
    > res['Content-Type'] = "text/html"
    > res.body = st
    > end
    >
    > In addition, do_POST should be defined for POST request.
    That's ok thank you.
    Well, now instead of having the output of results (server side) to
    "$stderr" I want to have it in a "log_file". How to initialize the BasicLog
    class?

    jilani

    --
    [url]http://www.jilani.net[/url]

    JK Guest

Similar Threads

  1. accessing html fields
    By Jessica Weiner in forum ASP.NET Building Controls
    Replies: 1
    Last Post: July 5th, 04:56 AM
  2. Storing HTML in Memo Fields?
    By stuart1231 in forum Coldfusion Database Access
    Replies: 2
    Last Post: July 23rd, 11:43 PM
  3. HTML form fields and special characters
    By Alen in forum Coldfusion - Advanced Techniques
    Replies: 3
    Last Post: May 31st, 09:23 PM
  4. Replies: 0
    Last Post: July 5th, 02:00 AM
  5. mysql database fields into html list/menu
    By Franz Josef Fuka in forum PHP Development
    Replies: 1
    Last Post: June 30th, 02:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139