applet与servlet通信!

fengdabin 2003-08-23 11:51:08
我想完成applet到servlet的通讯,我是利用java.net.*,所提供的服务。
看了很多相关材料,而且感觉代码很容易理解,不知道为什么,我自己尝试,就是不行,请帮帮忙,先谢了。

applet程序:
public class TestApplet extends Applet {
URL url;
URLConnection urlcon;
int hello;
public void init(){
try{
hello=111;
String link="http://localhost:8080/examples/servlet/TestServlet";
url=new URL(link);
urlcon = url.openConnection();
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
urlcon.setUseCaches (false);
urlcon.setDefaultUseCaches (false);

DataOutputStream dos=new DataOutputStream(urlcon.getOutputStream());
dos.writeByte(hello);

dos.flush();
dos.close();

}catch(ProtocolException pe){
pe.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}

}

Servlet程序:

import java.io.*;

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

public class TestServlet extends HttpServlet
{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doPost(request,response);
}

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
System.out.println("hello");
DataInputStream in = new DataInputStream(request.getInputStream());

try {
int hello;
hello=in.readByte();
in.close();
System.out.println(" "+hello);
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
...全文
142 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧