java 一个很简单的applet

一个很简单的applet,可是写得很是规范。。
/*
* This is a very simple example of how to use the HTTPClient package in an
* Applet. It just POSTs a request to a cgi-script on the server when you
* hit the 'Doit' button, and then displays the returned headers and data in
* a text window.
*/

package HTTPClient.doc;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.TextArea;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import HTTPClient.HTTPResponse;
import HTTPClient.HTTPConnection;

public class HTTPClientExample extends Applet implements Runnable, ActionListener
{
    private HTTPConnection con;
    private HTTPResponse   rsp = null;
    private String         script = "/cgi-bin/my_script.cgi";

    private String         disp = "";
    private Thread         thread = null;
    private boolean        done = false;
    private TextArea       text;


    public void init()
    {
/* setup a text area and a button */

        setLayout(new BorderLayout());

add("Center", text = new TextArea(60, 60));
text.setEditable(false);

Button doit = new Button("Doit");
doit.addActionListener(this);
add("South", doit);

/* get an HTTPConnection */

        try
        {
     con = new HTTPConnection(getCodeBase());
        }
        catch (Exception e)
        {
            disp = "Error creating HTTPConnection:\n" + e;
            repaint();
            return;
        }
    }

    public void start()
    {
/* run the http request in a separate thread */

if (thread == null)
{
     done   = false;
     thread = new Thread(this);
     thread.start();
}
    }

    public void run()
    {
try
{
     while (true)
     {
   /* wait for the button to be pressed */

   waitForDoit();
   if (done) break;

   /* POST something to the script */

   disp = "POSTing ...";
   repaint();
   rsp = con.Post(script, "Hello World again");
   repaint();
     }
}
catch (Exception e)
{
     disp = "Error POSTing: " + e;
     e.printStackTrace();
     repaint();
}
    }

   
    private synchronized void waitForDoit()
    {
try { wait(); } catch (InterruptedException ie) { }
    }

    private synchronized void notifyDoit()
    {
notify();
    }


    public void stop()
    {
if (thread != null)
{
     done   = true;
     notifyDoit();
     thread = null;
}
    }


    public void actionPerformed(ActionEvent evt)
    {
notifyDoit(); // tell request thread to do the request
    }


    public void paint(Graphics g)
    {
text.setText(disp + "\n");

if (rsp == null) return;

try
{
     text.append("\n---Headers:\n" + rsp.toString());
     text.append("\n---Data:\n" + rsp.getText() + "\n");
}
catch (Exception e)
{
     text.append("\n---Got Exception:\n" + e + "\n");
}
    }
}

转载于:https://www.cnblogs.com/xinzhuangzi/archive/2010/08/27/4100532.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值