246 CHAPTER 8 PERSISTENT (Free web host) COMMUNICATIONS PATTERN protected
246 CHAPTER 8 PERSISTENT COMMUNICATIONS PATTERN protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int lastCount = new Integer(request.getHeader( “X-Version-ID”)).intValue(); int waitCount = 0; while(waitCount < 100) { if(lastCount < _callCount) { PrintWriter out = response.getWriter(); out.println("Content (" + _buffer + ") Call Count (" + _callCount + ")(" + Calendar.getInstance().getTime() + ")"); response.setHeader("X-Version-ID", new Integer(_callCount).toString()); return; } try { Thread.currentThread().sleep(1000); } catch (InterruptedException e) { } waitCount ++; } response.setStatus(408, "No change"); } When the doGet method is called, the first thing that happens is the extracting of the X-Version-ID header value, which is assigned to lastCount. The next step is to enter a while loop that will count for 100 times, testing for updated data. Within the loop, a test is performed to check whether the sent counter (lastCount) is less than the global counter (_callCount). If the test returns a true value, the output is generated, resulting in the latest changes being sent to the client. Generated in the output is the HTTP header X-Version-ID with the latest version identifier. If there is nothing to send, the current thread is put to sleep by using the method Thread.currentThread().sleep(1000) for a short period of time before beginning a new iteration that tests whether there is new data. If after 100 iterations there are no changes, the HTTP 408 return code is generated, indicating a time-out. Looking at the HTTP header implementation, the cynical reader would think that the alternate solution looks strikingly similar to an HTTP cookie. In fact, the cynical reader is absolutely correct, but the solution was illustrated to show that HTTP cookies are very useful when used properly. The advantage of using HTTP cookies in contrast to the proposed solution is that there are no necessary changes to make to the client-side code. Only on the server side are changes necessary, and they are very similar to the proposed solution. Before we move to the next topic, one last item has to be covered. In the original declaration of GlobalStatus, the class implemented the SingleThreadModel interface. This is a unique feature of Java, which says that only one client can call the servlet. I don t advise using the SingleThreadModelfor your applications, but it was done for simplicity and ease of illustration. When writing two-stream communication mechanisms, most likely files, object instances, and so on will be shared, which means the data must be synchronized.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.