[Simpleweb-Support] Accessing Variables from within a handle
Brought to you by:
niallg
From: Lisa C. <car...@ym...> - 2013-02-02 03:43:41
|
I'm using the simpleframework for a little utility. Here's the scenario. In one thread I have a horse counter. It keeps track of how many horses are in a barn. In the other thread I have the simple webserver. public class http_responder implements Container { private http_responder container; private Server server; private Connection connection; private SocketAddress address; private HorseCounter horsecounter; public void handle(Request request, Response response) { try { PrintStream body = response.getPrintStream(); long time = System.currentTimeMillis(); response.setValue("Content-Type", "text/plain"); response.setValue("Server", "HelloWorld/1.0 (Simple 5.0.4)"); response.setDate("Date", time); response.setDate("Last-Modified", time); body.println("Horses in Barn: " + horsecounter.GetNumberOfHorses()); body.close(); } catch(Exception e) { e.printStackTrace(); } } public void serveit(HorseCounter horsecounter) throws Exception { this.container = new http_responder(); this.server = new ContainerServer((Container) container); this.connection = new SocketConnection(server); this.address = new InetSocketAddress(8080); this.horsecounter = horsecounter; this.connection.connect(address); } } My question is: How do I get my horsecounter passed into the handle? I tried passing it as a variable to the serveit method, but I just figured out that interfaces can't hold variables. (I'm new to Java) It makes sense to me why it didn't work, but I'm still in the dark as to how to serve up dynamic content using variables in my running application. I've looked through the JavaDocs, but I got overwhelmed pretty quickly. Would you mind pointing me in the right direction? |