# Cgi.rb HTML construction tools - from within Webrick servlet

**URL:** <https://www.ruby-forum.com/t/cgi-rb-html-construction-tools-from-within-webrick-servlet/101727>\
**Category:** Ruby\
**Created:** [August 7, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/cgi-rb-html-construction-tools-from-within-webrick-servlet/101727 "2006-08-07T17:57:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 7, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/cgi-rb-html-construction-tools-from-within-webrick-servlet/101727/1 "2006-08-07T17:57:48Z")

</div>

Folks, I’m just playing with webrick and writing a servlet  
for the first time here.

I’ve previously used Perl’s CGI.pm to write a lot of stuff,  
and used extensively its tools for generating HTML, especially  
forms - and having the form construction elements retain state  
data.

I’ve started writing up a handful of similar form generation  
elements for my own use form within my servlet. If I were  
writing a cgi script, I’d have, of course, used cgi.rb (which  
looks like it behaves a lot like CGI.pm).

But I’m not writing cgi. I’m writing a servlet and I can’t  
believe that I’m the first person who needed to generate  
form elements, sometimes reusing the data from the req.query, etc.

Am I missing something? Re-creating a wheel unnecessarily?

Can I just use cgi.rb (can I feed it the req.query)?

BTW, thanks, everyone. I haven’t had so much fun writing code  
in years as I’ve been having in the couple of weeks since  
I started playing with Ruby.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 7, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/cgi-rb-html-construction-tools-from-within-webrick-servlet/101727/2 "2006-08-07T21:28:13Z")

</div>

Hi,

On 8/7/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> elements for my own use form within my servlet. If I were  
> writing a cgi script, I’d have, of course, used cgi.rb (which  
> looks like it behaves a lot like CGI.pm).
> 
> But I’m not writing cgi. I’m writing a servlet and I can’t  
> believe that I’m the first person who needed to generate  
> form elements, sometimes reusing the data from the req.query, etc.
> 
> Am I missing something? Re-creating a wheel unnecessarily?

well, there are many web frameworks from large to small ones (rails,  
nitro, camping)  
then there is markaby, builder et al. to generate markup  
and even erb for templates.

> Can I just use cgi.rb (can I feed it the req.query)?

It seems you can mixin CGI::HtmlExtension to your servlet - that way  
you’ll have all the methods there.

require ‘cgi.rb’

class YourServlet \< WEBrick::Whatever  
include CGI::HtmlExtension

```
def a_method()
      checkbox("name", "value", true)
end

```

end

I haven’t tried it - therefore i’m not 100% sure.

Jano

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 16, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/cgi-rb-html-construction-tools-from-within-webrick-servlet/101727/3 "2006-08-16T23:58:07Z")

</div>

“Jan S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> On 8/7/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Folks, I’m just playing with webrick and writing a servlet  
> > for the first time here.
> > 
> > I’ve previously used Perl’s CGI.pm to write a lot of stuff,  
> > and used extensively its tools for generating HTML, especially

> well, there are many web frameworks from large to small ones (rails,  
> nitro, camping)  
> then there is markaby, builder et al. to generate markup  
> and even erb for templates.
> 
> > Can I just use cgi.rb (can I feed it the req.query)?
> 
> It seems you can mixin CGI::HtmlExtension to your servlet - that way  
> you’ll have all the methods there.

I haven’t had a chance to try out your suggestion yet, but I  
wanted to thank you for making it. I’ll give it a shot.

In the meantime, I just wrote a handy-dandy HtmlTools object  
which generates some of the common stuff for me - pop-ups,  
checkboxes, markup for headers, etc. It’s very primitive,  
and I should replace it.

Thanks
