With beans and Maps, you can use the dot operator, but only if the thing you type after the dot is a legal Java identifer.
This
${musicMap.Ambient}
Is the same as this1
${musicMap["Ambient"]}
But this
${musicList["1"]}
CANNOT be turned into this
${musicList.1}
Sharpen your pencil
What prints?
Given the servlet code below, figure out what would print (or if there’d be an error, just write, you know, “error”). Answers are at the bottom of the next page.
java.util.ArrayList nums = new java.util.ArrayList(); nums.add("1"); nums.add("2"); nums.add("3"); request.setAttribute("numbers", nums); String[] favoriteMusic = {"Zero 7", "Tahiti 80", "BT", "Frou Frou"}; request.setAttribute("musicList", favoriteMusic);
Code Magnets
Don’t be surprised if you find something like this on the exam (except in the real exam it’ll look... uglier).
Study the three classes on the page, and the servlet code on the opposite page, then construct the code magnets to make the EL that’ll produce the response shown in the browser. (Turn the page for the answers, but not until you DO THIS, especially if you’re going to take the exam.)
package foo; public class Toy { private String name; public void setName(String name) { this.name=name; } public String getName() { return name; } }
package foo; public class Person { private Dog dog; private String name; public void setDog(Dog dog) { this.dog=dog; } public Dog getDog() { return dog; } public void setName(String name) { this.name=name; } public String getName() { return name; } }
package foo; public class Dog { private String name; private Toy[ ] toys; public void setName(String name) { this.name=name; } public String getName() { return name; } public void setToys(Toy[] toys) { this.toys=toys; } public Toy[ ] getToys() { return toys; } }
Answers to Sharpen on previous page: 1) Tahiti 80 2) BT 3) Frou Frou 4) Frou Frou
Servlet code
foo.Person p = new foo.Person(); p.setName("Leelu"); foo.Dog d = new foo.Dog(); d.setName("Clyde"); foo.Toy t1 = new foo.Toy(); t1.setName("stick"); foo.Toy t2 = new foo.Toy(); t2.setName("neighbor's cat"); foo.Toy t3 = new foo.Toy(); t3.setName("Barbie™ doll head"); d.setToys(new foo.Toy[]{t1, t2, t3}); p.setDog(d); request.setAttribute("person", p);
Compose the EL for this output:
Code Magnets Answers
This is not the ONLY way to produce the output, but it’s the only way using this set of magnets. Bonus exercise: write the EL expressions a little differently (forget the magnets), but print the same result.
Servlet code
foo.Person p = new foo.Employee(); p.setName("Leelu"); foo.Dog d = new foo.Dog(); d.setName("Clyde"); foo.Toy t1 = new foo.Toy(); t1.setName("stick"); foo.Toy t2 = new foo.Toy(); t2.setName("neighbor's cat"); foo.Toy t3 = new foo.Toy(); t3.setName("Barbie™ doll head"); d.setToys(new foo.Toy[]{t1, t2, t3}); p.setDog(d); request.setAttribute("person", p);
Compose the EL for this output:
${person.name}'s dog ${person.dog.name}'s toys are: ${person.dog.toys[0]. name}, ${person.dog.toys[1].name}, and a ${person.dog.toys[2].name}
Five Minute Mystery
The Case of the Missing Content
Documents-R-Us has created a content management system used primarily for creating tutorials for desktop applications. Part of the application allows content developers to create “Tip of the Day” chunks of content, which are stored in the request-scoped attribute currentTip
. For example, if the tip was “Wash your hair every other day,” then the screen would include a box like this:
The JSP code for this tip box is:
<div class='tipBox'> <b>Tip of the Day:</b> <br /> <br /> ${currentTip} </div>
A new client is trying to create a tutorial using the system, but can’t seem to get the tips to display correctly. For example, the tip “<b></b> tags make things bold!” is rendered like this:
“What gives?” exclaims Tawny, the client’s lead JSP developer. “Where did the beginning of the tip go? Why didn’t the bold tags get displayed?” She issues a bug report immediately to Documents-R-Us.
What do you think? Did the bold tags get sent to the output stream? Why aren’t they being displayed?