注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

jiwenxu的博客

CODE

 
 
 

日志

 
 

Servle和applet的通信[转]  

2006-11-12 09:18:09|  分类: J2EE |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
sendApplet.java

import java.applet.Applet;
import java.awt;
import java.net.*;
import java.io.*;

public class sendApplet extends Applet
{
Lable sendlable;
Lable messageinfor;
Button sendButton;
TextField messageText;
TextField sendText;
URLConnection connect;
String message;

public String getAppletInfo()
{
return "sendApplet - Applet to communicate with
receiveServlet , Written by ding;";
}

public synchronized void init()
{
super.init();
resize(500,300);
sendlable = new Lable("Please Enter Massage");
messageInfor = new Lable("What you have send:");
sendButton = new Button("Send");
sendText = new TextField(40);
messageText = new TextField(40);
messageText.setEditable(false);


//下面实现一个Panel
Panel mainp = new Panel();
GridBagLayout gbl = new GridBaglayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0;
gbc.weighty = 0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight= 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
mainp.setLayout(gbl);
gbl.setConstrains(sendable,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);

//把实现的Panel加入Applet
setLayout(new BorderLayout());
Add("Center",mainp);

chatURL = getCodeBase();
}

public synchronized void start()
{
}

public synchronized void stop()
{
}

public synchronized void destroy()
{
}

//方法SEND 把用户在applet 中输入的字符串送给Servlet
//然后把Servlet 的处理结果显示出来

Private void Send()
{
message = sendText.getText();
//清除用户的输入信息
sendText.setText("");
showStatus("Message send!");
//把输入的字符串转化为 x-www-form-urlencoded 格式
String queryString =
"/servlet/ReceiveServlet?message="+URLEncoder.encode(message);
p("Attempting to send:"+message);

//建立与Servlet的联接,并取得Servelt的输出信息
try
{
connect = (new URL(chatURL,queryString)).openConnection();
showStatus("open connection!");
//下次连接不用Cache
connect.setDefaultUseCaches(false);
//这次连接也不用Cache
connect.setUseCaches(false);
//打开淂流用于读数据
connect.setDoInput(true);
//不能用于写数据
connect.setDoOutput(false);
//服务器与客户的真正连接
connect.connect();
p("Made connection to "+connect);
showStatus("Open Stream!");
DataInputStream in = newDataInputStream(connect.getInputStream());
showStatus("reading!");
message = in.readLine();

while (message! = null)
{
messageText.setText(message);
message = in.readLine();
}

}
catch(MalformedURLException e2)
{
System.err.println("MalformedURLException!");
e2.printStackTrace(System.err);
showStatus("MalformedURLException!");
}
catch(IOException e1)
{
System.err.println("IOException!");
e2.printStackTrace(System.err);
showStatus("IOException");
}
}
}

//applet 对用户的动作作处理。
public boolean action(Event evt,Object arg)
{
if (evt.target == sendButton)
{
Send();
return true;
}
return super.action(evt,arg);
}

private void p(String debug)
{
System.err.println("app-serv-let:"+debug);
}
}


receiveservlet.java

import java.io.*;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.http.*;

public class receiveservlet extends HttpServlet
{
public void service (HttpServletRequest req,HttpServletResponseres)
throws ServletException,IOException
{
res.setContentType("text/plain");
ServletOutputStream out = res.getOutputStream();
out.print("receive user message:");
out.print("req.getParameter("message"));
}

public String getServletInfo()
{
return "return a String representation of the current time";
}
}


add-serv-let.html

<html>
<head>
<title>sendApplet</title>
</head>
<body>
<hr>
<applet code=sendApplet width=400 height=300></applet>
<hr>
</body>
</html>
  评论这张
 
阅读(35)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018