tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kotsiras, Alexandros" <AKotsi...@mediaondemand.com>
Subject RE: Execute a CGI from a bean
Date Tue, 15 Aug 2000 21:58:59 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2448.0">
<TITLE>RE: Execute a CGI from a bean</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>I pasted the sample code for invoking a CGI script from servlet
through a URLConnection class</FONT>
<BR><FONT SIZE=2>The interesting part is that the servlet uses POST method to
invoke the CGI script so you wont see and query string </FONT>
<BR><FONT SIZE=2>on the browser.</FONT>
<BR><FONT SIZE=2>Just in case it helps.</FONT>
</P>

<P><FONT SIZE=2>Alex</FONT>
</P>

<P><FONT SIZE=2>public class CGIServlet extends HttpServlet {</FONT>
</P>

<P><FONT SIZE=2>&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp; public void init(ServletConfig config) throws ServletException
{</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp; super.init(config);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; // In case you wont to invoke
a CGI through secure https</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; //System.setProperty(&quot;java.protocol.handler.pkgs&quot;,
&quot;com.sun.net.ssl.internal.www.protocol&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; //java.security.Security.addProvider(new
com.sun.net.ssl.internal.ssl.Provider());</FONT>
</P>

<P><FONT SIZE=2>&nbsp; }</FONT>
</P>

<P><FONT SIZE=2>&nbsp; </FONT>
<BR><FONT SIZE=2>&nbsp; public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; response.setContentType(&quot;text/html&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; PrintWriter out = new PrintWriter
(response.getOutputStream());</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; out.println(&quot;&lt;html&gt;&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; out.println(&quot;&lt;head&gt;&lt;title&gt;CGIServlet&lt;/title&gt;&lt;/head&gt;&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; out.println(&quot;&lt;body&gt;&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; System.out.println(&quot;
- cgi -&nbsp; &quot;);</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; try{</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
URL url = new URL(&quot;<A HREF="http://209.1.23.51/javatips/javatip34/env.cgi" TARGET="_blank">http://209.1.23.51/javatips/javatip34/env.cgi</A>&quot;);&nbsp;&nbsp;&nbsp;
// That URL actually works !!</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//URL url = new URL(&quot;<A HREF="http://www.javaworld.com/javatips/javatip34/env.cgi"
TARGET="_blank">http://www.javaworld.com/javatips/javatip34/env.cgi</A>&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
URLConnection conn = url.openConnection();</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
conn.setDoOutput(true);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
conn.setDoInput(true);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
conn.setUseCaches(false);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
conn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
DataOutputStream daos = new DataOutputStream(conn.getOutputStream());</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Here you can directly write the query String to the</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
daos.writeChars(&quot;name=value&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
daos.flush();</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
daos.close();</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
DataInputStream dis = new DataInputStream(conn.getInputStream());</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
String line = &quot;&quot;;</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Read the response of the cgi script</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
while( (line = dis.readLine()) != null) {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
out.println(line);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//System.out.println(line + &quot;\n&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
dis.close();</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; catch(MalformedURLException ex)
{</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
out.println(&quot;&lt;PRE&gt;&quot; +&nbsp; ServletUtils.getStackTraceAsString(ex)
+ &quot;&lt;/PRE&gt;&quot; );</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; catch(Exception ex) {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.println(&quot;&lt;PRE&gt;&quot;
+&nbsp; ServletUtils.getStackTraceAsString(ex) + &quot;&lt;PRE&gt;&quot;
);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp; out.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp; out.close();</FONT>
<BR><FONT SIZE=2>&nbsp; }</FONT>
</P>
<BR>

<P><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: Oscar Sanchez Vilar [<A HREF="mailto:sanchezo@tcd.ie">mailto:sanchezo@tcd.ie</A>]</FONT>
<BR><FONT SIZE=2>Sent: Tuesday, August 15, 2000 2:52 PM</FONT>
<BR><FONT SIZE=2>To: tomcat-user@jakarta.apache.org</FONT>
<BR><FONT SIZE=2>Subject: Re: Execute a CGI from a bean</FONT>
</P>
<BR>

<P><FONT SIZE=2>Surprisingly, this works only with Netscape 4.0, not IE5. I have
both</FONT>
<BR><FONT SIZE=2>browsers opened, and while the redirection works in NS, ie says
that the page</FONT>
<BR><FONT SIZE=2>is not available (not that the file doesn't exist). Any ideas?</FONT>
</P>

<P><FONT SIZE=2>Thanks again,</FONT>
</P>

<P><FONT SIZE=2>Oscar</FONT>
</P>

<P><FONT SIZE=2>Luis Andrei Cobo wrote:</FONT>
</P>

<P><FONT SIZE=2>&gt; yes:</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; response.sendRedirect(&quot;/cgi-bin/htmloutput.cgi?args&quot;);</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; that should work.</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Luis</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt; From: Tim Diggins [<A HREF="mailto:subscribed@red56.co.uk">mailto:subscribed@red56.co.uk</A>]</FONT>
<BR><FONT SIZE=2>&gt; Sent: Monday, August 14, 2000 10:57 AM</FONT>
<BR><FONT SIZE=2>&gt; To: tomcat-user@jakarta.apache.org; oscarsanchez@mail.com</FONT>
<BR><FONT SIZE=2>&gt; Subject: RE: Execute a CGI from a bean</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Is there a reason that you can't you just redirect the
user to that cgi's</FONT>
<BR><FONT SIZE=2>&gt; URL (don't have my docs in front of me at the moment,
so I can't name the</FONT>
<BR><FONT SIZE=2>&gt; JSP syntax, but I remember it because it's analagous
to the ASP</FONT>
<BR><FONT SIZE=2>&gt; Response.Redirect() function.)</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; Tim</FONT>
<BR><FONT SIZE=2>&gt; (ASP converter)</FONT>
<BR><FONT SIZE=2>&gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>&gt; &gt; From: Oscar Sanchez Vilar [<A HREF="mailto:sanchezo@tcd.ie">mailto:sanchezo@tcd.ie</A>]</FONT>
<BR><FONT SIZE=2>&gt; &gt; Sent: 14 August 2000 11:53</FONT>
<BR><FONT SIZE=2>&gt; &gt; To: tomcat-user@jakarta.apache.org</FONT>
<BR><FONT SIZE=2>&gt; &gt; Subject: Execute a CGI from a bean</FONT>
<BR><FONT SIZE=2>&gt; &gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt; Hi there.</FONT>
<BR><FONT SIZE=2>&gt; &gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt; I'd like to implement the following scenario:</FONT>
<BR><FONT SIZE=2>&gt; &gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt; I have a JSP that uses a bean to validate some
user data, and&nbsp; if that</FONT>
<BR><FONT SIZE=2>&gt; &gt; user data meets some conditions, I would like
to give the user back a</FONT>
<BR><FONT SIZE=2>&gt; &gt; page generated by an existing CGI. The only
way I've found to do it is</FONT>
<BR><FONT SIZE=2>&gt; &gt; by having the user forwarded by the bean to
a jsp that&nbsp; redirects the</FONT>
<BR><FONT SIZE=2>&gt; &gt; user to the cgi using the HTML META REFRESH
Tag (0 secs), but this way</FONT>
<BR><FONT SIZE=2>&gt; &gt; the 2ond jsp is shown for a moment, situation
that i'd like to avoid.</FONT>
<BR><FONT SIZE=2>&gt; &gt; Does anyone know a better way?</FONT>
<BR><FONT SIZE=2>&gt; &gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt; Thanks in advance,</FONT>
<BR><FONT SIZE=2>&gt; &gt;</FONT>
<BR><FONT SIZE=2>&gt; &gt; Oscar</FONT>
</P>
<BR>
<BR>

</BODY>
</HTML>

Mime
View raw message