10 Replies Latest reply: Nov 16, 2006 7:24 AM by 807607 RSS

    URLConnection connection = url.openConnection();

    807607
      I try to invoke the next code:
      URL url = new URL("http://localhost:8080/proxy/servlet/servlet.ProxyServlet");
      URLConnection connection = url.openConnection();
                
      connection.setDoOutput(true);
      connection.setDoInput(true);
      connection.setUseCaches(false);
      connection.getInputStream(); //attention!
      It was interesting, but servlet method service() invoked when I use connection.getInputStream();
      Why?
        • 1. Re: URLConnection connection = url.openConnection();
          807607
          because servlet containers invoke that method in order to get the servlet to do something. I believe if you're extending HttpServlet that doPost and doGet are delegated to by the service() method
          • 2. Re: URLConnection connection = url.openConnection();
            807607
            That is good, but I want that service method invokes only when I do openStream.flush(), not when I just get InputStream.
            By the way, Sockets don't use this technique.
            • 3. Re: URLConnection connection = url.openConnection();
              807607
              when u call the getInputStream() method, the URLConnection instance opens a connection and issues a request if not issued allready earlier (by a call to the connect() method for example).
              As george wrote, at the server side, your web container simply invokes the appropriate servlet.
              Nothing too interesting about it ;-)
              • 4. Re: URLConnection connection = url.openConnection();
                807607
                That is good, but I want that service method invokes
                only when I do openStream.flush(), not when I just
                get InputStream.
                Can you explain why you need it to work like that? It sounds like a strange requirement.
                • 5. Re: URLConnection connection = url.openConnection();
                  807607
                  what is openStream?
                  Whatever that is, u should call getInputStream() only when you're done sending data to the server, do all u need to do with the connection and then call getInputStream().
                  URLConnection, or HttpURLConnection in this case is of course based on sockets and adds all the prtocol related functionnality, it has nothing to do with "techniques". It encapsulates the HTTP protocol over a socket connection, and exposes all u need, the usage obviously is simply a little different from a plain socket....
                  • 6. Re: URLConnection connection = url.openConnection();
                    807607
                    Thank you, I understand it!
                    I just forgot to invoke connect() method:

                    look to it specification
                    public abstract void connect()
                                          throws IOException
                    ...
                    After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
                    • 7. Re: URLConnection connection = url.openConnection();
                      807607
                      My last investigation:

                      Here is updated code source:
                      URL url = new URL("http://localhost:8080/proxy/servlet/servlet.ProxyServlet");
                      URLConnection connection = url.openConnection();
                                
                      connection.setDoOutput(true);
                      connection.setDoInput(true);
                      connection.setUseCaches(false);
                                      
                      connection.connect();                    
                      OutputStream out = connection.getOutputStream();
                      out.write("hello world".getBytes());          
                      out.close();          
                      
                      ((HttpURLConnection) connection).getResponseCode(); //attention
                      InputStream in = connection.getInputStream();
                      int first = in.read();
                      in.close();          
                      As I noticed when you write bytes in output stream they are stored in buffer and doesn't send to server right now.
                      (See HttpURLConnection API description)
                      But when, in my first topic example, I tried to receive input stream, method service() had invoked on servlet.
                      Do you know why? Because, in this time buffer flushed and data sent to server. I noticed, that direct flush() method invoking on output stream are not produce this effect. So, I noticed that buffer also flushed when you invoke method getResponseCode() (see code string with comment attention)
                      and then you may work with input stream. Now I'm interesting, do you now aesthetically beautiful method which may send output stream buffer to server, except getResponseCode() invoking?
                      • 8. Re: URLConnection connection = url.openConnection();
                        807607
                        Any method u call that requires the server's response will flush all data left on the client side buffer. The flush operation may be called several times (if u write a lot of data), or only once when the connection object decides u r completely done with sending data (like in the case of trying to obtain any part of the response - getResponseCode, getInputStream, etc.).
                        The nice thing about it here is that u shouldn't care. It's implementation specific and part of the idea behind abstraction - u r using an object representing an http connection, u shouldn't deal nor care about technical stuff like when the data is flushed.
                        • 9. Re: URLConnection connection = url.openConnection();
                          807607
                          As I know HTTP Protocol it needs trailing Content-Length in order to send data to server. So, how I can set it in Java 1.4 or maybe it may count automatic?
                          • 10. Re: URLConnection connection = url.openConnection();
                            807607
                            I doubt it'll count automatically, if it does it's implementation specific and not required, so don't rely on it.
                            Moreover it's only for requests that have a body (which means u don't need it for a GET request).
                            I believe u set it by calling setRequestProperty() method.
                            All this aside, If u ask me, I'd advise u to use apache's http client open source project, however imperfect it is (at least the last version I used about a year ago) it's better and more intuitive than the JDK classes.