Sunday 10 May 2015

OCEJWCD - 3. Implementing an MVC Design (part2)

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

3.3.  Forward control from a servlet to a JSP

One way to perform this is using javax.servlet.RequestDispatcher interface

ServletContext sc = this.getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");

There are 2 methods:
rd.include(request, response);
rd.forward(request, response);

The difference between them is that include will return the control to the invoking servlet but forward not.

* Note: For invoking a servlet from a JSP, the following jsp actions can be used,for example:
<jsp:include page="/MyServlet />
<jsp:forward page="/MyServlet />

The second way is using HttpResponse.sendRedirect(), this method of HttpServletResponse interface can be used to redirect response to another resource (servlet, jsp or html file).

response.sendRedirect("..");

Differences between sendRedirect and forward.
  • The forward method works at server side and sendRedirect at client side.
  • Request dispatcher can be used only when the resource is being forwarded resided in the same application. Otherwise sendRedirect can be use within an outside the server.
  • Forward method sends the same request and response method and sendRedirect sends a new request.

3.4.  Understand fundamentals of EL

EL Expression Language. It allows to dynamically access data from JavaBeans components by using simple expressions.

Examples of notation:

  • ${person.name}
where: 
- first argument is a map or bean name.
- second argument is a map key or bean property.

  • ${person["name"]}

in this case in the square brackets we can use a map key, bean property or an index in case of a list or array object: ${personList[0]}

EL with implicit objects. Some examples:

  • Request params ${param.name}
  • Header ${header.host}
  • page Context ${pageContext.request.method}
  • Cookie ${cookie.userName.value}
  • Context init param ${initParam.email}

List of operators in EL


  • [] .


  • ()


  • not ! empty


  • * / div % mod


  • + - (binary)


  • < > <= >= lt gt le ge


  • == != eq ne


  • && and


  • || or


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

No comments:

Post a Comment