Thursday 21 May 2015

OCEJWCD.- 6. More view facilities (part1)

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification
6.1. Understand the four data scopes.

The scopes in a JSP are four:
  • Page scope -- pageContext
  • Request scope -- request
  • Session scope -- session
  • Application scope -- application
Note: The scopes in a servlet are three: request(request), session(request.getSession()) and application(getServletContext()).

6.2. Understand and use EL dot and array access operators with Java Beans, arrays and collections.

EL (expression language) was introduced in JSP 2.0.  Some basics of EL are explained in this post:
ocewcd-3-implementing-mvc-design-part2

Features of EL:
  • There is not typecasting
  • Conversions are usually done implicitly
  • Double and single quotes are equivalent
  • Dot and array access operators
${object.property}     --> object can be a map or a bean
${object["property"]} --> object can be: map, bean, List or array. Inside the brackets is the key of the map, the attribute of the bean, the position of the list or array.

Example : to access property name from object user we can use these 3 different notations:
${user.name}
${user["name"]}
${user['name']}

More examples of EL (accessing bean and implicit variable):
6.3. Understand and use EL implicit objects.

EL Implicit objects:
  • pageScope - gets attribute from Page scope
  • requestScope  - get request attributes
  • sessionScope - get session object attributes
  • applicationScope - get application level attributes
  • param - servletRequest.getParameter()
  • paramValues - servletRequest.getParameterValues() 
  • header - get request header
  • headerValues - get request headers
  • cookie -  get Cookie objects
  • initParam - get context initialization parameters
  • pageContext - same as JSP pageContext
Examples:
${param.name}
${paramValues.name[0]} 
${header.host}

We can see in  detail how to access a parameter from a request (in a form that we want to submit). Then we can access the values using EL by param implicit object.
<form action="login.do" method="post">  
Enter user:<input type="text" name="user"/><br/><br/>  
Password  :<input type="password" name="password"/><br/><br/>  
<input type="submit" value="login"/>
User name is ${param.name}
User password is ${param.password}
See next post : More view facilities (part2)

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

No comments:

Post a Comment