14.3 Request Information

Java Servlet Programming, 2nd Edition > 14. The Tea Framework > 14.3 Request Information

 
< BACKCONTINUE >

14.3 Request Information

That's a lot of work to create static content, so let's create something more dynamic. The template constructor can accept any number of parameters, whose values are passed in automatically from the request's parameters (through the query string and POST data). The parameters can be of type String, Integer, Float, Double, and any other java.lang.Number subclass. The conversion to numeric types happens automatically. If no parameter by a given name is sent, or the type conversion fails, the template receives a null parameter value. The template shown in Example 14-2 uses its constructor to accept a parameter called name:

<% template SimplePage(String name) %> This is a simple page that does nothing special except show a name: <% name %>

If you access the template as /teatime/tea/SimplePage?name=Waldo, it will show "Waldo" in the page. If the template is accessed without a name parameter, it prints "null" in the page. Example 14-2 shows a template that checks for this case.

Example 14-2. A Template to Check Names
<% template SimplePage(String name) %> This is a simple page that does nothing special except show a name: <% name %> <P> <% if (name == null) {   "You gave a null name" } if (name == "Slim Shady") {   "Please stand up" } %>

This new template checks if the name was null and if so prints a warning to the user. The template also checks if the name was Slim Shady and if so gives the user special treatment. It's important to notice two things from the Slim Shady comparison: first, string values in Tea can be equivalence-checked with the == operator (as can all objects). This means there's no need to explain to a technical producer the purpose of the .equals( ) method. Second, a null string can be compared to another string without risk of a NullPointerException , something that could happen with .equals().

14.3.1 Digging Deeper

Other request information is available with TeaServlet. For example, a Tea template can access request headers and cookies, as well as parameters directly. Example 14-3 shows a Tea template that snoops request information. In practical use, it's fairly rare that a Tea template would need to access this information directly, choosing instead to let the application provide a processed version of the information to be displayed.

Example 14-3. A Template to Snoop Request Information
<% template Snoop() %> <HTML><HEAD><TITLE>Let's Snoop!</TITLE></HEAD><BODY> <% request = getRequest()  // Object type is implied %> <H1>Miscellaneous Info</H1> QueryString: <% request.queryString %> <BR> RemoteUser: <% request.remoteUser %> <BR> <H1>Parameter Info</H1> <% foreach (paramName in request.parameters.names) {   paramName ": " request.parameters[paramName] "<BR>" } %> <H1>Request Attribute Info</H1> <% foreach (attribName in request.attributes.names) {   attribName ': ' request.attributes[attribName] '<BR>' } %> <H1>Header Info</H1> <% foreach (headerName in request.headers.names) {   headerName ': ' request.headers[headerName] '<BR>' } %> <h1>Cookie Info</h1> <% foreach (cookie in request.cookies.all) {   cookie.name ': ' cookie.value '<BR>' } %> The session id cookie value is <% request.cookies["JSESSIONID"].value %>. <% // Some things we can't do in Tea... %> <%/* "<H1>Read from the User's Session</H1>" request.session["may"] = "work later" "<H1>Set an Attribute</H1>" request.attributes["will"] = "not work" "<H1>Set a Cookie</H1>" // Response manipulations are through application functions */%> </BODY></HTML>

We can learn quite a lot about Tea from this simple example. First, local variables (such as the request) don't need to declared against an object type. They are strongly typed, but the type assignment is implicit. There's an isa keyword in the language to handle casting duties if necessary.

We use a foreach statement that handles the frequent task of looping over a collection of objects. It's a flexible device, able to iterate over arrays as well as any object that implements java.util.Collection. The foreach statement also supports iterating over a range and iterating in reverse. For example:

foreach (count in 1 .. 10 reverse) { count '<BR>' }

prints a countdown starting with 10. The foreach statement handles all looping duties in Tea; there is no for or while statement. (Thus there are also no infinite loops in Tea.)

Tea statements need not terminate with a semicolon as they do in Java, nor are statements separated by new lines. Instead, the Tea compiler uses the carefully designed Tea grammar to calculate the end of statements! Semicolons are still supported for the rare circumstance where a semicolon is required to disambiguate statement separation and as a convenience for Java programmers who instinctively add semicolons.

Much of the time there's no need for a string concatenation operator in Tea. The presence of a "double-quoted" or `single-quoted' string within a Tea code block usually provides enough of a hint that there should be a string concatenation. For more explicit concatenation, use the & character. The + character isn't used because overloading the + operator causes typing ambiguities.

Comments in Tea follow the Java standard where /* and */ denote multiline comments and // begins a single-line comment. These comments aren't sent to the client, so long as you remember to place them within a Tea code block.

We used a multiline comment to comment out the final code block demonstrating things that can't be done: reading from a user's session, setting a request attribute, setting a cookie, and in fact setting anything on the request. The ability to access the session may be added later as part of the open source effort behind Tea; it's currently not supported only because WDIG internally uses another user-session-tracking mechanism. The other abilities are intentionally unsupported and would need to be done by Java code in a supporting application, due to the design constraint that "Acquired data cannot be directly modified in any way."


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

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