Forum: Ruby How to pass data in webrick servlets

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
74b12171278329601699336d0236b099?d=identicon&s=25 Wrapster Aol (wrapster)
on 2009-01-29 05:51
Hi all,

I am a newbie to servlet programming using webrick...
Was wondering how to accept data from a form and do necessary
processing..
To start with i wrote a sample servlet that accepts a name and prints
"welcome <name>"

But I do not know how to take this value from the form into the servlet
and print it out...
this is what i worte... can anyone please help me with this.

s = HTTPServer.new( :Port => 9000)
class TextServlet < HTTPServlet::AbstractServlet
  def do_GET(req, res)
    res.body = %Q{<html>
                    <form action = "text.rb">
                      Name:<input type = "text" name = "text1">
                      <input type = "submit">
                    </form>
                    </html>}
    res['Content-Type'] = "text/html"
  end
end

class Dataread < HTTPServlet::AbstractServlet
  def do_GET(req,res)
    res.body = %Q{<html>
                    <body>
                      Welcome <I dont know how to call it here>
                    </body>
                  </html>}
    res['Content-Type'] = 'text/html'
  end
end

trap("INT") {s.shutdown}
s.mount("/" , TextServlet)
s.mount("/text.rb", Dataread)
s.start

I am a total beginner to this , so please help me out....
Also if there is any resource online available do point me to it, apart
from webrick.org
Be958a41cef6cb01cc7e9d7385cf3d56?d=identicon&s=25 Brad Haywood (bradh)
on 2009-10-26 12:22
Wrapster Aol wrote:
> Hi all,
>
> I am a newbie to servlet programming using webrick...
> Was wondering how to accept data from a form and do necessary
> processing..
> To start with i wrote a sample servlet that accepts a name and prints
> "welcome <name>"
>
> But I do not know how to take this value from the form into the servlet
> and print it out...
> this is what i worte... can anyone please help me with this.
>
> s = HTTPServer.new( :Port => 9000)
> class TextServlet < HTTPServlet::AbstractServlet
>   def do_GET(req, res)
>     res.body = %Q{<html>
>                     <form action = "text.rb">
>                       Name:<input type = "text" name = "text1">
>                       <input type = "submit">
>                     </form>
>                     </html>}
>     res['Content-Type'] = "text/html"
>   end
> end
>
> class Dataread < HTTPServlet::AbstractServlet
>   def do_GET(req,res)
>     res.body = %Q{<html>
>                     <body>
>                       Welcome <I dont know how to call it here>
>                     </body>
>                   </html>}
>     res['Content-Type'] = 'text/html'
>   end
> end
>
> trap("INT") {s.shutdown}
> s.mount("/" , TextServlet)
> s.mount("/text.rb", Dataread)
> s.start
>
> I am a total beginner to this , so please help me out....
> Also if there is any resource online available do point me to it, apart
> from webrick.org

Hi Wrapster,

Forvive the late reply, but I only just joined up ;-)
You could replace the text between your <>'s to #{req.query['text1']}

Cheers,

Brad
This topic is locked and can not be replied to.