Java Code Examples

Included below are some examples of hand-coded JAVA applications created.

** JAVA LOGIN APPLETĀ VIEW APPLET

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

                        // import symantec.itools.awt.BaseTabbedPanel;


public class Submit extends Applet {

                        //    StatusCanvas status = new StatusCanvas();
        setBackground(new Color(16777215));

                        // public Component[] compo;

    void GoForIt(String s)

        {   if (s.equals("password"))
                URL         url;
                String pVal = password.getText();

                        // String url = "tests/"+pVal+".shtml";

            String url = pVal+".html";
                try

                {
                getAppletContext().showDocument(new URL(getDocumentBase(),url));

                        // {   getAppletContext().showDocument(
                        //new URL(getDocumentBase(),url), "_blank");

                }catch(Exception e)
                {
                System.out.println("Password Invalid\n Please Try Again.");

                        // System.out.println("Unable to load URL:");

                }
        }

        void button1_Clicked(Event event)
        {
                GoForIt(password.getText());
                }

        void textField1_EnterHit(Event event)
        {
                GoForIt(password.getText());
                button1.requestFocus();

                        // showStatus("logging in as "+password.getText()+
                        // " ... Please Wait...");
        }

                public void init()
                {

                super.init();


                // INIT_CONTROLS
                setLayout(null);
                setSize(120,24);

                button1 = new java.awt.Button();
                button1.setLabel("Enter");
                button1.setBounds(120,0,65,24);
                add(button1);

                password = new java.awt.TextField();
                password.setEchoChar('*');
                password.setBounds(0,0,120,24);
                add(password);

        }

        public boolean handleEvent(Event event) {
                if (event.target == password && event.id == Event.ACTION_EVENT)
                {
                    getAppletContext().showStatus(
                    "logging in as "+password.getText()+" ... Please Wait...");
                        textField1_EnterHit(event);
                        return true;
                }
                if (event.target == button1 && event.id == Event.ACTION_EVENT)
                {
                    getAppletContext().showStatus(
                    "logging in as "+password.getText()+" ... Please Wait...");
                        button1_Clicked(event);
                        return true;
                }

                if ((event.target == (button1) || (password)) &&
                   (event.id == (Event.MOUSE_ENTER)))
                {
                        getAppletContext().showStatus(
                        "Enter Password and Select Enter.");
                        return true;



              /* if (event.target == button1 && event.id ==
                Event.MOUSE_ENTER || event.target == password && event.id ==
                Event.MOUSE_ENTER)
                { getAppletContext().showStatus(
                "Enter Password and Select Enter.");
                        return true;  */






              /*  }else

                if(event.id==Event.KEY_PRESS && event.key == '\t')
                {
                  for(int i=0;i<compo.length;i++)
                  {
                  if(event.target==compo[i])
                  {
                do
                { i=(i+(event.shiftDown() ? compo.length-1 : 1))%compo.length;
                }
                while (!compo[i].isEnabled());
                compo[i].requestFocus();
                return true;
                  }
                }
            return false;*/
                }

                return super.handleEvent(event);
        }

        //to move from one TextField to another using tabkey
/*        public boolean handleEvent(Event e) {
               if(e.id==Event.KEY_PRESS && e.key == '\t') {
                  for(int i=0;i<compo.length;i++) {
                  if(e.target==compo[i]) {
                do {
                    i=(i+(e.shiftDown() ? compo.length-1 : 1))%compo.length;
              } while (!compo[i].isEnabled());
              compo[i].requestFocus();
             // if(compo[i] instanceof TextComponent) ((
             // TextComponent)compo[i]).selectAll();
              return true;
                  }
                }
            return false;
            }
        }  */
        /* public boolean mouseEnter(Event evt, int x, int y)
        {
// TODO: Place applet mouseDown code here
return true;
}
                */


                // DECLARE_CONTROLS
                java.awt.Button button1;
                java.awt.TextField password;
}



/* StatusCanvas defines the status bar in the frame */

/* class StatusCanvas extends Canvas {
    String strStatusText = "";
    int loginStatus;

    StatusCanvas() {
loginStatus = 0;
    }

    public void paint(Graphics g) {
            int w = size().width;
            int h = size().height;
            g.setColor(new Color(255,255,255));
            g.fillRect(0,5,w,h);
            g.setColor(new Color(0,0,0));
            g.setFont(new Font("Helvetica", Font.PLAIN, 10));
            g.drawString(strStatusText,3,15);
    }
*/
/* method to set the status string */

/*
    public  void setText(String strtxt) {
strStatusText=strtxt;
repaint();
     }
    public Dimension minimumSize() {
return new Dimension(225,20);
    }

    public Dimension preferredSize() {
return minimumSize();
    }
}   */
** JAVA / LDAP (NETSCAPE DIRECTORY SRVR) USER DATA RETRIEVAL
import netscape.ldap.*;
import netscape.ldap.util.*;
import netscape.ldap.controls.*;
import java.io.*;
import java.util.*;public class sort {

public static void main(String args[])
{
String host = “localhost”;
int port = 389;
String base = “o=whs.org”;
int scope = LDAPConnection.SCOPE_SUB;
String dn = “”;
String pwd = “”;
String filter = “sn=Walls”;

LDAPConnection ld = null;
LDAPSearchResults res = null;

try
{
ld = new LDAPConnection();
ld.connect(host,port,dn,pwd);

String attrs [] = {“cn”,”sn”,”givenname”,”uid”};

/* Create sort keys that specify the sort order. */
LDAPSortKey sortByFirstName = new LDAPSortKey(“givenname”);
LDAPSortKey sortByUID = new LDAPSortKey(“-uid”);
LDAPSortKey[] sortOrder = {sortByFirstName,sortByUID};

/* Create a server control using that sort key. */
LDAPSortControl sortCtrl = new LDAPSortControl(sortOrder,true);
LDAPSearchConstraints cons = ld.getSearchConstraints();
cons.setServerControls(sortCtrl);

res = ld.search( base, scope, filter, attrs, false,cons);

LDAPEntry findEntry = null;
while (res.hasMoreElements())
{
findEntry = null;
//try{
findEntry = (LDAPEntry)res.next();
System.out.println(“DN: “+findEntry.getDN());

LDAPAttributeSet attributeSet = findEntry.getAttributeSet();

for (int i=0;i<attributeSet.size();i++)
{
LDAPAttribute attribute=(LDAPAttribute)attributeSet.elementAt(i);
String attrName = attribute.getName();
System.out.println( attrName + “:” );

Enumeration enumVals = attribute.getStringValues();
if (enumVals != null) {
while ( enumVals.hasMoreElements() ) {
String nextValue = ( String )enumVals.nextElement();
System.out.println( “\t” + nextValue );
}
}
}
}
}

catch(LDAPException e)
{
e.printStackTrace();
System.exit(1);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}

try{
if (ld != null)
{
ld.disconnect();
}
System.out.println(“finished!”);
System.exit(0);
}
catch (LDAPException e1)
{
e1.printStackTrace();
System.exit(1);
}
}
}