Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Make httpserver creation public, how?

  1. #1
    Rexilion is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Make httpserver creation public, how?

    This is a continuation of my first thread on this forum. (thanks gimbal2!)

    Now, I would like to properly stop the server. For that, I can do server.stop(); . However, this does not work since the object server is not public, it is contained within the public pc_proxy class.

    How do I do that?

    Java Code:
    import java.net.*;
    import java.io.*;
    
    import com.sun.net.httpserver.*;
    import java.util.concurrent.Executors;
    
    import java.lang.reflect.Array;
    
    public class pc_proxy {
    	
    	public static String pc_path = "/pc/";
    	public static String pc_ext = ".jpg";
    	
    	public static void main(String[] args) throws Exception {
    
    		if ((args.length == 0) || (args.length == 1)) {
    			System.out.println("\ngebruik: pc_proxy host(inet) port(int) debug(bool)\n");
    			System.exit(-1);
    		}
    		
    		String host = (args[0]);
    		int port = Integer.decode(args[1]);
    		
    		HttpServer server = HttpServer.create(new InetSocketAddress(host, port), 0);
    		server.createContext(pc_path, new pc());
    		server.createContext("/exit", new exit());
    		server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
    		server.start();
      	}
      	
    	static class pc implements HttpHandler {
    		public void handle(HttpExchange pce) throws IOException {
    			String pclinkpath = pce.getRequestURI().getPath();
    			String pcnr = pclinkpath.substring(pc_path.length(), pclinkpath.length()-pc_ext.length());
    			
    			URL url = new URL("https://shop.plantconnect.nl/Afbeelding.ashx/" + pcnr);
    			InputStream is = url.openStream();
    			
    			ByteArrayOutputStream photo = new ByteArrayOutputStream();
    			
    			byte[] b = new byte[8192];
    			int length = 0;
    			
    			while ((length = is.read(b)) != -1) {
    				photo.write(b, 0, length);
    			}
          		is.close();
          		
          		OutputStream os = pce.getResponseBody();
          		pce.getResponseHeaders().set("Content-Type","image/jpeg");
          		
          		pce.sendResponseHeaders(200, 0);
          		os.write(photo.toByteArray(), 0, photo.size());
          		os.close();
          	}
        }
        
    	static class exit implements HttpHandler {
    		public void handle(HttpExchange eexit) throws IOException {
    			//server.stop(10);
    			System.exit(0);
    		}
        }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Make httpserver creation public, how?

    Basically you're running before you can walk here. You are trying to create a rather complicated program but you don't even know enough about the Java language yet to work with object instances; I say that because you're making the novice mistake of pasting static in front of everything.

    Take a step back and learn more about the programming language; when you get to the point where you don't need that static keyword anymore (except for the main()), you'll not even have to ask questions like this anymore.
    Rexilion likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Rexilion is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: Make httpserver creation public, how?

    Thank you for your answer.

    It's honest, I like that.

    I don't get the idea of why static is not a good idea, I only want one server. But I guess that if I want to do this 'right', I have to read some more.

    Thanks! I'll get back after some more education, this project is finished for now (it works).

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Make httpserver creation public, how?

    Its not about how many instances you will have at most, its all about code design.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 4
    Last Post: 09-14-2014, 06:16 PM
  2. Replies: 0
    Last Post: 05-18-2012, 01:29 PM
  3. Replies: 4
    Last Post: 10-11-2011, 11:48 PM
  4. com.sun.net.httpserver.HttpServer?
    By JosAH in forum Advanced Java
    Replies: 2
    Last Post: 08-08-2011, 12:30 PM
  5. compiler telling me to make a file for a public class
    By silverglade in forum New To Java
    Replies: 10
    Last Post: 04-26-2011, 07:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •