Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

You Say Seven, I Say 7

And finally, in case you've ever wanted to convert strings to numbers, the following example should tell you everything you need to know.

<%              
// converting a string to a number              
             
// define variables              
String someString = "97";              
int aRandomNumber = 3;              
             
// at this stage, someString is still treated as a string              
out.println(someString + " plus " + aRandomNumber              
+ " equals " + (someString+aRandomNumber) + "<p>");              
             
// now convert someString to a number              
int someNumber              
= Integer.parseInt(someString);              
             
// at this stage, someString has been converted to a              
number, stored in someNumber out.println(someNumber +              
" plus " + aRandomNumber + " equals " + (someNumber+aRandomNumber));              
             
%>

And here's the output.

97 plus 3 equals 973              
97 plus 3 equals 100

If you'd prefer to do things the other way around, the next example bears careful consideration.

<%              
// define variables              
int someNumber = 97;              
int aRandomNumber = 3;              
             
// at this stage, someNumber is still treated as a number              
out.println(someNumber + " plus " + aRandomNumber + "              
equals " + (someNumber+aRandomNumber) + "<p>");              
             
// now convert someNumber to a string              
String someString = Integer.toString(someNumber);              
             
// at this stage, someNumber has been converted to a              
string, stored in someString out.println(someString +              
" plus " + aRandomNumber + " equals " + (someString+aRandomNumber));              
             
%>

And here's the output.

97 plus 3 equals 100              
97 plus 3 equals 973

The upshot? parseInt() and toString() are your best friends when converting between string and numeric data types in JSP.

A Positive Response

Another interesting object, and one that comes in handy in the oddest places, is the Response object. As opposed to the Request object, which is used to retrieve information (come back next time for more on this), the Response object is typically used to send information to the browser; this information could include HTTP headers, redirection URLs, cookies and a variety of other items.

Since the Response object is an "implicit" object (so called because you do not need to explicitly create an instance of the object when you want to use it), you can begin using it immediately in a JSP document. The following example demonstrates using it to turn off caching via the "Cache-Control" header.

<%              
response.setHeader("Cache-Control","no-cache");              
%>              
[/code]              
You can also use it to redirect the client to another URL.              
[code]              
%              
response.setHeader("Location", "error.html");              
%>

You can use the sendRedirect() method to accomplish something similar - remember to use an absolute URL here.

<%              
response.sendRedirect("http://my.server.com/error.html");              
%>

You can set the MIME type of a document with the setContentType() method.

<%              
response.setContentType("image/jpeg");              
%>

and even set a cookie with the addCookie() method - more on this as we progress through this series.

That's about all we have time for. Next time, we'll be exploring the JSP Response object, used to process data from HTML forms... so you don't want to miss that one!

Note: All examples in this article have been tested on Linux/i586 with Tomcat 3.2 and JServ 1.1. Examples are illustrative only, and are not meant for a production environment. YMMV!
Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article