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);
}
}
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();
}
}
新闻热点
疑难解答