Oracle JSP - Developers Guide Release 1.0 |
JSPs define two levels of tags. The basic JSP tags (those that are prefixed with <%) are sufficient for writing any JSP page. However, to encourage a greater reliance on Beans which are responsible for encapsulating a page's business logic, the JSP architects defined additional tags (those that are prefixed <jsp: ) to simplify declaring and initializing Beans.
Note: The <jsp: > tag set was further expanded to simplify other common page operations such as including or forwarding a request.
However, the JSP architects did not define a (standard) bean set to provide common web application function for JSP developers. Oracle JSP begins this process by defining four classes to overcome the most significant omission: the inability to declare data variables at scope. In future releases, Oracle JSP plans to provide a more well rounded set of beans, including data access beans, to simplify the JSP development process. In the meantime, the JSP author should look at the web developers libraries included in JDeveloper 3.0.
This chapter introduces the Oracle JSP data types.
Samples that illustrate the concepts discussed in this chapter are provided in the chapter 5 samples section included in the download.
JSPs rely on the core Java data types for representing scalar values. Java provides two forms: primitive (language) data types such as byte, char, int, and double; and wrapper classes such as java.lang.Byte, java.lang.Character, java.lang.Integer, and java.lang.Double. Neither of these forms are a good fit with the <jsp:xxx > tags. And even if you are not using the <jsp:xxx > tags, they are not appropriate for when variables are not local. That is when a variable is declared at a JSP scope.
The problem with using the Java primitive types are:
The problem with using the Java wrapper classes (java.lang.*) are:
pageName = pageName + ".jsp";
However, within a JSP page, the variable pageName is local even if declared at scope. To update pageName correctly when it is stored at a JSP scope you must set the following:
pageName = pageName + ".jsp";
session.setAttribute("pageName", pageName);
It is a common mistake to forget this second step.
Oracle JSP provides four bean classes to wrap the most common Java data types.
Each class provides a Bean interface to a Java data type. Each bean has a single property, value, that holds the data variable. Each provides a getValue() and setValue() method for accessing the current value. Additional methods, getTypedValue() and setTypedValue(), are provided for situations when you need to store or retrieve the bean's value in a different form.
Note: Oracle JSP provides just four classes rather than wrappers for all the Java data types because these beans are designed to complement Oracle's JML extension tags. JML includes and extends the <jsp: > tags providing a complete tag library for developing JSP pages without writing Java (using the <% tags). A reduced set of data types is desirable when targeting the application assembler versus the proficient Java programmer.
You can create any of the four Oracle JSP data types via the <jsp:useBean> tag. The following example declares a application scoped variable count whose initial value is 0.
<jsp:useBean id="count" class="oracle.jsp.jml.Number" scope="application" />
Once declared, the bean's value can be manipulated using <jsp:setProperty> or <jsp:getProperty>. In this first example, the variable maxSize if declared and initialized to 25.
<jsp:useBean id="maxSize" class="oracle.jsp.jml.Number" scope="request" >
<jsp:setProperty name="maxSize" property="value" value="<%= 25 %>"
</jsp:useBean>
In the second example, the value of count is used to display this page's hit count:
<h3> This page has been visited <jsp:getProperty name="count" property="value" /> times. </h3>
or alternatively
<h3> This page has been visited <%= count.getValue() %> times. </h3>
It can also be directly manipulated in JSP scriptlets without reattaching at scope:
<%
// since count is scoped to the application we must synchronize
synchronize(count) {
count.setValue(count.getValue() + 1);
}
// more scriptlet code ..
%>
|
|