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>


Sunday, 22 February 2015

JSON Read & Write with Java (part II)

There are 2 API to perform JSON processing:

  • Object model ( JsonReader/JsonWritter)
  • Streaming (JsonParser/ JsonGenerator)

In the previous post we reviewed the use of JSON processing using object model : create objects with JsonBuilder, and I/O actions with JsonReader and JsonWritter.

Today, we will take a look to the other alternative of processing JSON with JsonParse and JsonGenerator.

Remember that working with Json processing requires to import the package javax.json.* (Available if we are working with Java7 enterprise edition). 

Write with JsonGenerator

The output will be:
{"type":"cheeseburguer","drink":"coke","sides":["chips","salad"]}

Read with JsonParse

The output will be:
type
cheeseburguer
drink
coke
sides
chips
salad



Saturday, 21 February 2015

JSON Read & Write with Java


JSON processing in Java is a new specification in JavaEE7 (JSR353).

Overview:
The acronym JSON means Javascript Object notation, and is a format for data interchange, as XML.

Below there is an example of a object : burguermenu represented in both JSON and in XML syntax:

Json syntax
{"menu":  {"type":"cheeseburguer", 
 "drink":"coke",
 "side":["chips","salad"]    
 }
}
XML syntax
<menu>
<type>cheeseburguer</type>    <drink>coke</drink>    <side>chips</side>    
<side>salad</side>
</menu>


To be able with deal with Json directly in Java we will need to import the package javax.json.* (Remeber this package is included in javaEE 7, and is not available in previous version). Alternatively you can download this library from https://jsonp.java.net/

There are two ways to perform I/O actions : ObjectModel and Streaming model
In this post we will explore the first one, with simple examples of reading and writing.

Create Json object in Java:

In the example a json object menu is created with JsonObjectBuilder:


Write a JsonObject --> obtain Json text

The method write the above object using JsonWriter
The output will be:
{"menu":{"type":"cheeseburguer","drinks":"coke","side":["chips","salad"]}}

Read Json text --> obtain a JsonObject

This method read the above output string using JsonReader and it will obtain a JsonObject The output will be:
{"type":"cheeseburguer","drinks":"coke","side":["chips","salad"]}
cheeseburguer
chips
salad


Monday, 16 February 2015

Java8 Method Enhancements

Add a body to a method in a interface?
No, because all methods in an interface are abstract.

Add a static method to an interface?? 
No, because static methods cannot be abstract and be overridden in subclasses. 

...but it was before Java8

In Java8 interfaces can contain abstract method, static methods an default methods. 

  • Static and default methods contain implementation.
  • All abstract, default, and static methods in an interface are implicitly public

Default method:

A default method has the word default and it includes a body with the implementation as we can see the method isAvailable in the following example:


When you extend an interface that contains a default method, you can do the following:

- Override : default boolean isAvailable(LocalDate date){//new implementation}
- Make it abstract: boolean isAvailable(LocalDate date);
- Inherit the default method: not including nothing in the extended interface

In the following example the above interface Agenda is extended in the interface WorkAgenda. The default method is overriden because a new implementation is included.  

In case we would prefer to make it abstract we could write the method signature but without body. A class that implements an interface must implement all the abstract methods declared in the interface.


Static Methods
Including static methods in interfaces replaces the idea of defining utilities in separate Utility class.

The static method is part of the interface and not of the implementing class. So the way to call the method is using the interface name. In the following example the static method formatDate is used in the main method. As formatDate is defined in the interface Agenda, the notation is Agenda.formatDate(Date date);

And remember that when an interface is modified to add a new default or static method, the classes that already implemented the interface do not need to be modified.