/** * Read a cookie * * @param name The name of the cookie * @return The value of the cookie, if the cookie does not exist it will return an empty string */ protected String getCookie(String name) { /* ** get a specific cookie by its name, parse the cookie. ** not used in this Applet but can be useful */ String myCookie = getCookie(); String search = name + "="; if (myCookie.length() > 0) { int offset = myCookie.indexOf(search); if (offset != -1) { offset += search.length(); int end = myCookie.indexOf(";", offset); if (end == -1) { end = myCookie.length(); } Log.println(this, "get: " + myCookie.substring(offset, end)); if (!myCookie.substring(offset, end).equals(" ")) { return myCookie.substring(offset, end); } else { return ""; } } else Log.println(this, "Did not find cookie: " + name); } return ""; }
/** * This method listens for connection requests and when a connection is made, the serve() method * is called to serve the client. */ public void run() { System.out.println("S: Entering Server run()"); while (!halted) { try { log.println("S: about to call serverSocket.accept()"); Socket clientSocket = serverSocket.accept(); log.println("S: just returned from serverSocket.accept()"); log.println("S: about to create a ClientServicer"); ClientServicer clientServicer = new ClientServicer(clientSocket, nextID++); clientServicer.start(); clientServicers.add(clientServicer); log.println("S: just returned from creating a ClientServicer"); } catch (IOException e) { System.err.println("S: Exception: " + e); e.printStackTrace(); break; } } log.println("S: Exiting Server run()"); }
/** * Write a cookie * * @param name The name of the cookie * @param value The value to write */ protected void setCookie(String name, String value) { value = cleanValue(value); /* ** write a cookie ** computes the expiration date, good for 1 month */ java.util.Calendar c = java.util.Calendar.getInstance(); c.add(java.util.Calendar.YEAR, 1); String expires = "; expires=" + c.getTime().toString(); String s1 = name + "=" + value + expires; Log.println(this, s1); try { JSObject myBrowser = JSObject.getWindow(applet); JSObject myDocument = (JSObject) myBrowser.getMember("document"); myDocument.setMember("cookie", s1); Log.println(this, "set:" + s1); } catch (JSException e) { e.printStackTrace(); } }
/** * Delete a cookie * * @param name The name of the cookie */ protected void deleteCookie(String name) { setCookie(name, " "); /* ** delete a cookie, set the expiration in the past */ java.util.Calendar c = java.util.Calendar.getInstance(); c.add(java.util.Calendar.MONTH, -1); String expires = "; expires=" + c.getTime().toString(); String s1 = name + expires; try { JSObject myBrowser = JSObject.getWindow(applet); JSObject myDocument = (JSObject) myBrowser.getMember("document"); Log.println(this, "del: " + s1); myDocument.setMember("cookie", s1); } catch (JSException e) { e.printStackTrace(); } }
/** * This constructor creates an EchoServer object * * @param portnum The port that the server will listen on. * @throws IOException */ public EchoServer(int portnum, Log log) throws IOException { this.log = log; log.println("S: Entering the constructor for EchoServer"); serverSocket = new ServerSocket(portnum); log.println("S: Exiting the constructor for EchoServer"); }