| [ Team LiB ] |
|
16.3 Using ScriptletsThe scriptlet element can be used to add a whole block of code to a page, including variable declarations. The code block must be enclosed by a scriptlet start-identifier, <%, and an end-identifier, %>. Example 16-1 shows a scriptlet that creates test data for action elements. Example 16-1. Scriptlet creating test data (scriptlet.jsp)<%@ page language="java" contentType="text/html" %> <%@ page import="java.util.*" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <% // Create an ArrayList with test data ArrayList list = new ArrayList( ); Map author1 = new HashMap( ); author1.put("name", "John Irving"); author1.put("id", new Integer(1)); list.add(author1); Map author2 = new HashMap( ); author2.put("name", "William Gibson"); author2.put("id", new Integer(2)); list.add(author2); Map author3 = new HashMap( ); author3.put("name", "Douglas Adams"); author3.put("id", new Integer(3)); list.add(author3); pageContext.setAttribute("authors", list); %> <html> <head> <title>Search result: Authors</title> </head> <body bgcolor="white"> Here are all authors matching your search critera: <table> <th>Name</th> <th>Id</th> <c:forEach items="${authors}" var="current"> <tr> <td>${fn:escapeXml(current.name)}<td> <td>${fn:escapeXml(current.id)}<td> </tr> </c:forEach> </table> </body> </html> The scriptlet element contains Java code that creates a java.util.ArrayList with java.util.HashMap elements and saves the list as a page scope attribute named authors by calling the setAttribute( ) method on the implicit pageScope object. The ArrayList is then used as the items attribute value in a <c:forEach> action. You can use a scriptlet like this to test the main page functionality before the real data source is available. In the final version, the scriptlet can be removed, and the data passed to the page from another page or a servlet. Let's look at another example, in which the implicit request object is inquired about the current client type to display different messages depending on whether the Internet Explorer or Netscape Navigator browser is used. Example 16-2 shows the complete page. Example 16-2. Browser dependent page (fragment.jsp)<%@ page language="java" contentType="text/html" %>
<html>
<head>
<title>Browser Check</title>
</head>
<body bgcolor="white">
<%
String userAgent = request.getHeader("User-Agent");
if (userAgent.indexOf("MSIE") != -1) {
%>
You're using Internet Explorer.
<% } else if (userAgent.indexOf("Mozilla") != -1) { %>
You're probably using Netscape.
<% } else { %>
You're using a browser I don't know about.
<% } %>
</body>
</html>
The first scriptlet uses the getHeader( ) method of the request object to get the value of the User-Agent header. This header contains a string with clues about the browser making the request. The header value is then used in a number of if statements to make an educated guess about the browser type and tell the user the result. What's most interesting here is that a number of scriptlets are used, each one containing only a fragment of a Java statement:
While none of the scriptlets by itself is a valid Java statement, the JSP container combines the fragments in the four scriptlets with code for writing the template text to the response body to form a valid statement. The end result is that when the first if statement is true, "You're using Internet Explorer" is displayed; when the second if statement is true, "You're probably using Netscape" is displayed. If none of the if statements are true, the final else block is used, displaying "You're using a browser I don't know about." The tricky part when using scriptlets like this is making sure that all the start and end braces are in place. If you miss just one of the braces, the code that the JSP container generates isn't syntactically correct. And, unfortunately, the error message that you get isn't easy to interpret. |
| [ Team LiB ] |
|