home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / PASS.ZIP / password.java < prev   
Encoding:
Java Source  |  1997-09-18  |  4.7 KB  |  159 lines

  1. /*
  2.  
  3.  
  4. password.java 
  5.  
  6.  
  7. Made by John Eriksson 1997  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. john_eriksson_sw@hotmail.com
  14.  
  15.  
  16.  
  17.  
  18.  
  19. This code is free to use and modify!!!
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. EXAMPLE HTML CODE:
  29.  
  30.  
  31. ------------------
  32.  
  33.  
  34.  
  35.  
  36.  
  37. <center>
  38.  
  39.  
  40. <applet code="password.class"  width=500 height=32>
  41.  
  42.  
  43. <param name=root value="http://www.myhomepage.com/secretfiles/">
  44.  
  45.  
  46. <param name=wrong value="http://www.myhomepage.com/secretfiles/wrong_pwd.html">
  47.  
  48.  
  49. <param name=color value="ffffff">
  50.  
  51.  
  52. <param name=textfield value="40">
  53.  
  54.  
  55. <param name=button value="Open">
  56.  
  57.  
  58. <param name=font_size value="12">
  59.  
  60.  
  61. <param name=font_face value="Courier">
  62.  
  63.  
  64. <param name=title value="Enter password:">
  65.  
  66.  
  67. </applet>
  68.  
  69.  
  70. <center>
  71.  
  72.  
  73.  
  74.  
  75.  
  76. */
  77.  
  78.  
  79.  
  80.  
  81.  
  82. import java.applet.*;
  83.  
  84. import java.awt.*;
  85.  
  86. import java.lang.*;
  87.  
  88. import java.net.*;
  89.  
  90. import java.io.*;
  91.  
  92. import java.util.*;
  93.  
  94.  
  95.  
  96. /*====================================================================================
  97.  
  98. THE MAIN CLASS FOR THIS APPLET
  99.  
  100. ====================================================================================*/
  101.  
  102. public class password extends java.applet.Applet 
  103.  
  104. {
  105.  
  106. private Button b_open;
  107.  
  108. private TextField t_password;
  109.  
  110. private Label l_title;
  111.  
  112.  
  113.  
  114.  
  115.  
  116. private String root;
  117.  
  118.  
  119. private int col;
  120.  
  121.  
  122. private String title;
  123.  
  124.  
  125. private String buttontxt;
  126.  
  127.  
  128. private int bgcolor;
  129.  
  130.  
  131. private int fsize;
  132.  
  133.  
  134. private String fface;
  135.  
  136.  
  137. private String errorURL;
  138.  
  139.  
  140.  
  141.  
  142. /************************************************************************/
  143.  
  144. /* Init the display */
  145.  
  146. /************************************************************************/
  147.  
  148. public void init() 
  149.  
  150. {
  151.  
  152. int loop;
  153.  
  154.  
  155.  
  156.  
  157.    String att = getParameter("root");
  158.  
  159.  
  160.    root = (att == null) ? this.getDocumentBase().toString() : att;
  161.  
  162.  
  163.    att = getParameter("textfield");
  164.  
  165.  
  166.    col = (att == null) ? 20 : (Integer.valueOf(att).intValue());
  167.  
  168.  
  169.    att = getParameter("font_size");
  170.  
  171.  
  172.    fsize = (att == null) ? 11 : (Integer.valueOf(att).intValue());
  173.  
  174.  
  175.    att = getParameter("font_face");
  176.  
  177.  
  178.    fface = (att == null) ? "Arial" : att;
  179.  
  180.  
  181.    att = getParameter("color");
  182.  
  183.  
  184.    bgcolor = (att == null) ? Color.white.getRGB() : (Integer.parseInt(att, 16));
  185.  
  186.  
  187.    att = getParameter("title");
  188.  
  189.  
  190.    title = (att == null) ? "" : att;
  191.  
  192.  
  193.    att = getParameter("button");
  194.  
  195.  
  196.    buttontxt = (att == null) ? "OPEN" : att;
  197.  
  198.  
  199.    att = getParameter("wrong");
  200.  
  201.  
  202.    errorURL = (att == null) ? "" : att;
  203.  
  204.  
  205.  
  206.  
  207.    setFont(new Font(fface, Font.PLAIN, fsize));
  208.  
  209.  
  210.    setBackground(new Color(bgcolor));
  211.  
  212.  
  213.  
  214.  
  215.    b_open = new Button(buttontxt);
  216.  
  217.  
  218.    t_password = new TextField(col);    
  219.  
  220.  
  221.    l_title = new Label(title);
  222.  
  223.  
  224.  
  225.  
  226.  
  227.    t_password.setBackground(Color.white);
  228.  
  229.  
  230.    t_password.setEchoCharacter('*');
  231.  
  232.  
  233.  
  234.  
  235.  
  236.    setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
  237.  
  238.  
  239.  
  240.  
  241.  
  242.    if(title.length()>0)
  243.  
  244.  
  245.       add(l_title);
  246.  
  247.  
  248.  
  249.  
  250.  
  251.    add(t_password);      
  252.  
  253.    add(b_open);      
  254.  
  255.  
  256.  
  257.  
  258.  
  259.    show();
  260.  
  261. }
  262.  
  263. /************************************************************************/
  264.  
  265.  
  266. /* Surf to error URL */
  267.  
  268.  
  269. /************************************************************************/
  270.  
  271.  
  272. void surfto_error()
  273.  
  274.  
  275. {
  276.  
  277.  
  278.    if(errorURL.length()>0) {
  279.  
  280.  
  281.       try {
  282.  
  283.  
  284.          getAppletContext().showDocument(new URL(errorURL),"_self");
  285.  
  286.  
  287.       }
  288.  
  289.  
  290.       catch (MalformedURLException e) {}   
  291.  
  292.  
  293.    }
  294.  
  295.  
  296.    else
  297.  
  298.  
  299.       showStatus("Invalid password!");
  300.  
  301.  
  302. }
  303.  
  304.  
  305.  
  306.  
  307.  
  308. /************************************************************************/
  309.  
  310.  
  311. /* Surf to an URL */
  312.  
  313.  
  314. /************************************************************************/
  315.  
  316.  
  317. void surfto()
  318.  
  319.  
  320. {
  321.  
  322.  
  323.    if(t_password.getText().length()>0) {
  324.  
  325.  
  326.       try{
  327.  
  328.  
  329.          URL surftoURL = new URL(root+t_password.getText()+".html");
  330.  
  331.  
  332.          InputStream in = surftoURL.openStream();
  333.  
  334.  
  335.          int input = in.read();
  336.  
  337.  
  338.          in.close();
  339.  
  340.  
  341.          getAppletContext().showDocument(surftoURL,"_self");
  342.  
  343.  
  344.       }
  345.  
  346.  
  347.       catch (MalformedURLException e) { surfto_error(); }   
  348.  
  349.  
  350.       catch (SecurityException e) { surfto_error(); }
  351.  
  352.  
  353.       catch (IOException e) { surfto_error(); }
  354.  
  355.  
  356.  
  357.  
  358.  
  359.       t_password.setText("");
  360.  
  361.  
  362.    }
  363.  
  364.  
  365. }
  366.  
  367.  
  368.  
  369. /************************************************************************/
  370.  
  371. /* Handle all events */
  372.  
  373. /************************************************************************/
  374.  
  375. public boolean handleEvent(Event evt)
  376.  
  377. {
  378.  
  379.  
  380.  
  381.  
  382.    if(evt.id == Event.KEY_PRESS && evt.target == t_password && evt.key==10){
  383.  
  384.  
  385.       surfto();
  386.  
  387.  
  388.       return(true);
  389.  
  390.  
  391.    }  
  392.  
  393.  
  394.    return super.handleEvent(evt);
  395.  
  396. }
  397.  
  398.  
  399.  
  400. /************************************************************************/
  401.  
  402. /* Handle all actions */
  403.  
  404. /************************************************************************/
  405.  
  406. public boolean action(Event evt, Object arg) 
  407.  
  408. {
  409.  
  410.    if (evt.target == b_open) {
  411.  
  412.       surfto();
  413.  
  414.  
  415.       return true;
  416.  
  417.    }
  418.  
  419.  
  420.    return(super.action(evt,arg));
  421.  
  422. }
  423.  
  424. }
  425.  
  426.  
  427.  
  428.