• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

一个applet-servlet通讯的例子

发布: 2007-7-14 21:19 | 作者: 佚名    | 来源: 网络转载     | 查看: 24次 | 进入软件测试论坛讨论

领测软件测试网 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

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网