call a cgi from jsp
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   mohamed_salim
Posted On:   Sunday, June 23, 2002 04:11 AM

Hi
In my jsp page i have icluded a shtml page.This shtml page is calling an cgi programme outside my jsp folder.Things are well but the problem is where i have called the cgi programme that is not working.Is any other way to call or include a cgi programme in jsp?Please... if any one help it out that wil be great.Here i am using tomcat sever.
Regards
Salim

Re: call a cgi from jsp

Posted By:   Erik_Runia  
Posted On:   Monday, June 24, 2002 11:22 AM

Here is how I call a cgi from a jsp or servlet using the URLConnection object



--------- CODE BELOW -----------




<%@ page import = "java.io.*" %>
<%@ page import = "java.net.*" %>

<%
String theOutput = null;
try {
URL dataURL = new URL(request.getParameter("theActionURL"));
URLConnection connection = dataURL.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(512);
PrintWriter outBS;
outBS = new PrintWriter(byteStream, true);

//create string of data to send to cgi
String postData = null;
postData = "name=blah&address=blah&soOn=blah";
outBS.print(postData);
outBS.flush();
String lengthString = String.valueOf(byteStream.size());
connection.setRequestProperty("Content=Length", lengthString);
connection.setRequestProperty("Content-Type", "text/html");
//write data to stream to URL
byteStream.writeTo(connection.getOutputStream());

//read data sent back from cgi server
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//do something with input from cgi

} catch (Exception e) {}


%>



--------- CODE ABOVE -----------



Hope this helps,


Erik

About | Sitemap | Contact