home *** CD-ROM | disk | FTP | other *** search
/ Total Java Scripts / Total Java Scripts.iso / JavaApplets / passwords / password / password.java < prev    next >
Encoding:
Java Source  |  1998-10-31  |  6.4 KB  |  226 lines

  1. /*
  2. password.java  v2.0
  3. Made by John Eriksson 1997  
  4.  
  5. john_eriksson_sw@hotmail.com
  6.  
  7. http://surf.to/bugbase
  8.  
  9. This code is free to use and modify!!!
  10.  
  11. EXAMPLE HTML CODE:
  12. ------------------
  13.  
  14. <center>
  15. <applet code="password.class"  width=500 height=32>
  16. <param name=root value="http://www.myhomepage.com/">
  17. <param name=wrong value="http://www.myhomepage.com/wrong_pwd.html">
  18. <param name=bgcolor value="ffffff">
  19. <param name=fgcolor value="000000">
  20. <param name=textfield value="40">
  21. <param name=button value="Open">
  22. <param name=font_size value="12">
  23. <param name=font_face value="Courier">
  24. <param name=title value="Enter password:">
  25. </applet>
  26. </center>
  27. */
  28.  
  29. import java.applet.*;
  30. import java.awt.*;
  31. import java.lang.*;
  32. import java.net.*;
  33. import java.io.*;
  34. import java.util.*;
  35.  
  36. /*===================================================================================*/
  37. /* THE MAIN CLASS FOR THIS APPLET
  38. /*===================================================================================*/
  39. public class password extends java.applet.Applet 
  40. {
  41. private Button b_open;
  42. private TextField t_password;
  43. private Label l_title;
  44.  
  45. private String root;
  46. private int col;
  47. private String title;
  48. private String buttontxt;
  49. private int bgcolor;
  50. private int fgcolor;
  51. private int fsize;
  52. private String fface;
  53. private String errorURL;
  54.  
  55. /*************************************************************************/
  56. /* Init the display */
  57. /*************************************************************************/
  58. public void init() 
  59. {
  60. int loop;
  61.  
  62.    //*** Get parameters.
  63.    String att = getParameter("root");
  64.    root = (att == null) ? this.getDocumentBase().toString() : att;
  65.    att = getParameter("textfield");
  66.    col = (att == null) ? 20 : (Integer.valueOf(att).intValue());
  67.    att = getParameter("font_size");
  68.    fsize = (att == null) ? 11 : (Integer.valueOf(att).intValue());
  69.    att = getParameter("font_face");
  70.    fface = (att == null) ? "Arial" : att;
  71.    att = getParameter("bgcolor");
  72.    bgcolor = (att == null) ? Color.white.getRGB() : (Integer.parseInt(att, 16));
  73.    att = getParameter("fgcolor");
  74.    fgcolor = (att == null) ? Color.black.getRGB() : (Integer.parseInt(att, 16));
  75.    att = getParameter("title");
  76.    title = (att == null) ? "" : att;
  77.    att = getParameter("button");
  78.    buttontxt = (att == null) ? "OPEN" : att;
  79.    att = getParameter("wrong");
  80.    errorURL = (att == null) ? "" : att;
  81.  
  82.    //*** Set font.
  83.    setFont(new Font(fface, Font.PLAIN, fsize));
  84.    //*** Set background color.
  85.    setBackground(new Color(bgcolor));
  86.  
  87.    //*** Create objects.
  88.    b_open = new Button(buttontxt);
  89.    t_password = new TextField(col);    
  90.    l_title = new Label(title);
  91.  
  92.    //*** Set background color for text field.
  93.    t_password.setBackground(Color.white);
  94.    //*** Set forebround for title.
  95.    l_title.setForeground(new Color(fgcolor));
  96.  
  97.    //*** Set echo character for textfield.
  98.    t_password.setEchoCharacter('*');
  99.  
  100.    //*** Lets build teh layout.
  101.    setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
  102.  
  103.    //*** Add title if any.
  104.    if(title.length()>0)
  105.       add(l_title);
  106.    //*** Add textfield and button.
  107.    add(t_password);      
  108.    add(b_open);      
  109.  
  110.    //*** Lets show our creation.
  111.    show();
  112. }
  113.  
  114. /*************************************************************************/
  115. /* Check if an url exists */
  116. /*************************************************************************/
  117. public static boolean check_if_URL(String url)
  118. {
  119. StringTokenizer tok = new StringTokenizer(url);
  120. String protocol = tok.nextToken(":");
  121. String host = tok.nextToken(":/");
  122. String uri;
  123. int port = 80; 
  124.  
  125.    //*** Extract protocol, host and uri.
  126.    if (tok.hasMoreTokens())
  127.       uri = "/" + tok.nextToken(" ");
  128.    else
  129.       uri = "/";
  130.  
  131.    //*** Make the request.
  132.    try {
  133.       //*** Open socket to host.
  134.       Socket clientSocket = new Socket(host, port);
  135.  
  136.       //*** Get output and inputstream.
  137.       PrintStream outStream = new PrintStream(clientSocket.getOutputStream());
  138.       DataInputStream inStream = new DataInputStream(clientSocket.getInputStream());
  139.  
  140.       //*** Send request to host.
  141.       outStream.println("HEAD " + uri + " HTTP/1.0\n");
  142.  
  143.       StringTokenizer line = new StringTokenizer(inStream.readLine());
  144.       String code = line.nextToken(" ");
  145.              code = line.nextToken(" ");
  146.       
  147.       if(Integer.parseInt(code)<200 || Integer.parseInt(code)>=300)
  148.          return(false);
  149.  
  150.       //*** So far so good...lets return true.
  151.       return(true);
  152.    } 
  153.    catch(IOException ioe) {
  154.       return(false);
  155.    } 
  156.    catch(Exception e) {
  157.       return(false);
  158.    }
  159. }
  160.  
  161. /************************************************************************/
  162. /* Surf to error URL */
  163. /************************************************************************/
  164. void surfto_error()
  165. {
  166.    if(errorURL.length()>0) {
  167.       try {
  168.          getAppletContext().showDocument(new URL(errorURL),"_self");
  169.       }
  170.       catch (MalformedURLException e) {}   
  171.    }
  172.    else
  173.       showStatus("Invalid password!");
  174. }
  175.  
  176. /************************************************************************/
  177. /* Surf to an URL */
  178. /************************************************************************/
  179. void surfto()
  180. {
  181.    if(t_password.getText().length()>0) {
  182.       try{
  183.          URL surftoURL = new URL(root+t_password.getText()+".html");
  184.          t_password.setText("");
  185.         
  186.          if(!check_if_URL(surftoURL.toString())) {
  187.             surfto_error();
  188.          }
  189.          else {
  190.             getAppletContext().showDocument(surftoURL,"_self");
  191.          }
  192.       }
  193.       catch (MalformedURLException e) { surfto_error(); }   
  194.       catch (SecurityException e) { surfto_error(); }
  195.       catch (IOException e) { surfto_error(); }
  196.    }
  197. }
  198.  
  199. /************************************************************************/
  200. /* Handle all events */
  201. /************************************************************************/
  202. public boolean handleEvent(Event evt)
  203. {
  204.    //*** Handle key press in textfield.
  205.    if(evt.id == Event.KEY_PRESS && evt.target == t_password && evt.key==10){
  206.       surfto();
  207.       return(true);
  208.    }  
  209.    return super.handleEvent(evt);
  210. }
  211.  
  212. /************************************************************************/
  213. /* Handle all actions */
  214. /************************************************************************/
  215. public boolean action(Event evt, Object arg) 
  216. {
  217.    //*** Handle button press.
  218.    if (evt.target == b_open) {
  219.       surfto();
  220.       return true;
  221.    }
  222.    return(super.action(evt,arg));
  223. }
  224. }
  225.  
  226.