This week's giveaway is in the Threads forum.
We're giving away four copies of Java Concurrency Live Lessons and have Doug Schmidt on-line!
See this thread for details.
Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can servlet wrap a CGI application?  RSS feed

 
Harry Chan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am wondering if I can wrap an CGI application into a servlet and then interact with other J2EE components?
Thanks for any comment.
Harry
 
David Freels
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Runtime.exec to execute your app.
David
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easily. If the CGI script is http://myserver/cgi-bin/script.pl then from within your servlet you can open the URL and read its output stream:
URL cgiURL = new URL("http://myserver/cgi-bin/script.pl");
InputStream in = cgiURL.openStream();
For GET requests, just append the query string to the URL. POST is slightly more complicated. You need to open a URLConnection object from cgiURL, then setDoOutput(true), set content type to "application/x-www-form-urlencoded", and write your query string to the URLConnection's output stream. After that, you just open the input stream as shown above.
 
Harry Chan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the comment!!
Harry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!