Sunday, 24 May 2015

OCEJWCD.- 6. More view facilities (part2)

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

6.4. Create and use arithmetic expressions in EL.
The expressions inside EL are evaluated at execution time and sent to the output stream.
Inside the brackets we can evaluate conditions or calculate an arithmetic result.

Examples: 
${3+7}  --> returns 10
${2*(5%3)} --> returns 4

Rules with EL
- EL is null friendly. In case of null or unknown values the page still displays.
- EL treats null value as "zero" in arithmetic expressions.
- EL treats null value as "false" in boolean expression.

6.5. Identify the need for iteration and selection in the view, and use JSTL tags to address those needs.
Example: Creating a servlet that adds a list of elements to the request. Then a list  iteration is displayed in the browser.

1) Include servlet declaration in deployment descriptor (web.xml)
  <servlet>
    <servlet-name>Listado</servlet-name>
    <servlet-class>example.Listado</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Listado</servlet-name>
    <url-pattern>/List</url-pattern>
  </servlet-mapping>

2) Define method get in servlet class
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<String> fruitList = new ArrayList<String>();
fruitList.add("orange");
fruitList.add("apple");
fruitList.add("pineapple");
request.setAttribute("fruitList", fruitList);
this.getServletContext().getRequestDispatcher("/Selection.jsp").
forward(request, response);
}
view raw Listado.java hosted with ❤ by GitHub
3) JSP using JSTL iteration
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<select id="list">
<c:forEach items="${fruitList}" var="fruit">
<option value="${fruit}">
${fruit}
</option>
</c:forEach>
</select>
</body>
</html>
view raw Selection.jsp hosted with ❤ by GitHub
Then writing the following url in the browser: http://localhost:8080/ExampleServlets/List a selection list is displayed:

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

No comments:

Post a Comment