Servlets hosting - JavaSercer Pages 5.1.3 Using JavaBeans There is also

November 30, 2006 on 1:04 pm | In Java |

JavaSercer Pages 5.1.3 Using JavaBeans There is also some dynamic content in this example. Step back a moment and think about the type of dynamic content you see on the Web every day. Common examples might be a list of web sites matching a search criteria on a search engine site, the content of a shopping cart on an e-commerce site, a personalized news page, or messages on a bulletin board. Dynamic content is content generated by some server process, for instance the result of a database query. Before it is sent to the browser, the dynamic content needs to be combined with regular HTML elements into a page with the right layout, navigation bars, the company logo, and so forth. In a JSP page, the regular HTML is the template text described earlier. The result of the server processing - the dynamic content - is commonly represented by a JavaBeans component. A JavaBeans component, or just a bean for short, is a Java class that follows certain coding conventions, so it can be used by tools as a component in a larger application. In this chapter, we discuss only how to use a bean, not how to develop one. (If you’re a programmer and not already familiar with JavaBeans, you may want to skip ahead to Chapter 15, to learn about these coding conventions.) A bean is often used in JSP as the container for the dynamic content to be displayed by a web page. Typically, a bean represents something specific, such as a person, a product, or a shopping order. A bean is always created by a server process and given to the JSP page. The page then uses JSP elements to insert the bean’s data into the HTML template text. The type of element used to access a bean in a page is called a JSP action element. JSP action elements are executed when a JSP page is requested (this is called the request processing phase, as you may recall from Chapter 3). In other words, JSP actions represent dynamic actions that take place at runtime, as opposed to JSP directives, which are used only during the translation phase (when the JSP page is turned into Java servlet code). JSP defines a number of standard actions and also specifies how you can develop custom actions. For both standard and custom action elements, use the following notation: action_body Action elements, or tags as they are sometimes called,4 are grouped into libraries (known as tag libraries). The action name is composed of two parts: a library prefix and the name of the action within the library, separated by a colon (i.e., jsp:useBean). All actions in the JSP standard library use the prefix jsp, while custom actions can use any prefix except jsp, jspx, java, javax, servlet, sun, or sunw. You specify input to the action through attribute/value pairs in the opening tag. The attribute names are case-sensitive, and the values must be enclosed in single or double quotes. For some actions, you can also enter data that the action should process in the action’s body. It can be any text value, such as a SQL statement, or even other nested JSP action elements. You will see examples of action elements with a body later. Before you use a bean in a page, you must tell the JSP container which type of bean it is and associate it with a name. The first JSP action in Example 5.1, , is used for this purpose: The id attribute is used to give the bean a unique name. It must be a name that is a valid Java variable name: it must start with a letter and cannot contain special characters such as dots, plus signs, etc. The class attribute contains the fully qualified name of the bean’s Java class. Here, the name clock is associated with an instance of the class java.util.Date. Note that we don’t specify a body for this action. When you omit the body, you must end the opening tag with />, as in this example. In this case, when the JSP container encounters this directive, there is no bean currently available with the name clock, so the action creates a bean as an instance of the specified class and makes it available to other actions in the same page. In Chapter 8, you will see how can also be used to locate a bean that has already been created. Incidentally, the action supports three additional attributes: scope, type, and beanName. The scope attribute is described in detail in Chapter 8, and the other two attributes are covered in Appendix A. We don’t need to worry about those attributes here. 4 An element is actually represented by a start tag and an end tag, but the term “tag” is often used to refer to what’s formally known as an element. page 45

Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Virtualwebstudio jsp web hosting provider

No Comments yet

TrackBack URI

Sorry, the comment form is closed at this time.