Web hosting rating - 362 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER

362 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN The configuration of the SearchBuilderclass is relatively simple. The initmethod is over ridden, and the configuration values are retrieved from the configuration file associated with the web application. In the Architecture section of this chapter, the asynchronous interface implements the Persistent Communications pattern, and implements the HTTP GET and HTTP POST, which in Java Servletspeak means to implement the doPost and doGet methods. Following is the imple mentation of the doPost method: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { HttpSession session = request.getSession( true); AsynchronousParent parent = null; if( session.isNew()) { parent = new AsynchronousParent(); session.setAttribute( “parent”, parent); } else { parent = (AsynchronousParent)session.getAttribute( “parent”); } String value = request.getParameter( “query”); String queryIdentifier = request.getHeader( “X-query-identifier”); if( value != null && value.length() > 0) { synchronized( parent) { SearchBuilder.buildCommands( parent); parent.processRequest( new SearchRequest(queryIdentifier, value)); } response.setContentType( “text/xml”); PrintWriter out = response.getWriter(); out.println( “success“); } } At the beginning of doPost, the session is retrieved by using the method getSession and is assigned to the variable session. The session is associated with an HTTP cookie. Using Java servlets, it is possible to query whether a new session has been created by using the method isNew. If isNew returns true, an instance of AsynchronousParent (which is the asynchronous controller) is instantiated and assigned to the session by using the method setAttribute. If isNew does not indicate a new session, the already-existing AsynchronousParent instance is returned by using the method getAttribute. From that point on, there is a valid AsynchronousParent instance that is responsible for instantiating the Command interface instances and collecting the results. To execute a search, the value for the query string variable query is retrieved by using the method getParameter. The method getParameter can be called even though an HTTP POST is made. The query identifier that is stored as an HTTP header is retrieved by using the method getHeader and is assigned to queryIdentifier.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Leave a Reply