首页 > 学院 > 开发设计 > 正文

Applet用Http隧道技术和Servlet通讯 (源码)

2019-11-18 13:56:12
字体:
来源:转载
供稿:网友

  Applet用Http隧道技术和Servlet通讯 (源码) 作者:jdeveloper

Applet source





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

public class TestDataStreamApplet extends Applet
{
String response;
public void init(){

try{
java.net.URL url = new java.net.URL(getDocumentBase(),"servlet/DataStreamEcho");
java.net.URLConnection con = url.openConnection();
con.setUseCaches(true);
con.setDoOutput(true);
con.setDoInput(true);
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteout);
out.writeUTF("hello world!");
out.flush();
byte buf[] = byteout.toByteArray();
con.setRequestPRoperty("Content-type","application/octest-stream");
con.setRequestProperty("Content-length",""+buf.length);
DataOutputStream dataout = new DataOutputStream(con.getOutputStream());
dataout.write(buf);
dataout.flush();
dataout.close();

DataInputStream in = new DataInputStream(con.getInputStream());
response = in.readUTF();
System.out.println("read from server :" + response);
in.close();
}
catch(Exception e){
e.printStackTrace();
}

}

public void paint(Graphics g){
g.drawString("read from server :" + response,10,20);

}
}


Servlet source





import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class DataStreamEcho extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException ,java.io.IOException
{
DataInputStream in = new DataInputStream(req.getInputStream());
resp.setContentType("application/octest-stream");
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(byteout);
String response = in.readUTF();
out.writeUTF(response);
out.flush();
byte buf[]= byteout.toByteArray();
resp.setContentLength(buf.length);
ServletOutputStream servletout = resp.getOutputStream();
servletout.write(buf);
servletout.close();
}

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表