Monday 30 March 2015

OCEJWCD - 2. Introduction to Java Server Pages

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

2.1. Describe why servlets are not the whole solution

Generate dynamic content with a sevlet class a first approach can be writing html in the output and be rendered in a browser. This can work but it is not the most convenient solution.

Example of HTML embedded in a java program:





This is not a good solution due to:
-difficult to read and maintain.
-ugly code and with compilation problems because of carriage returns, quotes...etc,all the characters that will need to be scaped.

Instead of applying HTML in java the solution is java in HTML --> JSP

JSP (Java Server Pages) is a technology to create dynamic web pages based in HTML, XML, using Java programming language.

2.2. Describe essentials of JSPs

JSP basic syntax:
  • Directives : 
    • <%@ page...
    • <%@ include...
    • <%@ taglib...
  • Scriplet:     
    • <% out.println("name");%>
  • Expression  
    • <%= Math.round(2.56324)%>
  • Declaration 
    • <%! int count=1;%>
JSP lifecycle process:
JSP is just a servlet. The container translates jsp and convert it into .java then the container calls the servlet service() method.
  1. The browser sends an HTTP request to the web server.
  2. The web server recognizes that the request is for a JSP page based on the URL and forwards it to the container.
  3. The container loads the JSP page and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code that implements the corresponding dynamic behaviour of the page. MyJSP.jsp --> MyJSP_jsp.java
  4. The container compiles the servlet into an executable class. MyJSP_jsp.java -->MyJSP_jsp.class
  5. The container loads the Servlet class and instantiated it (jspInit())
  6. The container creates a new thread and and executes it( _jspService()). During execution, the servlet produces an output in HTML format, inside an HTTP response.
  7. The web server forwards the HTTP response to the browser.
  8. Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page.


2.3. Understand fundamentals and reasons for MVC architecture

The fundamentals to apply JavaEE patterns are:
-Performance
-Modularity
-Flexibility
-Maintainability
-Extensibility

Reasons for MVC architecture:
  • separation of concerns (every element model, view , controller can be changed independently, and minimizes the impact in other tiers of the application, at the same time ) 
  • loose couplings (model component hide internal details to  the view a controller components).
OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

Tuesday 10 March 2015

OCEJWCD - 1. Introduction to Java Servlets

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

1.1    Describe web applications, CGI, and the role of Java

Web applications are the software app that runs in a web browser.

CGI is Common Gateway Interface. It was the technology commonly used as a standard for dynamic web programming until they were replaced by servlets.

They are based in a helper app that work with the server (written generally in Perl) and serves dynamically generated data in an HTML static page.

Role of Java

JavaEE application Server


Servlets technology serves dinamically data to the client usign JSP (java server pages). 

Servlets Container:

  • Communications support (handle the request and response)
  • Lifecycle management (init/service/destroy)
  • Multithreading support (1 thread per rquest)
  • Declarative security (in DD)
  • JSP support


1.2    Describe benefits of Java servlet technology
  • Servlets have the advantages of portability and security that java technology provides.
  • Servlets are more efficient , CGI program needs to load for every request, but servlets are called first time they remain active in the servlet memory, for every request a new thread is created.

1.3    Create a simple Java Servlet

Example of Servlet:


Notes to create web applications in Eclipse:

  • Create web project : Select new project->web->dynamic web project
  • Create servlet class: In our scr package create a new: Servlet. It creates a class with autogenerated methods to define a servlet.
  • Create Deployment Descriptor: Web Project -> rightClick -> JavaEE Tools -> Generate Deployment Descriptor Stub.

1.4    Define three-tier architecture


Three-tier architecture is a software architecture, every layer is developed and maintained as independent modules.

1.5    Define Model-View-Controller (MVC) architecture

Model view controller is an architectural pattern for implementing web applications.


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

Sunday 8 March 2015

OCEJWCD - certification in web component developer JavaEE6

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

This is the certification I am currenty studying :  Oracle certified Expert in Web Component Developer.
I will be sharing in following posts the summaries about the different topics that I am working on.
I hope it will be a useful resource for reference and review. But overall I recommend a good book and coding as you learn: a good combination of reading and practice.
For preparing this certification these are the materials I am currently using :

  • Book HeadFirst Servlets & JSP (O'Reilly). Authors: Bryan Basham, Kathy Sierra & Bert Bates.
  • Servlets 3.0 specification (JSR-000315)

For practicing the exam:
  • I will buy the full version of mock Exams Enthuware (there is also a trial version).
  • The book HeadFirst Servlets & JSP also includes a final mock exam in the appendix.
Also taking a look to the forum JavaRanch is always recommendable, because in this site people share their experiences with the exam, share common doubts, and contains useful resources.
The requirements for this certification is to have passed OCPJP (formely SCPJP). It is the most common certification to start after passing the JavaSE certification, and it is a good point to start with JavaEE certification path.
The existing certifications for JavaEE 6 currently are:
There are analogous certifications for J2EE 1.5 but I think it is worthy to go directly for the latest version available in the exams, in this case 6 (take into account the latest version in JavaEE is 7).

In education.oracle.com you can find further information.
Specifically for OCEWCD the main details of the exam are:
  • Number of questions: 57
  • Passing score: 64%
  • Duration: 140 min
Topics:
OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

Sunday 1 March 2015

JAXB : XML Read & Write in Java

JAXB is "Java Architecture for xml binding" and consists in:
-Schema compiler
-Schema generator
-Binding runtime framework

The classes to read, write and binding XML in Java are included in JavaSE and JavaEE. These classes are contained in package: javax.xml.bind.*

We will analyze two examples of read and write XML using marshalling and unmarshalling as in the following diagram:


Read/Write operations:

XML --> Unmarshalling --> Object
XML <-- Marshalling    <--  Object

By default the encoding in the marshalling process is UTF-8

Example:
In the following example the readXML method will read an XML file and it will be converted into an Object. Then the writeXML method transform the object into XML again.

XML menu

Object menu
Not to forget to use annotations to indicate the root and elements: @XmlRootElement and @XmlElement :



  • Read XML with JAXB
The steps are:
-Create a JAXBContext
-Create object Unmarshaller
-Define inputStream object
-Read XML using the unmarshalling object



  • Write XML with JAXB 
The steps are:
-Create a JAXBContext
-Create object Marshaller
-Define the output format
-Define outputStream object
-Write XML using the marshalling object



  • Executing the main method - Results


The output in console will be:
cheeseburguer
coke
chips
salad

Also as a result of writing method a new output.xml file will be created:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<menu>
    <drink>coke</drink>
    <side>chips</side>
    <side>salad</side>
    <type>cheeseburguer</type>
</menu>