java - sun Httpserver: access from the Handler to an object created externally -
java - sun Httpserver: access from the Handler to an object created externally -
maybe dumb question: i'm trying realize little server in java com.sun.net.httpserver package. @ origin of server programming, i'm missing something.
it should work this:
first, creates object (an hashmap) lately , periodically updated every 24 hours then there handler process requests received. processing phase done basing on content of hashmap, created outside handler.pseudo code (something dirt)
public static void main(string args[]){ // creation of hashmap (which has periodically updated) httpserver server = httpserver.create(new inetsocketaddress(8000), 0); server.createcontext("/hashmap", new handler()); server.start(); } class handler implements httphandler { public void handle(httpexchange xchg) throws ioexception { //operations involves (readonly) hashmap created } }
the question is: how allow handler read hashmap? there way pass object parameter handler?
yes, wrapper class:
public class httpserverwrapper{ private hashmap map = ...; public httpserverwrapper(int port) { httpserver server = httpserver.create(new inetsocketaddress(port), 0); server.createcontext("/hashmap", new handler()); server.start(); } public static void main(string args[]){ int port = 8000; new httpserverwrapper(port); } public class handler implements httphandler { public void handle(httpexchange xchg) throws ioexception { map.get(...); } } }
java hashmap httphandler com.sun.net.httpserver
Comments
Post a Comment