Using the RT Expression Language

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 4.  Using the Expression Language


Using the RT Expression Language

JSTL also includes support for scriptlet type expressions through the RT expression language. These expressions are bounded between the <%= and %> characters. By using the RT expression language, you can access familiar constructs from scriptlet programming.

JSTL implements support for two scripting languages through the use of twin tag libraries. To access the EL version of the if tag, the tag <c:if> is used. The RT version of the if tag can be accessed by using the <c-rt:if> tag. The four components of JSTL are each duplicated to support the RT expression language. Table 4.3 shows four components that can use RT with JSTL. This is not defined in the JSTL 1.0 spec, but it is in the JSP 1.2 spec.

Table 4.3. The Four Components of RT

Component

URI

Prefix

Core

http://java.sun.com/jstl/core-rt

c-rt

XML Processing

http://java.sun.com/jstl/xml-rt

x-rt

I18N Formatting

http://java.sun.com/jstl/fmt-rt

fmt-rt

Relational DB Access (SQL)

http://java.sun.com/jstl/sql-rt

sql-rt

For example, to take advantage of the core RT tags, you would use the following command:

<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %> 

WARNING

Of course, you must also make sure that your web.xml file has entries for the RT libraries if you want to use them. Appendix B covers this.


The RT expression language allows you to use expressions exactly as you did in JSP scriptlet programming. For example, the following <c-rt:if> tag checks the request method:

<c-rt:if test="<%=request.getRequestMethod().equals("POST" %>"> 

In general, you should always use an EL expression if one is available. However, there are a few cases where EL expressions do not provide the same functionality as RT. Let's examine a few of these cases.

Determining the Browser

Often, it is necessary to determine what browser the user is using. This can be done using some of the RT tags. Listing 4.8 shows a brief example of how to determine the browser.

Listing 4.8 Determining the Browser (browser.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %> <html>   <head>     <title>Check Browser</title>   </head>   <body>     <c-rt:choose>       <c-rt:when test="<%=request.getHeader(\"User-Agent\").   indexOf(\"MSIE\")!=-1%>">       You are using Internet Explorer       </c-rt:when>       <c-rt:otherwise>       You are using Netscape, or some other browser....       </c-rt:otherwise>     </c-rt:choose>   </body> </html> 

As you can see in Figure 4.4, the current browser is identified as Microsoft Internet Explorer. Figure 4.5 shows the same program running with Netscape Communicator.

Figure 4.4. The current browser is Internet Explorer.

graphics/04fig04.jpg

Figure 4.5. The current browser is Netscape Communicator.

graphics/04fig05.jpg

In order to determine the current browser, the page must check the header variables. To do this, a <c-rt:choose> block is set up. Inside <c-rt:when>, test is used to retrieve the User-Agent, which specifies the browser being used. The following lines of code do this:

<c-rt:when test="<%=request.getHeader(\"User-Agent\").indexOf(\"MSIE\")!=-1%>">   You are using Internet Explorer </c-rt:when> 

Case-Insensitive String Comparisons

The EL expression language does not have many commands for the manipulation of strings. When two strings are compared in EL, the comparison is always case sensitive. To do a comparison that is not case sensitive, it is necessary to use EL expressions. Listing 4.9 shows a simple JSTL program that uses RT expressions to do a case-insensitive comparison.

Listing 4.9 A Case-Insensitive String Comparison (if.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %> <html>   <head>     <title>If Caseless</title>   </head>   <body>     <c:set var="str" value="jStL" />     <jsp:useBean  type="java.lang.String" />     <c-rt:if test="<%=str.equalsIgnoreCase(\"JSTL\")%>">       They are equal     </c-rt:if>   </body> </html> 

To do a case-insensitive comparison, we must be able to use the equalsIgnoreCase found in the String class. In scriptlet JSTL, here's an expression that checks to see if the value stored in str is equal to JSTL (ignoring case):

<%=str.equalsIgnoreCase("JSTL")%> 

This expression returns true if the compare matched. This expression can be directly included in a JSTL if statement. This requires an if statement from the RT tag library; the normal <c:if> tag that we've seen earlier will not work. To use an RT expression, you must use the <c-rt:if> tag. The complete if statement is shown here:

<jsp:useBean  type="java.lang.String" /> <c-rt:if test="<%=str.equalsIgnoreCase(\"JSTL\")%>"> </c-rt:if> 

RT scripts do not have direct access to JSTL scoped variables. To access such a variable, you have to map that variable into a bean first. This is the purpose of the <jsp:useBean> tag just before the if statement. The <jsp:useBean> tag shown here will make the scoped variable str available to RT expressions.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch04lev1sec3]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

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