CSDN博客

img NetSniffer

Applet用Http隧道技术和Servlet通讯[ZZ]

发表于2004/10/14 9:28:00  1173人阅读

分类: JAVA

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

}
 

 --------------------------------------------

 简易的
 Applet与Servlet通讯 (片段源码)
加入时间 2004-3-1 2:01:35 本站域名 www.code365.com
浏览统计 Total:63 | Year:63 | Month:16 | Day:1

Applet与Servlet通讯 (源码) 作者:jdeveloper

Applet source (partly)
...


 
showStatus(textField1.getText());
URL servlet = new URL(this.getDocumentBase(),"servlet/GetParameterServlet");
URLConnection connection = servlet.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
PrintWriter out = new PrintWriter(byteStream, true);
String postData = "Name=" + URLEncoder.encode(textField1.getText());
out.print(postData);
out.flush();
String lengthString = String.valueOf(byteStream.size());
connection.setRequestProperty("Content-Length", lengthString);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byteStream.writeTo(connection.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
textArea1.setText("");
String s;
for(; (s = br.readLine()) != null && s.length() > 0; textArea1.appendText(s + "/n"));
   ...
 

Servlet source

 

 
// GetParameterServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class GetParameterServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
   
    PrintWriter out = response.getWriter();
    String title = "Reading Request Parameters";
    String name = request.getParameter("Name");
    out.println(title);
    out.println("Hello: " + name );   
   
  }

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

 

阅读全文
0 0

相关文章推荐

img
取 消
img