我在网上搜的时候,HttpConnection这个词频繁出现,但是我发现这个好像是J2ME开发里才有的类包,不知道J2EE里应该怎么连接。
我大概解释一下我的需求:
1.写一个servlet,接收别人以GET或POST(2种方式都要支持)提交给我的数据.
2.写另外一个servlet,以GET或POST(2种方式都要支持)方式提交我的数据信息给别人的一个URL,别人在接收到我的信息后会给我一个反馈信息,我要拿到这个反馈信息并记录到日志里。
小弟是初学JAVA,只知道PHP里是怎么做的,由于时间紧,元旦还在加班,忘达人赐教,不胜感激。

解决方案 »

  1.   

    你可以参考一下最简单的Socket方法
      

  2.   

    用Socket ServerSocket就可以了
      

  3.   

    各位大哥们不要说得这么笼统呀,呵呵,只能用socket吗?还有那位叫我参考tomcat源代码的老兄,在哪个包里?我装的是apache-tomcat-5.5.14,都集成好了的,有你说的那个源代码吗?Tomcat根目录下好多目录啊!
      

  4.   

    我在看JAVA编程思想,里面socket就讲了没几页,不是很详细。估计可以了解一下了,呵呵。
      

  5.   


    java.net.URL u=null;
    java.net.URLConnection uc=u.openConnection();http://www.google.com/search?q=URLConnection
      

  6.   

    我在书上看到个socket例子。我javac JabberClient.java会生成JabberClient.class和JabberServer.class,但是执行时得到如下结果,很是奇怪,为什么全是null呢?
    addr = localhost/127.0.0.1
    socket = Socket[addr=localhost/127.0.0.1,port=8080,localport=1933]
    null
    null
    null
    null
    null
    null
    null
    null
    null
    null
    closing...//: c15:JabberServer.java
    // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
    // www.BruceEckel.com. See copyright notice in CopyRight.txt.
    // Very simple server that just
    // echoes whatever the client sends.
    import java.io.*;
    import java.net.*;public class JabberServer {  
      // Choose a port outside of the range 1-1024:
      public static final int PORT = 8080;
      public static void main(String[] args) 
          throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Started: " + s);
        try {
          // Blocks until a connection occurs:
          Socket socket = s.accept();
          try {
            System.out.println(
              "Connection accepted: "+ socket);
            BufferedReader in = 
              new BufferedReader(
                new InputStreamReader(
                  socket.getInputStream()));
            // Output is automatically flushed
            // by PrintWriter:
            PrintWriter out = 
              new PrintWriter(
                new BufferedWriter(
                  new OutputStreamWriter(
                    socket.getOutputStream())),true);
            while (true) {  
              String str = in.readLine();
              if (str.equals("END")) break;
              System.out.println("Echoing: " + str);
              out.println(str);
            }
          // Always close the two sockets...
          } finally {
            System.out.println("closing...");
            socket.close();
          }
        } finally {
          s.close();
        }
      } 
    } ///:~//: c15:JabberClient.java
    // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
    // www.BruceEckel.com. See copyright notice in CopyRight.txt.
    // Very simple client that just sends
    // lines to the server and reads lines
    // that the server sends.
    import java.net.*;
    import java.io.*;public class JabberClient {
      public static void main(String[] args) 
          throws IOException {
        // Passing null to getByName() produces the
        // special "Local Loopback" IP address, for
        // testing on one machine w/o a network:
        InetAddress addr = 
          InetAddress.getByName(null);
        // Alternatively, you can use 
        // the address or name:
        // InetAddress addr = 
        //    InetAddress.getByName("127.0.0.1");
        // InetAddress addr = 
        //    InetAddress.getByName("localhost");
        System.out.println("addr = " + addr);
        Socket socket = 
          new Socket(addr, JabberServer.PORT);
        // Guard everything in a try-finally to make
        // sure that the socket is closed:
        try {
          System.out.println("socket = " + socket);
          BufferedReader in =
            new BufferedReader(
              new InputStreamReader(
                socket.getInputStream()));
          // Output is automatically flushed
          // by PrintWriter:
          PrintWriter out =
            new PrintWriter(
              new BufferedWriter(
                new OutputStreamWriter(
                  socket.getOutputStream())),true);
          for(int i = 0; i < 10; i ++) {
            out.println("howdy " + i);
            String str = in.readLine();
            System.out.println(str);
          }
          out.println("END");
        } finally {
          System.out.println("closing...");
          socket.close();
        }
      }
    } ///:~
      

  7.   

    HttpConnection是URLConnection的子类,
    URL url=new URL("http://www.csdn.net");
    URLConnection uc=(HttpConnection)url.openConnection();
      

  8.   

    HttpConnection这个词频繁出现,但是我发现这个好像是J2ME开发里才有的类包,不知道J2EE里应该怎么连接。
    ====================================
    老兄把j2me和j2ee区别得太开了点吧.既然两者都是被统称为java,用法都是一样的.
      

  9.   

    可以看网络机器人java编程这本书
      

  10.   

    用Socket ServerSocket
    参考一下Socket方法
      

  11.   

    servlet里读数据直接获取request的inputstream来做输入流,读取里面的信息就可以了,做法和socket的类似。主语提交数据信息给别人的一个URL,可以通过URL类获得一个URLConnection然后获得outputstream,方法类似下面:
    public String write(String name){
            String nameInfo = "";
            String result = "";
            try{
                URL url = new URL("http://test.com/test.jsp);
                URLConnection connSpms = url.openConnection();
                if (connSpms instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection=(HttpURLConnection) connSpms;
                    httpConnection.connect();
                
                    String strXML="java test"
                    OutputStream out = connSpms.getOutputStream())),true);
                
                    out.write(strXML);
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println(result+"\n");
            return result;
        }