3. public void doPost( HttpServletRequest req,
HttpServletResponse res) throws
ServletException, IOException {
Human p = new Human();
p.setName("Evan");
Dog dog = new Dog();
dog.setName("Spike");
p.setDog(dog);
req.setAttribute("person", p);
RequestDispatcher rd =
req.getRequestDispatcher("withscript.jsp");
rd.forward(req,res);
}
4. <body>
<%= ((jsp.example.bean.Human)
request.getAttribute("person")).getDog().get
Name() %>
<jsp:useBean id="person"
class="jsp.example.bean.Human"
scope="request"/>
Dog's name is : <jsp:getProperty
name="person" property="dog"/>
</body>
19. • In Servlet
Map musicMap = new HashMap();
musicMap.put(“Ambient”, “Zero”);
request.setAttribute(“musicMap”,musicMap);
20. • In JSP
• ${musicMap.Ambient}
• ${musicMap[“Ambient”]}
21. What About This ?
• ${musicMap[Ambient]}
• This time with no “ “
22. • In Servlet
Map musicMap = new HashMap();
musicMap.put(“Ambient”, “Zero”);
request.setAttribute(“musicMap”,musicMap);
request.setAttribute(“Genre”,”Ambient”);
23. • In JSP
• ${musicMap[“Genre”]} -> doesn’t work
• ${musicMap[Genre]} -> evaluated
because there’s an attribute object named
Genre and it has a value named “Ambient”
25. Don’t try…
• Don’t put everything that are not qualified
as an identifiers behind the dot (.)
operator!
• ${musicMap[“Ambient”]} -> ${musicMap.Ambient}
• ${musicList[“1”]} -> X ${musicList.1}
34. • With scriptlet
<% Cookie[] cookies = request.getCookies();
for(int i=0; i < cookie.length; i++) {
if ((cookies[i].getName()).
equals(“username”)) {
out.println(cookies[i].getValue());
}
}
%>
• With EL
${cookie.userName.value}
38. package jsp.example.method;
public class DiceRoller {
public static int rollDice() {
return (int) ((Math.random()*6)+1);
}
}
The method must be public and static