Archive for November, 2006

[ Team LiB ] 5.2 The Header The

Friday, November 17th, 2006

[ Team LiB ] 5.2 The Header The header is the site’s simplest component, as its only job is to display a banner with the title of the page, along with the user’s name as added in Chapter 3. In order to clean it up a little, the header will be modified to show only the user’s name if the user has logged in. The resulting page is shown in Listing 5.1. Listing 5.1 The header <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>


Java News Today:


Hello !

This is about as simple a page as one could hope for. It loads a bean, checks a property with the c:if tag, and displays a value with the c:out tag. The bean is loaded from the session scope because the user information should remain active as long as the user is active on the site. If this bean were in the request scope, the user’s preferences would need to be reloaded or worse, would be lost completely on every new page. If the bean were in the application scope, every user would share the same data, which would not allow each user to have different options. Thus, session scope is definitely the right place for this bean. Note that the test simply checks the value of a property. The test does not need to check whether the ${user.5isLoggedIn == true}isLoggedIn property itself returns true or false directly. [ Team LiB ] Page 92

Hint: This post is supported by Gama web hosting hrvatska services

[ Team LiB ] Page 91

Thursday, November 16th, 2006

[ Team LiB ] Page 91
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

[ Team LiB ] 5.1 The Beans Generally

Thursday, November 16th, 2006

[ Team LiB ] 5.1 The Beans Generally the first step of any large project, data modeling, consists of deciding what data the system will need to maintain, how this data will be represented, and how it interrelates. Traditionally, such modeling takes place in the context of a database, discussed in Chapter 6. For current purposes, however, it is reasonable to model everything in terms of beans. As there is not yet anywhere to store all the data, the examples in this chapter use beans in which the data has been hard-coded, although this is never a good idea in real-world projects. Even when prototyping a system, it is better to use a small, simple database. However, this little cheat will not significantly change the way the pages work, so it will suffice for now. Java News Today has already identified a few beans it will need. In Chapter 3, JNT developed the QuizBean, which holds the question, options, and correct answer for the daily quiz, and created a UserInfoBean to hold users’ preferences for colors, as well as a name. At this time, JNT is ready to consider allowing users to register on the site, in order to store their preferences permanently. This will necessitate adding some logic to the UserInfoBean in order to handle logging users on the system. The fields added will be username, password, and isLoggedIn, a flag that will be true if the user is currently logged in and false otherwise. In addition to users and quizzes, the other major entities behind the JNT site are articles. An ArticleBean will hold the text of the story, a headline, and a date and time of publication. Each ArticleBean will also have a unique numeric identifier to identify and load that story. As with a physical newspaper, articles will be grouped into major sections covering broad categories, such as J2EE, the Java community, related technologies, and so on. Each section will have a name and a description and will also keep track of all the articles it contains. This containment will be managed by creating a SectionBean and giving each instance of SectionBean an array of ArticleBean instances. This should immediately suggest the use of the jsp:forEach tag to display all the stories in a section, and indeed such a page will be on the site. This illustrates how the data-modeling phase of a project can inspire and affect the page-designing phase. Similarly, sections will be grouped into an edition. At the moment, an edition will have only an array of sections; later, its role will be expanded to manage many of the personalization options Java News Today will offer. Finally, in order to make this a community site, the staff at Java News Today would like to allow users to add comments to stories. Some sites, notably http://www.slashdot.org, provide very sophisticated commenting systems that can include threaded discussions, moderation of comments, and a whole host of other features. For the moment, JNT will allow only a simple “flat” commenting system, whereby comments simply appear in the reverse order they were added. This suggests the need for a CommentBean and an array of such beans held by each ArticleBean. Figure 5.1 shows a sample of beans as they might exist in memory and their relationships to one another. Figure 5.1. The JNT beans. Page 90
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

[ Team LiB ] Chapter 5. Java News

Thursday, November 16th, 2006

[ Team LiB ] Chapter 5. Java News Today: Part I Armed with the power of beans, the expression language, and the standard tag library, Java News Today is at last ready to start putting its site together in earnest. In order to do so, JNT will need to decide what functionality the site will offer, design the beans that will represent the entities they will be dealing with, and build pages to provide that functionality. This chapter looks at each of these steps. [ Team LiB ] Page 89
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

Page 88

Thursday, November 16th, 2006

Page 88

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services

[ Team LiB ] 4.11 Tags Learned in

Wednesday, November 15th, 2006

[ Team LiB ] 4.11 Tags Learned in this Chapter c:forEach Repeats a section of the page for every item in an array Parameters: items: An expression specifying the array to use, most likely a bean property var: The name of the variable with which each element in the array will be referred Body:Arbitrary JSP code c:out Displays a value Parameters: value: An expression to be evaluated and displayed Body: Arbitrary JSP code; the body content will be displayed if value is null c:if Conditionally include a portion of the page Parameters: test: An expression that should be a logical test of a property var: If present, names a variable where the result of the expression will be stored Body: Arbitrary JSP code c:choose Includes one of several portions of a page Parameters: None Body: Arbitrary number of c:when tags and, optionally, one c:otherwise tag c:when One possibility for a c:choose tag Parameters: test: An expression that should be a logical test of a property Body: Arbitrary JSP code c:otherwise The catch-all possibility for a c:choose tag. If none of the expressions in the c:when tags evaluates to true, the body of the c:otherwise will be included. Parameters: None Body: Arbitrary JSP code c:set Set a property in a bean Parameters: target: The name of a bean property: The property within the bean to set value: The value to assign; may be a script Body: None fmt:formatNumber Format a number for output Parameters: value: The value to be formatted; may be a script pattern: A pattern specifying how the number should be formatted Body: None fmt:formatDate Format a date and/or time for output Parameters: value: The value to be formatted; may be a script pattern: A pattern specifying how the date should be formatted Body: None [ Team LiB ] Page 87
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services

[ Team LiB ] 4.10 Summary and Conclusions

Wednesday, November 15th, 2006

[ Team LiB ] 4.10 Summary and Conclusions Now we’re cooking! This chapter introduced the concept of custom tags and the standard tag library now part of the JSP specification. These tags give page authors full control over what the user ends up seeing, by providing the means to show arbitrary values, repeat a section of a page as many times as needed, conditionally remove a section of page, or choose from many possible sections. In the next chapter, we’ll see how these tags, together with beans, allow Java News Today to build its site. [ Team LiB ] Page 86
Note: If you are looking for cheap and quality provider to host and run your java application check Astra java hosting services

[ Team LiB ] Page 85

Wednesday, November 15th, 2006

[ Team LiB ] Page 85
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services

[ Team LiB ] 4.9 Selecting among Multiple

Wednesday, November 15th, 2006

[ Team LiB ] 4.9 Selecting among Multiple Choices Once again, the preceding example had to use two c:if tags, although the bodies are different in this case. However, this is still somewhat clumsy, as the same check is being performed twice: once to see whether it is true and once to see whether the reverse is true. This double check is needed because the c:if tag is capable of deciding only between two alternatives: to include its body or not to include it. Another set of tags allows multiway branching, or choosing from among several mutually exclusive possibilities. Unlike the other tags seen so far, three tags work together to obtain the desired result. The outermost tag, c:choose, has no parameters; it merely serves as a container for a collection of two other tags: c:when and c:otherwise. Each individual c:when tag acts a lot like a c:if tag. Both tags take a parameter called test, which should be a script, and render their body content if the condition in the script is true. The difference is that multiple c:if tags will each be checked in turn, whereas a c:choose tag will stop after finding the first c:when tag with a test that is true. In other words, consider a set of possible values for a bean property, such as the colors red, green, and blue. The following snippet of code would check each of these possibilities regardless of the value: The following snippet will check whether the color is red; if so, it will stop and will not then have to check whether it is green and then blue: Clearly, the second option is more efficient. In addition, using the c:choose tag groups related code in one place and so makes JSPs easier to read and understand. The c:choose tag works with another tag: c:otherwise. This tag also has no parameters; its body will be evaluated if none of the c:when tags has a true condition. It is now clear how it would be possible to avoid doing the check twice in Listing 4.11 by using one c:when and a c:otherwise rather than by using two c:if tags. This is shown in Listing 4.13. Listing 4.13 The choose tag There are no tracks! What a boring CD. Here are the tracks:

  • Page 84
    Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services

[ Team LiB ] 4.8 Combining Tags As

Tuesday, November 14th, 2006

[ Team LiB ] 4.8 Combining Tags As mentioned previously, the bodies of JSP tags can contain anything, including other JSP tags. An example is the c:out tag within the c:forEach tag in Listing 4.8. To demonstrate this further, the c:if and c:forEach tags work together in the following example. If given an empty array, a c:forEach tag will not render its body content at all. This is fine but can lead to some odd-looking pages. In Listing 4.8, if the CD is empty, the page will display “Here are the tracks” and then stop. This is technically correct but to the user may look as though the page stopped generating halfway through. It would be better to inform the user that the CD is empty rather than to display a list with no elements. This can be accomplished by putting the c:forEach tag inside a c:if tag, as shown in Listing 4.12. Listing 4.12 Tags working together <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

Artist:

Year:

There are no tracks! What a boring CD. Here are the tracks:

Conceptually, the only new thing about this example is the check that is done in the c:if tag. The empty in the test checks whether the named property exists,[3] and if it does exist and is an array, whether it has any elements. The exclamation point in the test should be read as “not.” It means that if the following test would be true, it returns false, and vice versa. [3] Technically, it tests whether the value equals null, as will be discussed in Chapter 9. [ Team LiB ] Page 83

Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider