爱心技术专栏专题

一个applet-servlet通讯的例子

摘录:爱心 来源:爱心 加入时间:2006年09月13日
摘要:
一个applet-servlet通讯的例子
Applet文件
===========================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class sen…

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info/a_9806c7159835a7a8.htm

一个applet-servlet通讯的例子

站点:爱心种子小博士 关键字:一个applet-servlet通讯的例

   
一个applet-servlet通讯的例子
Applet文件
===========================
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);

}

}


Servlet文件
===========================
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public void Riceive extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContenType("text/plain");
ServletOutputStream out=res.getOutputStream();
out.print("recive user message:");
out.print(req.getParameter("message"));


}//end service

}//end receive

客户服务中心信箱:[email protected] [email protected] 网站地图

声明

合作伙伴: