// EchoApplication.java by Kari Laitinen // 2012-12-21 Applet from http://www.frank-buss.de/echoservlet/index.html // 2013-02-04 First working application. // 2013-09-26 Last modification. /* Security problems prevented the running of the original applet. This Java application was created based on the applet This program can communicate with a Java Servlet that is located in address "http://localhost:8080/EchoServlet/" */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class EchoApplication extends Frame implements WindowListener { private TextField inputField = new TextField(); private TextField outputField = new TextField(); private TextArea exceptionArea = new TextArea(); /** * Setup the GUI. */ public EchoApplication() { setSize( 600, 400 ) ; setTitle( "EchoApplication" ) ; // set new layout setLayout( new GridBagLayout() ) ; // add title Label title = new Label("Echo Application", Label.CENTER); title.setFont(new Font("SansSerif", Font.BOLD, 14)); GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(5, 5, 5, 5); add(title, c); // add input label, field and send button c = new GridBagConstraints(); c.anchor = GridBagConstraints.EAST; add(new Label("Input:", Label.RIGHT), c); c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; add(inputField, c); Button sendButton = new Button("Send"); c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; add( sendButton, c) ; sendButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { send_button_pressed(); } }); // add output label and non-editable field c = new GridBagConstraints(); c.anchor = GridBagConstraints.EAST; add(new Label("Output:", Label.RIGHT), c); c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; add(outputField, c); outputField.setEditable(false); // add exception label and non-editable textarea c = new GridBagConstraints(); c.anchor = GridBagConstraints.EAST; add(new Label("Exception:", Label.RIGHT), c); c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.weighty = 1; c.fill = GridBagConstraints.BOTH; add(exceptionArea, c); exceptionArea.setEditable(false); addWindowListener( this ) ; } /** * Get a connection to the servlet. */ private URLConnection getServletConnection() throws MalformedURLException, IOException { // Connection zum Servlet öffnen URL servlet_url = new URL( "http://localhost:8080/EchoServlet/"); URLConnection url_connection = servlet_url.openConnection(); // konfigurieren url_connection.setDoInput(true); url_connection.setDoOutput(true); url_connection.setUseCaches(false); url_connection.setRequestProperty( "Content-Type", "application/x-java-serialized-object" ) ; // und zurückliefern return url_connection; } /** * Send the inputField data to the servlet and show the response from servlet in the outputField. */ private void send_button_pressed() { try { // get input data for sending String text_to_send = inputField.getText(); // send data to the servlet URLConnection url_connection = getServletConnection(); OutputStream output_stream = url_connection.getOutputStream(); ObjectOutputStream object_output_stream = new ObjectOutputStream( output_stream ); object_output_stream.writeObject( text_to_send ) ; object_output_stream.flush() ; object_output_stream.close() ; // receive result from servlet InputStream input_stream = url_connection.getInputStream() ; ObjectInputStream object_input_stream = new ObjectInputStream( input_stream ) ; String text_from_servlet = (String) object_input_stream.readObject() ; object_input_stream.close() ; input_stream.close() ; // show result outputField.setText( text_from_servlet ) ; } catch ( Exception exception ) { exception.printStackTrace() ; exceptionArea.setText( exception.toString() ) ; } } public void windowClosing( WindowEvent event ) { setVisible( false ) ; System.exit( 0 ) ; } // Other methods required by the WindowListener interface: public void windowActivated( WindowEvent event ) {} public void windowClosed( WindowEvent event ) {} public void windowDeactivated( WindowEvent event ) {} public void windowDeiconified( WindowEvent event ) {} public void windowIconified( WindowEvent event ) {} public void windowOpened( WindowEvent event ) {} public static void main( String[] command_line_arguments ) { EchoApplication this_application_window = new EchoApplication() ; this_application_window.setVisible( true ) ; } }