当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

Java applet-servlet通讯的例子[Java编程]

赞助商链接



  本文“Java applet-servlet通讯的例子[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class sendapplet extends Applet implements ActionListener
{
  Label sendlable;
  Label messageInfo;
  Button sendButton;
  TextField sendText;
  TextField messageText;
  URLConnection connect;
  String message;
  URL chatURL;
  public String getAppletInfo()
  {
    return "this is a applet--servlet";
  }
  public synchronized void init()
  {
    super.init();
    resize(500,300);
    sendlable=new Label("Please enter Massage");
    messageInfo=new Label("What you hava send:");
    sendText=new TextField(40);
    sendButton=new Button("Send");
    messageText=new TextField(40);
    messageText.setEditable(false);
    //
    //
    Panel mainp=new Panel();
    GridBagLayout gbl=new GridBagLayout();
    GridBagConstraints gbc=new GridBagConstraints();
    gbc.weightx=0;
    gbc.weighty=0;
    gbc.gridy=0;
    gbc.gridx=0;
    gbc.gridwidth=10;
    gbc.gridheight=1;
    gbc.anchor=GridBagConstraints.CENTER;
    gbc.fill=GridBagConstraints.NONE;
    mainp.setLayout(gbl);
    gbl.setConstraints(sendlable,gbc);
    mainp.add(sendlable);
    gbc.gridy=1;
    gbc.gridwidth=9;
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbl.setConstraints(sendText,gbc);
    mainp.add(sendText);
    gbc.gridx=9;
    gbc.gridwidth=1;
    gbc.fill=GridBagConstraints.NONE;
    gbl.setConstraints(sendButton,gbc);
    mainp.add(sendButton);
    gbc.gridx=0;
    gbc.gridy=2;
    gbc.gridwidth=10;
    gbl.setConstraints(messageInfo,gbc);
    mainp.add(messageInfo);
    gbc.gridy=3;
    gbc.weighty=100;
    gbc.gridheight=10;
    gbc.fill=GridBagConstraints.BOTH;
    gbl.setConstraints(messageText,gbc);
    mainp.add(messageText);
    sendButton.addActionListener(this);
    //构造的panel加入到applet中
    setLayout(new BorderLayout());
    add("Center",mainp);
    chatURL=getCodeBase();
  }
  public synchronized void start()
  {
  }
  public synchronized void stop()
  {
  }
  public synchronized void destroy()
  {
  }
  //
  private void Send()
  {
    message=sendText.getText();
    sendText.setText("");
    //showStatus("Message send!");
    p("Message send!");
    messageText.setText("message");
    String queryString="/servlet/Receive?message"+URLEncoder.encode(message);
    p("Attemping to send :"+message);
    try{
      connect=(new URL(chatURL,queryString)).openConnection();
      showStatus("Open Connection!");
      connect.setDefaultUseCaches(false);
      connect.setUseCaches(false);
      connect.setDoInput(true);
      connect.setDoOutput(false);
      connect.connect();
      p("Make connection to "+connect);
      showStatus("Open Sream!");
      DataInputStream in=new DataInputStream(connect.getInputStream());
      showStatus("reading");
      message=in.readLine();
      while(message!=null)
      {
        messageText.setText(message);
        message=in.readLine();
      }//end while

    }//end try
    catch(MalformedURLException e2)
    {
       System.err.println("Malformedurlexception:"+e2.toString());
    }//end catch
    catch(IOException e1)
    {
       System.err.println(e1.toString());
    }
  }//end Send
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==sendButton)
    {
      Send();
    }
  }
  private void p(String debug)
  {
     messageText.setText(debug);
    //System.out.println("app-servlet:"+debug);
  }
}


  以上是“Java applet-servlet通讯的例子[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .