Home arrow Java & J2EE arrow Page 6 - The JSP Files (part 1): Purple Pigs In A Fruitbasket

Basket Case - Java

Get to grips with Java Server Pages with this introductorytutorial and find out how to use one of the more powerful server-sidelanguages around. This first part explains the history and basics of JSPdocuments, and also illustrates variables, includes and the String object.

TABLE OF CONTENTS:
  1. The JSP Files (part 1): Purple Pigs In A Fruitbasket
  2. Studying The Foundations
  3. Java In A Teacup
  4. Enter John Doe
  5. Putting Two And Two Together
  6. Basket Case
  7. Alphabet Soup For The Soul
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 9
February 07, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The String object comes with a bunch of useful methods, which can come in handy when performing string manipulation.

The first of these is the length() method, used to obtain the (you guessed it!) length of a specific string. Let's modify the example you just saw to demonstrate how this works:


<html> <head> </head> <body> <%! // define the variables String apples = "Purple pigs "; String oranges = "riding orange pumpkins"; String fruitBasket; %> <% // print the first two strings out.println("<b>The first string is</b>: " + apples + "<br>"); out.println("<b>The second string is</b>: " + oranges + "<br>"); // concatentate the strings fruitBasket = apples + oranges; // display out.println("<b>And the combination is</b>: " + fruitBasket + "(" + fruitBasket.length() + " characters)<br>Who says you can't add apples and oranges?!"); %> </body> </html>

And the output is:

The first string is: Purple pigs The second string is: riding orange pumpkins And the combination is: Purple pigs riding orange pumpkins(34 characters) Who says you can't add apples and oranges?!

You can extract a specific character from the string with the charAt() method, which accepts an offset as parameter. For example, the following code snippet would return the character "o":

<% String name = "Bozo The Clown"; out.println(name.charAt(3)); %>

Note that the offset 0 indicates the first character, since Java, like many of its counterparts, uses zero-based indexing.

You can also extract a segment of a string with the substring() method, which allows you to specify the start and end points of the string segment to be extracted. Take a look at this sentence and see if you can spot the hidden message within it:

<%! String me = "I am a highly-skilled and hardworking developer!"; %>

No? How about now?

<%! String me = "I am a highly-skilled and hardworking developer!"; String message; %> <% message = me.substring(0,2) + me.substring(15,22) + me.substring(26,27) + me.substring(45,48); out.println(message); %>

And here's the output:

I killed her!


 
 
>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: