CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN (Anonymous web server)
CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN If the query value is not null and has some value, a synchronized block is entered. The synchronized block is important because no two requests should be executing queries at the same time. Think of it this way: a client creates an application in which a user could very quickly generate search requests. Those two very quickly executed requests could run concurrently but should not. Do not make the mistaken assumption that the synchronized block cannot run multiple queries at the same time. It can have multiple queries going at the same time. What is not possible is to start multiple queries at the same time. The problem of starting multiple queries at the same time is that the AsynchronousParent is a session variable that could be associated with multiple web browsers. Remember from previous examples, an HTTP cookie is associated with a URL, and if multiple windows of a web browser reference the same URL, so will the same HTTP cookie. Some readers may argue that my code is not efficient enough. True, but my objective was to illustrate that when executing requests asynchronously, there is one AsynchronousParent instance associated with one session, which is one cookie that collects all results and multiple queries running at the same time. When running in an asynchronous manner, there are concurrency issues to consider that must not be taken lightly. Let s get back to the synchronization block. After the method processRequest is called, the generated output is a simple success. Anything more than a successful result is not required as the Persistent Communications pattern expects results when using the HTTP GET. Before I continue the discussion, a side step is necessary regarding the query identifier. In the Persistent Communications pattern, the query identifier was called a version number. In the Persistent Communications pattern, the version number was used by the server to know when to return data to the client. The query identifier in the example is used to identify the version number of the query. Retrieving a Result To retrieve a result, an HTTP GET is executed, and that means the doGet method needs to be implemented. The implementation of doGet will test the AsynchronousParentinstance for available results and is implemented as follows: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession( true); Object obj = session.getAttribute( “parent”); if( obj != null) { AsynchronousParent parent = (AsynchronousParent)obj; SearchResult result = (SearchResult)parent.getResult(); if( result != null) { response.setHeader( “X-transaction-identifier”, result.getTransactionIdentifier()); PrintWriter out = response.getWriter(); out.println( “
You want to have a cheap webhost for your apache application, then check apache web hosting services.