Java Code
writeFile.pl
#!/usr/bin/perl
use CGI ':standard';
my $outfile = "mailing.out";
print header; # required first set of lines
# Write to file
open(OUT, ">>$outfile") or print("Couldn't open $outfile: $!");
if (param('message'))
{
print OUT param('message'),"\n";
}
close(OUT);
print "Thanks!";
SaveDataToFileOnServer.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SaveDataToFileOnServer extends JApplet
implements ActionListener
{
JPanel toppane;
JScrollPane scrollPane;
JButton submit;
JTextField tf_name;
JCheckBox cb_funny;
JCheckBox cb_happy;
JRadioButton rb_red, rb_blue, rb_green;
ButtonGroup colorGroup;
JLabel l_name, l_funny, l_happy, l_colors;
JPanel formPanel;
public void init( )
{
setLayout( new BorderLayout( ) );
setupButton( );
setupForm( );
}
public void setupForm( )
{
l_name = new JLabel( "Name:", JLabel.RIGHT );
l_colors = new JLabel( "Favorite Color:", JLabel.RIGHT );
tf_name = new JTextField( 10 );
cb_funny = new JCheckBox( "Funny?" );
cb_happy = new JCheckBox( "Happy?" );
rb_red = new JRadioButton( "red" );
rb_blue = new JRadioButton( "blue" );
rb_green = new JRadioButton( "green" );
colorGroup = new ButtonGroup( );
colorGroup.add( rb_red );
colorGroup.add( rb_blue );
colorGroup.add( rb_green );
formPanel = new JPanel( new GridLayout(3,2) );
formPanel.add( l_name );
formPanel.add( tf_name );
JPanel p_cb = new JPanel( new FlowLayout( ) );
p_cb.add( cb_funny );
p_cb.add( cb_happy );
formPanel.add( p_cb );
formPanel.add( cb_happy );
formPanel.add( l_colors );
JPanel p_colors = new JPanel( new FlowLayout( ) );
p_colors.add( rb_red );
p_colors.add( rb_blue );
p_colors.add( rb_green );
formPanel.add( p_colors );
add( formPanel, BorderLayout.NORTH );
}
public void setupButton( )
{
submit = new JButton( "Submit" );
submit.addActionListener( this );
toppane =new JPanel( new FlowLayout( ) );
toppane.add( submit );
add( toppane, BorderLayout.SOUTH );
}
public void actionPerformed( ActionEvent ae )
{
Object src = ae.getSource( );
if( src == submit )
{
invalidate( );
String email = "Form Submission\n";
email += "\n name = " + tf_name.getText( );
email += "\n funny? = " + cb_funny.isSelected( );
email += "\n happy? = " + cb_happy.isSelected( );
email += "\n color = " + rb_red.isSelected( );
email += "\n color = " + rb_red.isSelected( );
email += "\n color = " + rb_red.isSelected( );
sendMail( "http://www.cs.colostate.edu/~boese/cgi-bin/writeFile.cgi?message=", email );
formPanel.removeAll( );
JLabel thanks = new JLabel( "Thanks for your input!" );
formPanel.add( thanks );
validate( );
}
}
public String sendMail( String fullPath, String msg )
{
try {
String enc = URLEncoder.encode(msg, "UTF-8");
URL target = new URL( fullPath + enc );
String content;
URLConnection con = target.openConnection( ); // open connection
con.setUseCaches( false );
con.setDefaultUseCaches( false );
byte b[ ] = new byte[ 1024 ]; // byte array
int nbytes; // num bytes read in
String retVal = new String();
BufferedInputStream in = new BufferedInputStream( con.getInputStream(), 2048 );
while( (nbytes = in.read( b, 0, 1024 )) != -1 ) // while there's more data to read
{
content = new String( b, 0, nbytes ); // get 1024 bytes of data fr file
retVal += content;
}
in.close( ); // close connection
return retVal;
}
catch( Exception e ) { return "Error " + e.toString( ); }
}
}
©2006 by E.S.Boese. All Rights Reserved