Shared web hosting - CHAPTER 9 STATE NAVIGATION PATTERN 301 The

June 10th, 2008

CHAPTER 9 STATE NAVIGATION PATTERN 301 The method getStateWindowName is used to retrieve an array State interface instance based on the name of a window. In the StateFilter class implementation, the method is not used because the method is intended to be used by some other processor carrying out some application logic. The method getEmptyState returns an empty State instance based on the URL and window name. The method copyState is used to transfer the state of one state instance to another. The method copyStatemight do a physical copy from one State instance to another Stateinstance. Or the method copyState might do an in-place copy. It depends on the implementation of StateManager and is kept flexible for diversity purposes. Pattern Highlights The State Navigation pattern is used to solve the web application usability problem associated with HTTP POST and with the inconsistencies of running a web application using multiple web browsers. Using the State Navigation pattern, you can separate the state of an HTML page from the HTML page. With a separation, it is simpler to manage and accumulate state that can be used by a process to execute a single transaction. The State Navigation pattern requires active participation by the programmer to make everything work and as such could be prone to problems. The following are the important highlights of the State Navigation pattern: The pattern is used to associate a state with an HTML page. E B V N The associated state is in most cases nonbinding, and therefore, if lost, will not cause an application malfunction. In the worst case, a lost state results in the user having to reenter the data. HTML frames, when used extensively, may pose a problem for the pattern because the way that the browser manages navigation is modified and typically frames are given a name. Normally frames are not problematic, but if HTML frames are used, you should build a prototype so that there are no surprises. The pattern makes it possible to build applications that are transaction friendly because the state is cumulated by a state manager and can later be referenced as single action. The pattern provides a consistent user interface because posting the data is a separate step that is not part of the web browser s history. This solves the problem of posting data again when navigating HTML pages based on the history. The window name is a physical window name but could be used as an application grouping. For example, if a window is popped up, a window name could be reused, creating a relation between two separate HTML windows without sacrificing the ability to try out permutations of a form.
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

300 CHAPTER (Cheap web hosting) 9 STATE NAVIGATION PATTERN process

June 9th, 2008

300 CHAPTER 9 STATE NAVIGATION PATTERN process it? Let s say that I buy a ticket and fill out the form. When I click the Submit button, I want to buy the ticket; but when I click Back, I want to know the form details used to buy the ticket. Knowing the details, I can click Forward, and a ticket will not be bought twice which would have happened if I had to click Submit. Therefore, to buy the ticket, some handler has to process the posted data, thus requiring the State filter to store the data and to let the handler process the data. An Example State Manager Handler In the StateFilter implementation, the variable _stateManager references the type StateManager. The type StateManager is an interface and manages the state that is posted and retrieved. Using interfaces makes it possible to separate intention from implementation as per the Bridge pattern. The State interface is defined as follows: public interface State public String getURL() public void setURL( String URL) public String getWindowName() public void setWindowName( String windowname) public String getBuffer() public void setBuffer( String buffer) public String getStateIdentifier() E B V N public void setStateIdentifier( String hashcode); } The interface is based on four properties (URL, WindowName, Buffer, and StateIdentifier) that are implemented as getters and setters. The property Buffer is used to assign and retrieve the state sent by the client. The property StateIdentifieris used to assign and retrieve the state identifier of an HTML page. The property URLis the URL of the state, and finally WindowNameis the associated window name. A minimal implementation of the Stateinterface would define four private data members of the type String, String, String, and String. What is more complicated is the implementation of the StateManager interface. An advanced implementation is beyond the scope of this book and depends on the context of the problem. The StateManager interface is important in the overall architecture because it is meant to be shared by servlets and external processes. A servlet could be used to manage and accumulate the state, whereas a J2EE server could be used to execute the transaction on the accumulated state. The idea is to implement the State filter and let the architecture manage the state. The StateManager interface is defined as follows: public interface StateManager { public State getEmptyState( String url, String windowName); public State copyState( String stateIdentifier, String url, String windowName); public State[] getStateWindowName( String windowName); }
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

CHAPTER 9 STATE NAVIGATION PATTERN else { (Graphic web design)

June 9th, 2008

CHAPTER 9 STATE NAVIGATION PATTERN else { state = _stateManager.copyState( data._stateHeader, data._path, data._windowName); } httpresponse.setContentType( _resourceStateContentType); httpresponse.setHeader( _XPageState, state.getStateIdentifier()); httpresponse.setStatus( 200, “success”); PrintWriter out = httpresponse.getWriter(); out.print( state.getBuffer()); return; } else if( data._operation == OP_POST) { ServletInputStream input = httprequest.getInputStream(); byte[] bytearray = new byte[ httprequest.getContentLength()]; input.read( bytearray); State state = _stateManager.copyState( data._stateHeader, data._path, data._windowName); state.setBuffer( new String( bytearray).toString()); httpresponse.addHeader( _XPageState, state.getStateIdentifier()); chain.doFilter( httprequest, httpresponse); return; } E B V N } In the implementation of runFilter, the first parameter is the object instance allocated by the method initializeRequest. And as with isTrigger, a typecast is made to convert the type and assign it to the variable data. From there, the decision blocks are based on the data members of the variable data that were assigned in isTrigger. There are two state operations: retrieve state and post state. The first decision block (== OP_RETRIEVE) tests whether the operation is a state retrieval, and the second decision block (== OP_POST) tests whether the operation is a post. If the operation is a state retrieval and the asked-for state is none (indicating that the client has not associated a state with an HTML page), an empty state is created. A new empty state is created by using the method getEmptyStateHashcode(), and the method getHashcode()retrieves the hash code of a state. By default, when creating an empty state, a hash code will automatically be created. Using the method copyState copies the old state to a new state, and is explained shortly. After calling the copyState method, various methods on the httpresponsevariable are called to generate the response. If the operation is a posting, the posted stream is retrieved from the request by using the method input.read. The read buffer is stored in the variable bytearray, which happens to be an array of bytes. As when a state is retrieved, the state is copied by using the method copyState, and then assigned by using the method state.setBuffer. The state is copied from the original reference, and the new data overwrites the old. By copying a state, the state manager can create a trail of dependencies and associations that could be used by the state manager for optimization purposes. In the response, the newly generated state header is added by using the method addHeader. The last and very important step is to call the method chain.doFilter because that allows the posting to be processed by a handler. This raises the question, If the state is stored, why
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

298 CHAPTER 9 STATE NAVIGATION (Web hosting colocation) PATTERN else

June 8th, 2008

298 CHAPTER 9 STATE NAVIGATION PATTERN else if( data._method.compareTo( “PUT”) == 0 || data._method.compareTo( “POST”) == 0) { if( _resourceStateContentType.compareTo( httprequest.getContentType()) == 0) data._path = httprequest.getHeader( _XPageOriginalURL) data._operation = OP_POST; return true; data.reset() return false; } The method isTrigger is used to determine whether the method runFilter should execute, and if so, isTrigger populates the Data type instance. That way, if runFilter executes, runFilter will not need to organize the details of the state or resource call. For the method isTrigger, the first parameter inpdatais the object instantiated by the method initializeRequest. Hence the first step of isTrigger is to typecast the parameter to the type Data and assign the instance to the variable data. The variable tail is the end of the URL and is used to test whether the state identifier /state is present. If the identifier does exist as per the decision (if( tail.compareTo( _URLStateIdentifier…), the URL assigned to data._path must not contain the state keyword. If the URL does not contain the state keyword, a test is made to see whether the state E B V N header (stateHeader) exists. If the state header does not exist, the request is not a State Navigation request. If the state header does exist, the URL assigned to data._pathis the same as the input URL. The example illustrates testing for two conditions, but it is possible to test for only a single condition and make a decision. The example of two conditions was shown to illustrate the code for each condition. If the code after the initial decision block is reached, we are assured the request is a State Navigation request and the standard variables can be assigned. The variables data._method, data._stateHeader, and data._windowName are assigned to the HTTP method, HTTP header, and window name, respectively, so that they may be used by the runFilter method. The last decision block inthe implementation of isTrigger tests which State filter operation is being executed. The operation can be one of two values: HTTP GET or HTTP POST. Support is added for the HTTP PUT, which is classified as an HTTP POST. If either decision block returns a true value, the data member data_operation is assigned to OP_RETRIEVEor OP_POST. Having isTrigger return a truevalue will cause the runFilter method to be executed, which is implemented as follows: public void runFilter(Object inpdata, HttpServletRequest httprequest, HttpServletResponse httpresponse, FilterChain chain) throws IOException, ServletException { Data data = (Data)inpdata; if( data._operation == OP_RETRIEVE) { State state; if( data._stateHeader.compareTo( “none”) == 0) { state = _stateManager.getEmptyState( data._path, data._windowName); }
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

CHAPTER 9 STATE NAVIGATION PATTERN 297 _path

June 5th, 2008

CHAPTER 9 STATE NAVIGATION PATTERN 297 _path = null; _windowName = null; public Object initializeRequest() return new Data() public void destroyRequest( Object objData) The class Data is declared as a private class and is used only in the scope of the StateFilter class. Five publicly declared data members reference the HTTP method, HTTP state header, window name, path representing the URL, and locally defined operation type. In the implementation of initializeRequest, a new instance of Data is returned. There is no implementation for destroyRequestbecause it is not necessary to do anything when the object is destroyed. The third piece of the State filter is the code to test whether the request or post is related to manage the server-side state: private static final int OP_NONE = 0; private static final int OP_RETRIEVE = 1; private static final int OP_POST = 2; public boolean isTrigger( Object inpdata, HttpServletRequest httprequest, E B V N HttpServletResponse httpresponse) { String tail = httprequest.getRequestURI().substring( httprequest.getRequestURI().length() - _URLStateIdentifierLength); String stateHeader = httprequest.getHeader( _XPageState); Data data = (Data)inpdata; if( tail.compareTo( _URLStateIdentifier) == 0) { data._path = httprequest.getRequestURI().substring( 0, httprequest.getRequestURI().length() - _URLStateIdentifierLength); } else { if( stateHeader == null) { return false; } data._path = httprequest.getRequestURI(); data._method = httprequest.getMethod() data._stateHeader = stateHeader; data._operation = OP_NONE; data._windowName = httprequest.getHeader( _XPageWindowName) if( data._method.compareTo( “GET”) == 0) data._operation = OP_RETRIEVE; return true;
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

296 CHAPTER 9 STATE NAVIGATION PATTERN public (Disney web site)

June 5th, 2008

296 CHAPTER 9 STATE NAVIGATION PATTERN public void init(FilterConfig filterConfig) throws ServletException { _filterConfig = filterConfig; _resourceStateContentType = filterConfig.getInitParameter( “resource-state-content-type”); _XPageState = filterConfig.getInitParameter( “page-state-header”); _XPageWindowName = filterConfig.getInitParameter( “page-window-name”); _URLStateIdentifier = filterConfig.getInitParameter( “url-state-identifier”); _URLStateIdentifierLength = _URLStateIdentifier.length(); _XPageOriginalURL = filterConfig.getInitParameter( “page-original-url”); try { String strClass = filterConfig.getInitParameter(”state-manager”); _stateManager = (StateManager) StateFilter.class.getClassLoader().loadClass( filterConfig.getInitParameter( “state-manager”)).newInstance(); } catch (Exception e) { throw new ServletException( “Could not instantiate _stateManager”, e); } } E B V N In the implementation of StateFilter, the data memberassignmentsare dynamic and can be specified in the HTTP server configuration file. Not all data members will be explained because that would be too lengthy and redundant. The data members _resourceStateContentType and _XPageState are the counterparts to the client-side-defined StateController. constResourceStateContentTypeand StateController.constPageStateHeader data members, respectively. The data member _stateManageris the state manager implementation. The idea is that the filter manages the state retrieval and storage calls, whereas _stateManager is the imple mentation of the retrieval and storage of the state. By separating the actual doing from the calling functionality, the doing can determine which persistence medium is used. For the scope of this book, the persistence medium is the memory, but could also be implemented to use a database or hard disk. The second piece of code relates to the object used to manage state and resource reference information that is created on a per request instance and is passed to the isTrigger and runFilter routines. The implementations for initializeRequest and destroyRequest are as follows: private class Data public String _method; public String _stateHeader; public String _windowName; public int _operation; public String _path; public void reset() _method = null; _stateHeader = null; _operation = OP_NONE;
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

CHAPTER 9 STATE NAVIGATION PATTERN 295 public (Web design online)

June 4th, 2008

CHAPTER 9 STATE NAVIGATION PATTERN 295 public interface Router { public void setConfiguration( String key, String value); public boolean IsResource(HttpServletRequest request); public void WriteRedirection( Rewriter rewriter, HttpServletRequest request); } The modification of the interface involves the addition of the setConfiguration method, which assigns the configuration information. The configuration information is used by the Router interface implementation when figuring out whether a request is a resource or a specific representation. The method WriteRedirection has been modified to include the parameter rewriter. As the configuration information is passed to the Router interface instance, having the parameter rewritermay not seem necessary. It is necessary because otherwise a hidden dependency in the implementation of the interfaces is created, complicating the development of modular code. The implementations of the Rewriter and Router interfaces remain as illustrated in the Permutations pattern. The resulting implementation is a prototype example for the server side that can be used to filter implementations. When implementing the Decorator pattern, the filters should be stacked by using the HTTP filter mechanism. What is important is the ordering of the filters, because some HTTP filter implementations have an ordering dependency. Implementing the State Layer In Figure 9-12, the Resource to Representation filter appears after the State filter, which is E B V important so that not all requests need to be processed. For example, when retrieving the associated state of a resource, it is not necessary to execute a handler. The State layer captures the associated state request and processes it directly. Managing the State Calls As per the previous discussion, the filter needs to implement two functionalities: storing the state and retrieving the state. The State layer will extend the TriggerFilter class, and the implementation will be outlined in four pieces. The first piece is the filter initialization: public class StateFilter extends TriggerFilter private FilterConfig _filterConfig; private StateManager _stateManager; private String _resourceStateContentType; private String _XPageState; private String _XPageWindowName; private String _URLStateIdentifier; private int _URLStateIdentifierLength; private String _XPageOriginalURL;
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

294 CHAPTER 9 STATE NAVIGATION PATTERN public (Free web space)

June 4th, 2008

294 CHAPTER 9 STATE NAVIGATION PATTERN public Object initializeRequest() { return null; public void destroyRequest( Object objData) } public boolean isTrigger(Object objData, HttpServletRequest request, HttpServletResponse response) { if (_router.IsResource( request)) { return true; return false; } public void runFilter(Object objData, HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { Rewriter rewriter; try { rewriter = (Rewriter)ResourceEngineFilter.class.getClassLoader(). loadClass(_clsRewriter).newInstance(); } catch( Exception ex) { E B V N return; } _router.WriteRedirection( rewriter, request); In the example class ResourceEngineFilter, the methods isTrigger and runFilter are implemented, as required by TriggerFilter. The method init is used to initialize the filter and retrieve the filter configuration information, and specifically the base-directory that is used by the class FilterRouter or the Router interface instance. In the implementation of init, the default Router instance _router is instantiated by using the configuration declaration item router. In contrast, in the Permutations pattern implementation, the instantiation of Router was hard-coded. Regardless of how the Router interface instance is instantiated, in the example of ResourceEngineFilter, the Router interface instance must be stateless with respect to the HTTP request. The statelessness is required because the Router instance is associated with the ResourceEngineFilter, which is also stateless. What is not stateless, but is instantiated with every triggered filter request, is the Rewriter interface instance. This is because the implementations of the Rewriter will require multiple calls, and the calls will reference some state generated by the HTTP request. The statelessness results in a modified version of the Routerinterface that is defined as follows:
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

CHAPTER 9 STATE NAVIGATION PATTERN filter instance. (Cool web site)

June 3rd, 2008

CHAPTER 9 STATE NAVIGATION PATTERN filter instance. They are not illustrated for simplicity purposes. The class TriggerFilteris implemented as an abstract class because TriggerFilteron its own is not very useful and provides a basic functionality that would otherwise be constantly implemented. TriggerFilter implements the Template pattern, and therefore to have anything happen, some class has to subclass TriggerFilter. The method doFilter is part of the interface Filter and is called whenever an HTTP request is made. When the method doFilter is called depends on the order of the filter in the configuration file. The order in the configuration file is a Java feature, and other platforms may have other ways to define the order indicating when a filter is called. When the method doFilter is called, the parameters requestand response are converted into the types HttpServletRequestand HttpServletResponse, respectively. This is necessary because the Httptypes offer methods and properties that help process an HTTP request. The methods isTriggerand runFilter are declared abstract, which means any class that extends TriggerFilterwill need to implement the abstract methods. The method isTrigger is called to check whether the request should be processed by the implemented subclass. The method runFilter is executed to process the HTTP request. If isTriggerreturns a value of false, the HTTP request processing continues as usual, and in the case of the example the method chain.doFilter is called. The State Navigation patternis implementedby using the TriggerFilter class,but before the architecture of the State Navigation pattern is detailed, the Permutations Pattern is rewritten to use the TriggerFilter class. E B V N Rewriting the Permutations Pattern Implementation The purpose of rewriting the Permutations pattern is to illustrate how the pattern can be implemented as a filter instead of a handler. The difference between a handler and filter is not huge, but there are some structural changes. Following is the implementation of the Permutations pattern using Java: public class ResourceEngineFilter extends TriggerFilter private FilterConfig _filterConfig; private Router _router; private String _clsRewriter; public void init(FilterConfig filterConfig) throws ServletException { _filterConfig = filterConfig; try { _router = (Router)ResourceEngineFilter.class.getClassLoader().loadClass( filterConfig.getInitParameter(”router”)).newInstance(); _router.setProperty( “base-directory”, baseDirectory); _clsRewriter = filterConfig.getInitParameter(”rewriter”); } catch (Exception e) { throw new ServletException( “Could not instantiate classes “, e); } }
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

292 CHAPTER 9 STATE NAVIGATION (Graphic web design) PATTERN GET

June 3rd, 2008

292 CHAPTER 9 STATE NAVIGATION PATTERN GET /ajax/chap07/page1/state HTTP/1.1 Accept: application/ajax-state Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 X-Page-Window-Name: window-2005-10-03-10-10-10-1245 X-Page-State: none The request has two pieces of information that could trigger a filter: URL ([url]/state) and HTTP header (X-Page-State). To trigger a filter via a URL, the URL must be processed. Processing a URL is a relatively expensive step and potentially buggy. The bugginess results when a URL has the same text as a trigger. The header is necessary only when the state is retrieved, and that occurs only when the XMLHttpRequest object is used. Thus adding an HTTP header is not complicated or inconvenient. Which solution you use depends on your URLs and what you are comfortable with. The rule of thumb is that an HTTP header can be used when both the client and the server are capable of processing the custom header, and a URL should be used whenever the server knows what kind of client will process the data. When implementing an HTTP filter, a basis class that executes the trigger and runs the filter action is illustrated by using the following Java filter code. On other platforms and program ming languages, the code will be similar because other platforms also have the concept of an HTTP filter. E B V N public abstract class TriggerFilter implements Filter { public abstract Object initializeRequest(); public abstract void destroyRequest( Object objData); public abstract boolean isTrigger( Object objData, HttpServletRequest httprequest, HttpServletResponse httpresponse); public abstract void runFilter( Object objData, HttpServletRequest httprequest, HttpServletResponse httpresponse, FilterChain chain) throws IOException, ServletException; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httprequest = (HttpServletRequest)request; HttpServletResponse httpresponse = (HttpServletResponse)response; Object data = initializeRequest(); if( isTrigger( data, httprequest, httpresponse)) { runFilter( data, httprequest, httpresponse, chain); } else { chain.doFilter( request, response); } destroyRequest( data); } } When implementing a Java filter, the class implements the Filter interface. Two methods are not illustrated: init and destroy, which are used to initialize and destroy, respectively, the
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.