All pastes #986055 Raw Edit

Mine

public text v1 · immutable
·published 2008-04-15 14:24 UTC
rendered paste body
package applets;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;


public class AppletForm extends Applet implements ActionListener 
{
	TextField inputField;
	Button submitButton;
	TextArea textArea;
	
	public void init()
	{
		setLayout(null);
		
		submitButton = new Button("Send");
		this.add(submitButton);
		submitButton.addActionListener(this);
		submitButton.setBounds(100, 100, 100, 20);
		
		textArea = new TextArea();
		textArea.setEditable(false);
		this.add(textArea);
		textArea.setBounds(300, 50, 200, 200);
		
		inputField = new TextField();
		inputField.setEditable(true);
		this.add(inputField);
		inputField.setBounds(100, 50, 100, 20);

		
	}
	
	public void paint(Graphics g)
	{
		g.drawString("Form", 20, 20);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource() == submitButton)
		{
			showStatus("Form Submitted");
			submitForm();//here
		}
		else
		{
			showStatus("Some other event occurred");
		}
	}
	
	private void submitForm()
	{
		try
		{
			URL url = new URL(getCodeBase(), "/handleAppletForm.do");
		
			Properties props = new Properties();
			props.put("inputField",inputField.getText());

			String argString = "";
	
			argString = toEncodedString(props);
		
			URLConnection con = url.openConnection();
		
			con.setDoInput(true);
			con.setDoOutput(true);
			con.setUseCaches(false);
			con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		
			DataOutputStream out = new DataOutputStream(con.getOutputStream());
		
			out.writeBytes(argString);
			out.flush();
			out.close();
		
			InputStream in = con.getInputStream();
			BufferedReader data = new BufferedReader(new InputStreamReader(in));
		
			String incoming = "";	
		
			while (!incoming.equals("end"))
			{
				incoming = data.readLine();
 				textArea.append(incoming + "\n");
				
			}
			showStatus(incoming);
			data.close();
		}
		catch (MalformedURLException mue)
		{
			showStatus(mue.toString());
		}	
		catch (IOException ioe)
		{
			showStatus(ioe.toString());
		}	
	}
	
	private String toEncodedString(Properties arguments)
	{
		StringBuffer buf = new StringBuffer();
		Enumeration names = arguments.propertyNames();
		while (names.hasMoreElements())
		{
			String name = (String)names.nextElement();
			String value = arguments.getProperty(name);
			buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
			if (names.hasMoreElements())
			{
				buf.append("&");
			}
		}
		return buf.toString();
	}
}