Download presentation
Presentation is loading. Please wait.
Published byMarybeth Bryant Modified over 9 years ago
1
COMP 321 Week 10
2
Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review
3
Accessing Attributes protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("userName"); request.setAttribute("name", name); RequestDispatcher view = request.getRequestDispatcher("/result.jsp"); view.forward(request, response); } Hello
4
Accessing Attributes // What if the attribute is an object? foo.Person p = new foo.Person(); p.setName("Evan"); request.setAttribute("person", p); RequestDispatcher view = request.getRequestDispatcher("/result.jsp"); view.forward(request, response); Person is:
5
Accessing Attributes // What if the attribute is an object? foo.Person p = new foo.Person(); p.setName("Evan"); request.setAttribute("person", p); RequestDispatcher view = request.getRequestDispatcher("/result.jsp"); view.forward(request, response); Person is:
6
Accessing Attributes Person is: Person is:
7
Accessing Attributes // What if we want to use polymorphism? public abstract class Person { public void setName(String name) {... } public String getName() {... } } public class Employee extends Person { public void setEmpID(int id) {... } public int getEmpID() {... } }
8
Accessing Attributes // What if we want to use polymorphism? public abstract class Person {... } public class Employee extends Person {... } // Generated code foo.Person person = null; // Code to get person attribute if (person == null) { person = new foo.Employee();... }
9
HTML -> JSP name: ID#:
10
HTML -> JSP name: ID#:
11
EL Expressions Follow the format ${First.Second} or ${First[Second]} First is either an implicit object or an attribute Second is a key, member, index, or expression
12
EL Expressions - Implicit Objects Maps pageScope, requestScope, sessionScope, applicationScope param, paramValues header, headerValues cookie initParam pageContext – the page context object
13
EL Expressions ${person.name} ${person["name"]} String [] myMusic = { "Zero 7", "Tahiti 80", "BT" }; request.setAttribute("musicList", myMusic); ${musicList[0]} ${musicList["1"]}
14
EL Expressions Map musicMap = new HashMap(); musicMap.put("Ambient", "Zero 7"); request.setAttribute("musicMap", musicMap); ${musicMap.Ambient} ${musicMap["Ambient"]} ${musicMap[Ambient]} request.setAttribute("Genre", "Ambient"); ${musicList[Genre]}
15
EL Expressions ${param.userName} ${paramValues.food[0]} ${paramValues.food[1]} ${header.host} ${pageContext.request.method}
16
EL Expressions ${requestScope.person.name} ${requestScope["foo.person"].name} ${cookie.userName.value} ${initParam.mainEmail}
17
EL Functions Can be used to call static methods in Java classes: 1.Write a Java class with a static method 2.Create a Tag Library Descriptor (TLD) file 3.Add a taglib directive to your JSP 4.Use EL to call the function
18
EL Functions class DiceRoller { public static int rollDice() { return (int)((Math.random() * 6) + 1); } DiceFunctions rollIt foo.DiceRoller int rollDice() ${mine:rollit()}
19
JSP Include Directive We know how to make SOAP suck less. We can help. Contact us at: ${initParam.mainEmail} Equivalent to pasting the header into Contact.jsp Position-dependent Takes place once at translation time
20
JSP Include Action ${param.subTitle} <jsp:include page="Header.jsp" We can help. Contact us at: ${initParam.mainEmail} Request and response are forwarded to included JSP Takes place at execution time Happens once per request jsp:param can be used to pass parameters
21
Code Magnets public class Toy { private String name; // Getters/setters } public class Person { private Dog dog; private String name; // Getters/setters } public class Dog { private String name; private Toy[] toys; // Getters/setters }
22
Code Magnets Person p = new Person("Leelu"); Dog d = new Dog("Clyde"); Toy t1 = new Toy("stick"); Toy t2 = new Toy("neighbor's cat"); Toy t3 = new Toy("Barbie doll head"); d.setToys(new Toy[]{t1, t2, t3}); p.setDog(d); request.setAttribute("person", p); Desired output: Leelu's dog Clyde's toys are: stick, neighbor's cat, and a Barbie doll head
23
Code Magnets Person p = new Person("Leelu"); Dog d = new Dog("Clyde"); Toy t1 = new Toy("stick"); Toy t2 = new Toy("neighbor's cat"); Toy t3 = new Toy("Barbie doll head"); d.setToys(new Toy[]{t1, t2, t3}); p.setDog(d); request.setAttribute("person", p); Desired output: Leelu's dog Clyde's toys are: stick, neighbor's cat, and a Barbie doll head EL: ${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}
24
Sharpen Your Pencil request.setAttribute("num", "2"); request.setAttribute("integer", new Integer(3)); ArrayList list = new ArrayList(); list.add("true"); list.add("false"); list.add("2"); list.add("10"); request.setAttribute("list", list); What prints for each of these? ${num > 3} ${integer le 12} ${requestScope["integer"] ne 4 and 6 le num || false} ${list[0] || list["1"] and true} ${num > integer} ${num == integer - 1} ${42 div 0}
25
Sharpen Your Pencil request.setAttribute("num", "2"); request.setAttribute("integer", new Integer(3)); ArrayList list = new ArrayList(); list.add("true"); list.add("false"); list.add("2"); list.add("10"); request.setAttribute("list", list); What prints for each of these? ${num > 3} false ${integer le 12} true ${requestScope["integer"] ne 4 and 6 le num || false} false ${list[0] || list["1"] and true} true ${num > integer} false ${num == integer - 1} true ${42 div 0} Infinity
26
JSTL Tip of the day: ${pageContent.currentTip} tags make things bold", we have a problem --> Tip of the day: tags make things bold Tip of the day: <b></b> tags make things bold
27
JSTL Tip of the day: <b></b> tags make things bold Hello, Hello, guest
28
JSTL Looping <% String [] items = (String[])request.getAttribute("movieList"); String var = null; for (int i = 0; i < items.length; i++) { var = items[i]; %> ${movie}
29
JSTL Conditionals Now you can stop even if you drive insanely fast. Our brakes won't lock up, no matter how badly you drive. Our brakes are the best.
30
JSTL Sheriff, Bartender, Cowgirl ${foo.name}
31
JSTL ${param.subTitle}
32
JSTL ">Click here The URL using params is: ${inputURL}
33
JSTL
34
JSTL
35
JSTL ">Click here The URL using params is: ${inputURL}
36
Error Pages Bummer. You caused a ${pageContext.exception} on the server. About to be bad...
37
Error Pages and the DD java.lang.Throwable /errorPage.jsp java.lang.ArithmeticException /arithmeticError.jsp 404 /notFoundError.jsp
38
Handling Exceptions in a JSP About to do a risky thing... If you see this, we survived.
39
Handling Exceptions in a JSP About to do a risky thing... There was an exception: ${myException.message} If you see this, we survived.
40
Deciphering the TLD 1.2 RandomTags randomThings random advice advice --> foo.AdvisorTagHandler empty user true
41
Using a Custom Tag Advisor Page public class AdvisorTagHandler extends SimpleTagSupport { private String user; public void setUser(String user) { this.user = user; } public void doTag() throws JspException, IOException { getJspContext().getOut().write("Hello " + user + " "); getJspContext().getOut().write("Your advice is: " + getAdvice()); } private String getAdvice() { // Code to give advice here }
42
Finding the TLD Since JSP 2.0, the container finds TLDs for you in: WEB-INF and its subdirectories Inside JAR files in the lib directory - in the META-INF directory and its subdirectories... randomThings /WEB-INF/myFunctions.tld
43
Lab 10-1 Introduction
44
Exam Review
45
Learning Activity – Music Search Implement MusicSearch servlet using model class Implement SearchResults.jsp using scripting Implement SearchResults2.jsp without scripting
46
Progress Check Due this week: Lab 8-1 Servlets Due next week: Exam 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.