HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Remote Debugging with Symantec Café"
Vol. 1, Issue 3, P.62

	

Listing 1: The Java source file

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

public class ipVerify extends Applet
{
  TextField tf;

  public void init()
  {
    tf = new TextField(20);
    add(tf);
  }
  private void
  postVal()
  {
    String ip = tf.getText();
    String postV = "IP=" + URLEncoder.encode(ip);
    URL base = getCodeBase();
    String port = (base.getPort() == -1) ?
                    "" : ":" + base.getPort();
    String urlString = new String(base.getProtocol() +
                                  "://" +
                                  base.getHost() +
                                  port +
                                  "/cgi-bin/post-ip.cgi");
    try
    {
      URL url = new URL(urlString);
      URLConnection UConn = url.openConnection();
      UConn.setDoOutput(true);
      PrintStream out = 
                 new PrintStream(UConn.getOutputStream());
      out.println(postV);
      out.close();
    }
    catch (MalformedURLException e) { }
    catch (IOException ioe) { }
  }
  private boolean
  ipVal()
  {
    String ip = tf.getText();
    int nTokens = 0;
    StringTokenizer st = new StringTokenizer(ip, ".");
    while (st.hasMoreTokens())
    {
      Integer in;
      nTokens++;
      String token = st.nextToken();
      try
      {
        in  = new Integer(token);
      }
      catch (NumberFormatException nfe)
      {
        return false;
      }
      if (in.intValue() < 0 || in.intValue() > 255)
      {
        return false;
      }
    }
    if (nTokens == 4)
    {
      return true;
    }
    return false;
  }

  public boolean
  handleEvent(Event evt)
  {
    if (evt.target == tf &&
        evt.id == Event.KEY_RELEASE &&
        evt.key == '\n')
    {
      if (ipVal())
        postVal();
      // else handle the error some how
    }
    return false;
  }
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.