Can I call a CGI program from JSP which generates name/value pair as its output? If so, how?

Serge Knystautas

A traditional CGI program is just a shell program that takes an HTTP request via stdin and returns an HTTP response via stdout. Your JSP could construct a "mock" HTTP request and send it to the CGI program using the Runtime.exec() method. A sample mock HTTP request would look like this...

GET / HTTP/1.0
User-Agent: My fake browser 1.0
Accept-Encoding: gzip
Keep-Alive: true

Remember to include a blank line after the headers. The CGI program would then create a response which you could read via the Process that was created when you called Runtime.exec().

An alternate approach is to just use an URL connection to the CGI program running on a web server. There you'd construct the appropriate request parameters and retrieve the results using the URL.openStream()'s input stream.