Section 14.2. Scripting Elements

   

14.2 Scripting Elements

There are three types of scripting elements: scriptlets, declarations, and expressions.

14.2.1 Scriptlets

Scriptlets allow little Java code blocks to be written directly in your JSP. You can perhaps remember the definition of scriptlets because they are small Java code scripts (the way applets are small applications and servlets are small servers). A scriptlet takes another syntax, one that looks more like ASP:

 <% SomeClass s = new SomeClass(param); %> 

There are certain things to notice about scriptlets. The code you write inside a scriptlet is pure Java code. You can instantiate objects, write methods , and you end your statements in a semicolon. Scriptlets cannot span multiple pages; that is, they must start and end on the same JSP. Scriptlets have page-level scope ”that means that a variable declared in one scriptlet is still visible later in the page. It also means that the same variable is not available across multiple requests . In this regard, its variables act like ColdFusion local variables (those in the VARIABLES scope).

A difficult aspect of scriptlets to keep in mind is that you cannot write HTML directly into them. So if you are performing a loop, for instance, and want to print out something to the browser, you must close the scriptlet but not the loop, do your business in HTML, and open a new scriptlet to close the loop!

 <%     for (int i = 0; i < 5; i++) { %> <h1>here is some text</h1> <%    } %> 

It looks odd at first, perhaps, but it makes perfect sense.

Note

There is an XML syntax for using scriptlets: <jsp:scriptlet> </jsp:scriptlet> . This is not widely supported, however. ColdFusion MX and Tomcat 4, for example, will not honor the XML syntax for scriptlets.


While it is very easy to start writing a good deal of scriptlet code into your JSPs, this practice is strongly discouraged. It severely detriments the readability and maintainability of your page. It makes it difficult for content or HTML developers to work with, and endangers your code. That Java code should instead be offloaded into beans (which are just a special kind of Java class we'll look at later in this chapter) or into other Java classes or custom actions. Doing so promotes strong object-oriented architecture, just as you might imagine that it is now preferable to have ColdFusion Components doing the real work of the page instead of interspersing small bits of HTML and CFML on into perpetuity. Scriptlet use should be kept to a minimum.

Nonetheless, scriptlets are useful, and they have their place, which we'll see in examples in the remainder of the book.

14.2.2 Declarations

Declarations are equivalent to the member variables and functions of a regular Java class. You write a declaration like this:

 <%! %> 

For example, in order to declare a String variable to reference later in the JSP, you can write this:

 <%! String animal = "kitty" %> 

This will create the String variable.

Note

It is rare to see declarations in practice, probably because the use of scriptlets has been so strongly discouraged and because they offer only a small, specific subset of what you can do in a scriptlet. They aren't necessary, and they can be confusing.


Of course, declarations cannot reference any variable that has already been declared.

14.2.3 Expressions

Expressions in ColdFusion and in Java are any legal statement in the language that evaluates to a single valid value of any data type. In JSP, an expression is more limited. The expression is still a statement that evaluates to a single value, but the data type of the expression result must be a string. The result of the expression is sent to the output stream using JspWriter out.println() . It looks like this:

 <%= aVariable %> 

This expression is the equivalent of this statement:

 ...  out.println(aVariable); ... 

This kind of code is fairly common. Pretend we have a database containing news articles that we want to display in the browser. Each news article has an author , a createdDate , and a title . In this pseudo-example, we take a parameter passed via a form or URL called typeID (remember that JSP, unlike CF, is case-sensitive; it inherits this characteristic from its mother). Then we call on the connect() method of a Java object called item to connect to a database. Then, we call the item object's viewArchive() method, passing it the typeID string variable. This method then hits the database, returning a result set for us to loop over. We write a while loop to iterate over the result set, assigning page-local variables the value of the columns retrieved from the database.

 ...  <%    // get result and set vars     String typeID = request.getParameter("typeID");     item.connect();     ResultSet rs = item.viewArchive(typeID);       while (rs.next()) {            String itemID = rs.getString("itemID");            String itemName = rs.getString("itemName");            String createdDate = rs.getString("createdDate");            String createdBy = rs.getString("createdBy");   %> 

Later in this same page, we can output the values of these new variables, incorporated with lovely HTML.

 ...  <tr> <td align="left"> <a href="some.jsp?itemID=<%= itemID %>" class="title1"> <%= itemName %></a> <br> <span class="greytext">date: <%= createdDate %></span><br> <span class="greytext">author: <%= createdBy %></span><br> </td> </tr> <% // close the while loop } %> ... 

Notice how the parameters used in builing a dynamic URL are string expressions, just as you might write this in ColdFusion:

 <a href = "some.cfm?itemID=<cfoutput>#queryname.itemID#  </cfoutput>"... 

   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net