Bắt đầu với IBM Websphere smash - p 14 ppsx

10 259 0
Bắt đầu với IBM Websphere smash - p 14 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

ptg 112 Chapter 5 Global Context In this case, we’ve created an empty list and posted an element to the list stored in the tmp zone. Next, we append a new list to the stored list using a zget. zget Method To retrieve variables placed in the global context, we use the zget method: Object zget(String location [, Object defaultValue ]); The zget method retrieves the list in the particular global context location. Optionally, you can provide a default value for the list being retrieved. If a default is not provided and the location is not found, a null is returned (see Listing 5.67). Listing 5.67 zget to Retrieve a List from the Global Context $slist = zget("/app/stringList", array()); $ilist = zget("/app/integerList"); Here we are retrieving our string list with a default empty ArrayList and our integer list with no default. We can also retrieve individual elements from our list by again using the #<key> notation (see Listing 5.68). Listing 5.68 zget to Retrieve an Element from a List in the Global Context $s = zget("/app/stringList#0"); $i = zget("/app/integerList#0"); This code retrieves the first element of each list. zdelete Method To remove a list from the global context, we use zdelete: boolean zdelete(String location [, boolean deleteChildren ]); The zdelete method deletes the specified location, removing any values stored there (see Listing 5.69). Listing 5.69 zdelete to Delete a List from the Global Context zdelete("/app/stringList"); zdelete("/app/integerList"); Here we delete both of the lists we’ve previously created and updated. The optional deleteChildren parameter works the same as the object usage of this method. As with zput, we can delete single elements from our list by specifying the #<key> in the location (see Listing 5.70). Download from www.wowebook.com ptg Accessing the Global Context 113 Listing 5.70 zdelete to Retrieve an Element from a List in the Global Context zdelete("/app/stringList#0"); zdelete("/app/integerList#1"); The first zdelete removes the first element of the string list, and the second zdelete removes the second element of the integer list. Sometimes code requires that a particular list be available before executing. We can test the availability of a list by using the zcontains method, the same way we do for objects. zcontains Method To test if a list exists in the global context, we use the zcontains method: boolean zcontains(String location); The zcontains method returns a boolean value indicating if the particular list is available in the global context (see Listing 5.71). Listing 5.71 zcontain to Test if a List Exists in the Global Context if(zcontains("/app/stringList") { //do something } The zcontains method can be used as a guard code that needs to have particular lists available. Like other list operations, the zcontains method can access particular elements of a list by using the #<key> notation (see Listing 5.72). Listing 5.72 zcontain to Test if an Element in a List Exists in the Global Context if(zcontains("/app/stringList#0") { //do something } This code tests to be sure that the first element in the string list exists before executing some code. The rest of the global context methods (zlist, zlist_all, and zdump) work the same as they do with objects. FirstElementLists FirstElementLists are similar to lists in many cases. The main difference is that with a FirstElementList, the first element is what is operated on by default. The idea is that these Download from www.wowebook.com ptg 114 Chapter 5 Global Context lists operate like stacks. Normally you would be operating only on the head of the list, which would be found at "<zone or locationPrefix>/list" instead of at "<zone or locationPrefix>/list#0" with a normal list. At the time of this writing, we were unable to create FirstElementLists in PHP. What follows is how we believe they will work once this oversight is corrected. zput Method To put a FirstElementList into the global context, we use the zput method: boolean zput(String location, array list); The zput method places the object in the particular global context location (see Listing 5.73). Listing 5.73 zput to Create a FirstElementList in the Global Context $list = array("string"); array_push($list, "some string"); array_push($list, "string object"); zput("/tmp/list#*", $list); With this code, we’ve created a list and then used zput to create or replace the list in the tmp zone so that we can use it at a later time during the course of our application execution. Notice that with FirstElementLists, we must use the #* notation to indicate we’d like to use a FirstElementList. We can replace single elements of our stored list by using #<key> to spec- ify where to put the value. zput("/tmp/list#1", "new string"); This replaces "string object" with "new string" in our stored list. To replace the first item in the list, we don’t need to use the #<key>. We simply need to use the location. zput("/tmp/list", "new first item"); This replaces the first item in the list. If we need to add to a list, we must use the zpost method. zpost Method To append to a FirstElementList in the global context, we use the zpost method: boolean zpost(String location, Object object); The zpost method appends the object to the list stored at the given location (see Listing 5.74). Listing 5.74 zpost to Append to a FirstElementList in the Global Context $list = array(); zpost("/tmp/list#*", $list); zpost("/tmp/list", "posted string object"); Download from www.wowebook.com ptg Accessing the Global Context 115 Here we’ve created an empty list and then posted a single element into the list. This method can also be used to append a list to another list (see Listing 5.75). Listing 5.75 zpost to Append a List to a FirstElementList in the Global Context $list = array(); zput("/tmp/list/#*", $list); zpost("/tmp/list", "posted string object"); array_push($list, "string object"); zpost("/tmp/list#*", $list); In this case, we’ve created an empty list and posted an element to the list stored in the tmp zone. Next, we add some elements to the array and then append them to the stored list using a zpost. zget Method To retrieve variables placed in the global context, we use the zget method: Object zget(String location [, Object defaultValue ]); The zget method retrieves the list in the particular global context location. Optionally, you can provide a default value for the list being retrieved. If a default is not provided and the location is not found, a null is returned (see Listing 5.76). Listing 5.76 zget to Retrieve a FirstElementList in the Global Context $slist = zget("/app/stringList#*", array()); $ilist = zget("/app/integerList#*"); Here we are retrieving our string list with a default empty array and our integer list with no default. We can also retrieve individual elements from our list by again using the #<key> nota- tion (see Listing 5.77). Listing 5.77 zget to Retrieve the First Element from a FirstElementList in the Global Context $s = zget("/app/stringList"); $i = zget("/app/integerList"); This code retrieves the first element of each list. It is possible to retrieve the Nth item in the list as well, using the #<key> notation (see Listing 5.78). Listing 5.78 zget to Retrieve an Element from a FirstElementList in the Global Context $s = zget("/app/stringList#1"); $i = zget("/app/integerList#2"); Download from www.wowebook.com ptg 116 Chapter 5 Global Context This code retrieves the second item in the string list and the third item in the integer list. zdelete Method To remove a list from the global context, we use zdelete: boolean zdelete(String location [, boolean deleteChildren ]); The zdelete method deletes the specified location, removing any values stored there (see Listing 5.79). Listing 5.79 zdelete to Delete a FirstElementList zdelete("/app/stringList#*"); zdelete("/app/integerList#*"); Here we delete both of the lists we’ve previously created and updated. The optional deleteChildren parameter works the same as the object usage of this method. As with zput, we can delete single elements from our list by specifying the #<key> in the location (see Listing 5.80). Listing 5.80 zdelete to Delete the First Element from a FirstElementList zdelete("/app/stringList"); zdelete("/app/integerList#1"); The first zdelete removes the first element of the string list, and the second zdelete removes the second element of the integer list. Sometimes code requires that a particular list be available before executing. We can test the availability of a list by using the zcontains method, the same way we do for objects. zcontains Method To test if a FirstElementList exists in the global context, we use the zcontains method: boolean zcontains(String location); The zcontains method returns a boolean value indicating if the particular list is available in the global context (see Listing 5.81). Listing 5.81 zcontains to Test if the Global Context Contains a FirstElementList if(zcontains("/app/stringList#*") { //do something } Download from www.wowebook.com ptg Accessing the Global Context 117 The zcontains method can be used as a guard code that needs to have particular lists available. With FirstElementLists, we can test the existence of the first by directly accessing the variable (see Listing 5.82). Listing 5.82 zcontains to Test if the Global Context Contains the First Element of a FirstElementList if(zcontains("/app/stringList") { //do something } This code tests to be sure that the first element in the list exists before executing some code. We can test other elements in the list using the #<key> notation (see Listing 5.83). Listing 5.83 zcontains to Test if the Global Context Contains an Element of a FirstElementList if(zcontains("/app/stringList#1") { //do something } This code tests to see if the second element in the list exists. The rest of the global context methods (zlist, zlist_all, and zdump) work as they do with lists and objects. Maps When this book was written, maps could not be created in PHP; they could only be manipulated. However, this oversight should be corrected by the time you are reading this. So, we present here what we believe the API will be. Maps are lists of key/value pairs. To put a single map into the global context, we use the zput method. zput Method To put a map into the global context, we use the zput method: boolean zput(String location, array map); The zput method places the object in the particular global context location (see Listing 5.84). Listing 5.84 zput to Create a Map in the Global Context $map = array(); $map["integer"] = 42; $map["string"] = "string object"; zput("/tmp/map", $map); Download from www.wowebook.com ptg 118 Chapter 5 Global Context With this code, we’ve created a map and then used zput to create or replace the map in the tmp zone so that we can use it at a later time during the course of our application execution. We can replace single elements of our stored map by using #<key> to specify where to put the value. zput("/tmp/map#string", "new string"); This replaces "string object" with "new string" in our stored map. We can also use zput to add to a map. zput("/tmp/map#foo", "added foo"); If we need to merge two maps, we must use the zpost method. zpost Method To append to a map in the global context, we use the zpost method: boolean zpost(String location, array map); The zpost method appends the object to the map stored at the given location (see Listing 5.85). Listing 5.85 zpost to Append to a Map in the Global Context $map = array(); map["foo"] = "foo value"; zpost("/tmp/map", $map); $map = array(); map["bar"] = "bar value"; zpost("/tmp/map", $map); Here we create a map and place a single key/value pair into it. Then we create another new map with another key/value pair. Finally, we use zpost to merge the maps in the tmp zone. zget Method To retrieve variables placed in the global context, we use the zget method: Object zget(String location [, Object defaultValue ]); The zget method retrieves the map in the particular global context location. Optionally, you can provide a default value for the map being retrieved. If a default is not provided and the location is not found, a null is returned (see Listing 5.86). Listing 5.86 zget to Retrieve a Map in the Global Context $smap = zget("/app/stringMap", array()); $imap = zget("/app/integerMap"); Download from www.wowebook.com ptg Accessing the Global Context 119 Here we are retrieving our string map with a default empty HashMap and our integer map with no default. We can also retrieve individual elements from our map by again using the #<key> notation (see Listing 5.87). Listing 5.87 zget to Retrieve an Element from a Map in the Global Context $s = zget("/app/stringMap#foo"); $i = zget("/app/integerMap#one"); This code retrieves the specified value element of each map that corresponds to the given key. zdelete Method To remove a map from the global context, we use zdelete: boolean zdelete(String location [, boolean deleteChildren ]); The zdelete method deletes the specified location, removing any values stored there (see Listing 5.88). Listing 5.88 zdelete to Remove a Map from the Global Context zdelete("/app/stringMap"); zdelete("/app/integerMap"); Here we delete both of the maps we’ve previously created and updated. The optional deleteChildren parameter works the same as the object usage of this method. As with zput, we can delete single elements from our map by specifying the #<key> in the location (see Listing 5.89). Listing 5.89 zdelete to Remove an Element from a Map in the Global Context zdelete("/app/stringMap#foo"); zdelete("/app/integerMap#one"); The first zdelete removes the element with the "foo" key from the string map, and the second zdelete removes the element with the "one" key from the integer map. Sometimes code requires that a particular map be available before executing. We can test the availability of a map by again using the zcontains method, the same way we do for objects. zcontains Method To test is a map exists in the global context, we use the zcontains method: boolean zcontains(String location); The zcontains method returns a boolean value indicating if the particular map is avail- able in the global context (see Listing 5.90). Download from www.wowebook.com ptg 120 Chapter 5 Global Context Listing 5.90 zcontains to Test if a Map Exists in the Global Context if(zcontains("/app/stringMap") { //do something } The zcontains method can be used as a guard code that must have particular maps avail- able. Like other map operations, the zcontains method can access particular elements of a map by using the #<key> notation (see Listing 5.91). Listing 5.91 zcontains to Test if an Element in a Map Exists if(zcontains("/app/stringMap#foo") { //do something } This code tests to be sure that the "foo" element in the map exists before executing code. The rest of the global context methods (zlist, zlist_all, and zdump) function as they do with objects. Conclusion In this chapter, we’ve run the gambit of what the global context is and what we can do with it. We learned about the different persistent and non-persistent zones. We examined the scope of each zone and what each zone should be used for. Finally, we learned how to access the global context in each of the three languages supported by WebSphere sMash. Download from www.wowebook.com ptg 121 Every Conversation Requires a Response There are many ways to respond to requests in WebSphere sMash. Essentially, there are two logi- cal request models in a web application. The first is aimed to serve people in the form of rendered visual content, and the other is focused more on delivering data and other resources. The second form is typically referred to as REST and is covered in Chapter 7, “REST Programming.” For now, we will focus on true visual response rendering and cover the different ways you can per- form a classic interactive web browser experience in WebSphere sMash. In a typical web experience, a user clicks a link in a browser and expects a new page in response. This is the classic browser page-by-page, request/response cycle. In the early days of the Internet, simple static pages were the only possible response type. This worked well but lim- ited the potential of a dynamic web experience. It wasn’t too long before CGI enabled site owners to provide simple dynamic content to their users. The CGI concept evolved into true application servers that were capable of advanced content generation using modern languages and program- ming APIs, such as Java Servlets, .Net, Ruby on Rails, PHP, and many others. The Java Servlet API and its derivatives and .Net, grew in both capability and complexity. It became the preferred enterprise solution for hosting web-based applications. On the other hand, Rails and PHP won the hearts and minds of developers outside of the big corporations but was dismissed as not secure and scalable enough to run real business applications. WebSphere sMash works to the middle of these two worlds by providing the ease of devel- opment offered by the scripting world of PHP and providing the enterprise-ready environment of a Java-based solution. In doing so, we can go from a basic page-by-page static website, all the way up to a full-blown rich Internet application, which can transform the simple browser into a fully capable application platform. C H A P T E R 6 Response Rendering Download from www.wowebook.com . browser experience in WebSphere sMash. In a typical web experience, a user clicks a link in a browser and expects a new page in response. This is the classic browser page-by-page, request/response. page-by-page static website, all the way up to a full-blown rich Internet application, which can transform the simple browser into a fully capable application platform. C H A P T E R 6 Response. by providing the ease of devel- opment offered by the scripting world of PHP and providing the enterprise-ready environment of a Java-based solution. In doing so, we can go from a basic page-by-page

Ngày đăng: 06/07/2014, 19:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan