5 Replies Latest reply: Aug 1, 2012 10:15 PM by EJP RSS

    seeking problem for how to load object in class Test to class Server...

    948966
      hello everyone, currently, i put some method named calla() in class Test....i have difficulty when to invoke the method from class Server...this is my Server class code
      import java.io.BufferedInputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.net.InetSocketAddress;
      import java.net.URI;
      
      
      import com.sun.net.httpserver.HttpExchange;
      import com.sun.net.httpserver.HttpHandler;
      import com.sun.net.httpserver.HttpServer;
      import com.sun.net.httpserver.Headers;
      
      public class SimpleHttpServer2{
      public static void main(String[] args) throws Exception { // for the main method (basic main method)
      
      
      
      HttpServer server = HttpServer.create(new InetSocketAddress(12345), 0);
      server.createContext("/", new RootHandler());
      server.setExecutor(null); // creates a default executor
      server.start();
      
       MainDialog a = new MainDialog();
       a.calla();
      
      }
      
      static class RootHandler implements HttpHandler {
      public void handle(HttpExchange t) throws IOException {
      
      
      URI xyz = t.getRequestURI();
      String response = "";
      String resp = "" + xyz;
      
      
      if(resp.equals("/?act=1")){
      // put in wait_doc code
      // then if a document is ready,
      // put the results of the read in response below
      response = "Waiting to read";
        // ClassLoader newClassLoader = new 
         //       java.net.URLClassLoader( newClassLoader.calla );
      
      // response = a.calla();
      }else if(resp.equals("/?act=2")){
      response = "Initialise the reader";
      }else
      response = "Error!";
      
      t.sendResponseHeaders(200, response.length());
      OutputStream os = t.getResponseBody();
      os.write(response.getBytes());
      os.close();
      }
      }
      i hope u guys can pull me out from this problem...thank you
        • 1. Re: seeking problem for how to load object in class Test to class Server...
          EJP
          I have difficulty
          You have what difficulty?
          • 2. Re: seeking problem for how to load object in class Test to class Server...
            gimbal2
            Well check out this piece of commented out magic:
              // ClassLoader newClassLoader = new 
               //       java.net.URLClassLoader( newClassLoader.calla );
             
            // response = a.calla();
            ... what?
            • 3. Re: seeking problem for how to load object in class Test to class Server...
              948966
              i want to run it in my browser....but the process didnt show up...its just run in my IDE called Netbeans....bcause i can see i put the object at main class...so, it just directly call it...but, what i want to do is, if at browser, i type : http://localhost:12345/?act=1 , it will return to the browser, the process that include in calla() inside Test class....when i put this object below main class, i know, this browser process that calla(), but it just display on my Netbeans not to browser...then, im try use ClassLoaderURI, but still not get....this is my final step in doing my project...if i can invoke this calla() into my browser, then, my project will finish...so, any idea, how to call....previously, im just try put this mehod inside RootHandler method like this:
              HttpServer server = HttpServer.create(new InetSocketAddress(12345), 0);
              server.createContext("/", new RootHandler());
              static class RootHandler implements HttpHandler {
              public void handle(HttpExchange t) throws IOException {
              
              
              URI xyz = t.getRequestURI();
              String response = "";
              String resp = "" + xyz;
              // URI z;
              
              if(resp.equals("/?act=1")){
              // put in wait_doc code
              // then if a document is ready,
              // put the results of the read in response below
              response = "Initialise the reader";
              MainDialog abc = new MainDialog();
              abc.Initialise();
              System.out.println("response: " + abc);
                // ClassLoader newClassLoader = new 
                 //       java.net.URLClassLoader( newClassLoader.calla );
              
              // response = a.calla();
              }else if(resp.equals("/?act=2")){
              response = "Waiting the reader";
              MainDialog abc = new MainDialog();
              abc.Reader();
              }else
              response = "Error!";
              
              t.sendResponseHeaders(200, response.length());
              OutputStream os = t.getResponseBody();
              os.write(response.getBytes());
              os.close();
              }
              }
              but, its just run in my IDE again, not to browser...so, how to settle this problem??
              • 4. Re: seeking problem for how to load object in class Test to class Server...
                DrClap
                Well, if that code is running at all then you should examine the result of this line of code:
                String resp = "" + xyz;
                Of course you should also check whether that code is running at all. You said absolutely nothing about what kind of response your browser gets, so your problem could be anything including that server not even listening for responses. So go and do some problem documentation and report back with something useful.
                • 5. Re: seeking problem for how to load object in class Test to class Server...
                  EJP
                  You are supposed to send HTML back to a browser.

                  I can't make any sense out of this line:
                  t.sendResponseHeaders(200, response.length());