WEB开发网
开发学院WEB开发Jsp Applet用Http隧道技术和Servlet通讯 (源码) 阅读

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

 2008-01-05 09:28:47 来源:WEB开发网   
核心提示:Applet用Http隧道技术和Servlet通讯 (源码) 作者:jdeveloper Applet source import java.io.*;import java.applet.Applet;import java.awt.*;public class TestDataStreamApplet extend

  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();
}

}

Tags:Applet Http 隧道

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接