Showing posts with label servlets. Show all posts
Showing posts with label servlets. Show all posts

Sunday, May 1, 2011

Web Component Developer Certification


Hi again, it's been a while since my last post. I just got certified as Oracle Certified Web Component Developer for J2EE 5 (OCPJWCD, also known as SCWCD), that's the reason why I've been away for the last month. I want to share some tips about passing the exam:

Exam Name: Oracle Certified Web Component Developer for J2EE 5
Exam Code: 310-083
Number of Questions: 69
Score to pass: 70%
Preparation Time: 4 months (about 6 hours/week)
Test Site: Prometric
Date: 5/11/2011

Why Certified?
There are many benefits about being certified, you can validate your knowledge, improve your work efficiency and even ask for a better income... I've been working with java for Web for about 3 years already and I thought this certification was going to be easy, but it wasn't. During my preparation I realized that there are several functionalities I wasn't aware of. Now that I'm certified I feel really good and confident about the technology and I'm even going to start studying for the web services certification. Anyway, the reason why I took the certification was to improve my knowledge about the java technology and aspire for a better income.

Why JEE 5 instead of JEE 6?
That was a really tough desicion, because a new version of the exam had arrived with the all new JEE 6... so I thought that taking the JEE 5 exam was like learning old technology. 
To make my decition I had to validate which technology my clients were using. I realized that none of my clients were using JEE 6, but they do have several inhouse web applications in JEE 5. It's not like they aren't getting in JEE 6 ever, but at short term JEE 5 would give me more job opportunities. Perhaps, next year I will update my certification to JEE 6, I think I'm going to let the market lead me.

Preparation
I've had previous experience with Java for Web, therefore I thought that studying for this certification was piece of cake... but the truth is that many of the underlying processes and the servlet container's responsabilities were unknown to me. I used this really great book, if you are looking for a good book to take the exam and learn, I truly recommend this one:


It has a lot of pages, believe me I know... but the reading is so easy and it has so many graphics and good sense of humor that made my reading very pleasent. As a regular employee I had to study at nights and weekends that's why I used to study 6 hours per week and it took me about 3 months and a half to finish the reading and exercises. After reading the book I started to prepare myself with mock exams. I was able to find some for free (check the links at the end) but I decided to buy some. 

For my previous certifications exams I used "whizlabs", it has a nice web interface so you can access it everywhere if you have internet connection and it gives you 1 test exam and 5 mock exams to validate your knowledge, also the reporting tool is pretty good. During that week I googled some other mock exams and then I found "enthuware". Checking the forums I realized that enthuware was as good as whizlabs, it wasn't a Web interface but it gives you 8 mock exams,  so I decided to give it a chance, also because whizlabs was USD75 and enthuware was USD20 at that time... I'm not getting in the discussion about which one is better, I have used both and I like both, it's just that enthuware was at a better price at that moment.

During the exam
You have 180 minutes to finish the exam, it took me about 90 minutes to finish it. I think my preparation was really good, I'm glad I chose enthuware because the UI interface was very similar to the real exam and some of the questions were similar too. My score was 88% which is very good. Something I wasn't prepare for was JSTL, the exam had about 5-10 questions about it, fortunately my job experience helped me there.

Useful Links
http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=322
http://www.coderanch.com/how-to/java/ScwcdLinks
http://www.enthuware.com/index.php/mockexams/sunjavacertifications/scwcd-questions
http://www.whizlabs.com/scwcd/scwcd.html


I hope those of you who are thinking about taking the exam may find this post useful, from now on I will keep posting about JavaMe and I'm going to start posting about Java for Web too.


bye.

Sunday, January 9, 2011

Servlet-GSON vs JSONME-JavaME III

This is the third post about JSON and JavaME. In previous releases: first we introduced JSON for JavaMe (J2ME), then we improved the example by adding compression/decompression support. Now, we are going to introduce Base64 format support for sending images in our JSON example.
Although this is not a best practice, because you should send the image's URL  instead of sending the image itself in the JSON String, sometimes you just need to do exactly that.

Requirements:

Servlet-GSON vs JSONME-JavaME: Contains the original classes. More info:
http://www.java-n-me.com/2010/11/servlet-gson-vs-jsonme-javame.html

Servlet-GSON vs JSONME-JavaME II: Second part of the saga. Adds compression/decompression support. More info:
http://www.java-n-me.com/2010/12/servlet-gson-vs-jsonme-javame-ii.html

Base64 encode-decode in JavaMe: Explains how to use Base64 format in JavaMe (J2ME). More info:
http://www.java-n-me.com/2010/12/base64-encode-decode-in-javame.html


You should check the previous requirements if you haven't, because in this post we use some of the classes defined there.

The image we are going to send is in PNG format:



OK, let's begin, first we are going to show you the changes in some of the original classes. As usual, let's start with the server side:

import java.awt.Image;
public class Client {

   /** Name of the client*/
    private String name;

    /** Last name of the client*/
    private String lastName;

    /** Identification of the client*/
    private String id;

    /** Photo of the client*/
    private transient Image photo;


    /** Photo of the client in Base 64 format*/
    private  String photoBase64;


    //Getters and Setters...
}

Our Client class has now two new attributes for the photograph of the client. The java.awt.Image attribute binds to the photograph of the client directly. The String attribute photoBase64 contains the Base64 format of the photograph that is going to be sent in the JSON String. Notice that the attribute photo is marked as transient, meaning that GSON won't use this attribute in the JSON String.

Following is the servlet class which will create a client with a photograph, will get the JSON String representation of the client and his photograph and will compress the data before sending the response to the mobile client. The servlet uses the GSON API, the java.util.zip.GZIPOutputStream and the JSE sun.misc.BASE64Encoder class:


import com.google.gson.Gson;
import java.io.*;
import java.net.URL;
import java.util.Vector;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import javax.swing.ImageIcon;
import model.Client;
import sun.misc.BASE64Encoder;

public class ClientsServlet extends HttpServlet {
   ...

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //sets the type of response and the charset used
        //Be careful, in your mobile client, read the response using the same charset
        //if sending the response as text. If you sent it as binary data,
        //there is no problem
        response.setContentType("application/json; charset=UTF-8");

        //create some clients and add them to the vector
        Vector vClients = new Vector();

        //used to convert Image to Base64 format
        BASE64Encoder encoder = new BASE64Encoder();

        Client clientOne = new Client();
        clientOne.setName("Alexis");
        clientOne.setLastName("López Ñ");
        clientOne.setId("123456");
        clientOne.setPhoto(new ImageIcon("images/duke.png").getImage());
        //get the image bytes. Read it as an URL
        byte[] imgBytes =
          getImageBytes("http://localhost:8080/GSON_Servlet/images/duke.png");
        String imageCoded = encoder.encode(imgBytes);
        clientOne.setPhotoBase64(imageCoded);

        //... add more clients...
      
        vClients
           .add(clientOne);

        //convert the clients vector to JSON using GSON, very Easy
        Gson gson = new Gson();
        String jsonOutput = gson.toJson(vClients);

        System.out.println("*****JSON STRING TO RESPONSE*****");
        System.out.println(jsonOutput);
        System.out.println("*********************************");

        System.out.println("Length of String in bytes = "
                                    + jsonOutput.getBytes().length);
        //compress de data
        byte[] compressed = compress(jsonOutput);
        System.out.println("Length of compressed bytes = "
                                    + compressed.length);

        //send the binary response
        ServletOutputStream out = response.getOutputStream();
        out.write(compressed, 0, compressed.length);
        out.flush();
        out.close();
    }
    ...
}

The servlet uses two important methods. One is the +compress(String):byte[] that we have analized in the previous post of this saga. The other one is the +getImageBytes(String):byte[] which is shown next:

/** * Returns the binary representation of the image passed as an URL * @param imgURL URL String of the image * @return array of binary data representing the image * @throws IOException */ public static byte[] getImageBytes(String imgURL) throws IOException { URL imgUrl = new URL(imgURL); ByteArrayOutputStream bout = new ByteArrayOutputStream(); InputStream in = imgUrl.openStream(); int i = 0; while ((i = in.read()) != -1) { bout.write(i); } in.close(); bout.close(); byte[] bytes = bout.toByteArray(); return bytes; }

The last method reads the image from the URL and gets its binary representation. One important thing to keep in mind, is that you should use PNG Images as it is the default format for JavaMe (J2ME) MIDP applications, not doing so may arise exceptions on the mobile client when parsing the Image data. When the servlet is run and it receives a POST request, the following message is shown on the console output:

INFO: *****JSON STRING TO RESPONSE***** 9/01/2011 06:11:23 PM INFO: [{"name":"Alexis","lastName":"López Ñ","id":"123456","photoBase64":"iVBORw0KGgoAAAANSUhEUgAAAP8AAADICAMAAAAQqcftAAAAAXNSR0IArs4c6QAAAwBQTFRFDAAA\r\nCgQKGAAAEAgMGAAIGAAQGAgIGAgQEBAIABAQCBAQEBAQEBAYEBgQEBgYECEYGAgYGBAIGBAQGBAY\r\nGBgIGBgQGBgYGBghGCEYIQAAIQAIIQAQIQgQIQgYIRAQIRAYKQAMKQgQKQAYIRAhIRgQIRgYIRgh\r\nISEYMQAILgUVNwQSQgAYKRAUKRgYKSEYPA0YISEhKRAhKRghKRgpMRQhMRgpPQghPRQhKSEhKSkh\r\nISEpKSYrMSUlNi4pOScvOzU1SBwpRkI9WgYgbBk1RkJKTkdHVkhNVFJMUlJaWlRUXlhaZVtdb2Fk\r\ncWxne2lwf3h5yBUsqDBU1Bcx0SRJ3BU53xs53yJK4TJjin5"
... not all data shown ... "}] INFO: *********************************



INFO: Length of String in bytes = 17425
INFO: Length of compressed bytes = 12890

As you can see, the JSON representation of the client now has the photograph in Base64 format in order to send the image data to the mobile client as part of the JSON String. Also notice that there is some compression in the response to the client.

On the mobile client side, there are also changes in the Client class:

import java.util.Vector;
import javax.microedition.lcdui.Image;
import org.bouncycastle.util.encoders.Base64;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

public class Client {


    /** Name of the client*/
    private String name;

    /** Constant name of the attribute name*/
    private static final String ATT_NAME = "name";

    /** Last name of the client*/
    private String lastName;

    /** Constant name of the attribute last name*/
    private static final String ATT_LAST_NAME = "lastName";

    /** Identification of the client*/
    private String id;

    /** Constant name of the attribute id*/
    private static final String ATT_ID = "id";

    /** Photo of the client*/
    private  Image photo;

    /** Photo of the client in Base 64 format*/
    private  String photoBase64;


     /** Constant name of the attribute photoBase64*/
    private static final String ATT_PHOTO_BASE_64 = "photoBase64";

    //Getters and Setters...

    /**
     * This method should be used by this class only, that's why it is private.
     * Allows to get a JSONObject from the Client passed as parameter
     * @param client Client Object to convert to JSONObject
     * @return JSONObject representation of the Client passed as parameter
     */
    private static JSONObject toJSONObject(Client client) {
        JSONObject json = new JSONObject();
        try {
            json.put(ATT_NAME, client.getName());
            json.put(ATT_LAST_NAME, client.getLastName());
            json.put(ATT_ID, client.getId());
            //only if the client has a photo associated send it as JSON
            if(client.getPhotoBase64() != null)
            {
                json.put(ATT_PHOTO_BASE_64, client.getPhotoBase64());
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    /**
     * Allows to get a Client Object from a JSON String
     * @param jsonText JSON String to convert to a Client Object
     * @return Client Object created from the String passed as parameter
     */
    public static Client fromJSON(String jsonText) {
        Client client = new Client();
        try {
            JSONObject json = new JSONObject(jsonText);

            //check if the JSON text comes with the attribue Name
            if (json.has(ATT_NAME)) {
                //asign the String value of the attribute name to the client
                client.setName(json.getString(ATT_NAME));
            }

            //check if the JSON text comes with the attribue last name
            if (json.has(ATT_LAST_NAME)) {
                client.setLastName(json.getString(ATT_LAST_NAME));
            }

            //check if the JSON text comes with the attribue id
            if (json.has(ATT_ID)) {
                client.setId(json.getString(ATT_ID));
            }

             //check if the JSON text comes with the attribue photo
            if (json.has(ATT_PHOTO_BASE_64)) {
                client.setPhotoBase64(json.getString(ATT_PHOTO_BASE_64));


                //once the photo in base 64 format has been read,
                //convert it to an Image. IMPORTANT: PNG image format or
                //you may get an Exception
                byte[] decoded = Base64.decode(client.getPhotoBase64());
                Image img = Image.createImage(decoded, 0, decoded.length);
                client.setPhoto(img);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return client;
    }
}

You can find the changes in bold. The important changes are the adition of the attributes for managing the photograph and the modification of the methods for parsing the JSON String to and from an Object. Notice the use of the org.bouncycastle.util.encoders.Base64 class to decode the Base64 format data of the photograph. You can find the original source code of the Client class (mobile client side) in the first post of this saga.

Next is the MIDlet that runs the code:

import com.tinyline.util.GZIPInputStream;
import java.io.*;
import java.util.Vector;
import javax.microedition.io.*;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;


public class JSONMIDlet extends MIDlet {


    private Form f = null;


    public JSONMIDlet() {
        //form for the photos
        f = new Form("Photo testing");
    }


    public void pauseApp() {
    }


    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }


    public void startApp() {
        try {
            //ask for the clients
            Vector clients = getClients();


            //show information of the clients
            System.out.println("*****CLIENT INFORMATION*****");
            for (int i = 0; i < clients.size(); i++) {
                Client cl = (Client) clients.elementAt(i);


                System.out.println("Client " + (i + 1) 
                                                + " name = " + cl.getName());
                System.out.println("Client " + (i + 1) 
                                                + " last name = " + cl.getLastName());
                System.out.println("Client " + (i + 1) 
                                                + " ID = " + cl.getId());
                System.out.println("Client " + (i + 1) 
                                                + " Photo Base64 = " + cl.getPhotoBase64());
                System.out.println("");


                //if the client has a photo, append it to the form
                if (cl.getPhoto() != null) {
                    f.append(cl.getPhoto());
                }
            }




            Display dsp = Display.getDisplay(this);
            dsp.setCurrent(f);




        } catch (IOException ex) {
            //manage the exception
            ex.printStackTrace();
        }
    }


...//other methods


}

The method +getClients():Vector has not changed and can be found in the last post of this saga (as there is compression/decompression support). Finally the emulator shows the following:


Well, that's it for today. Quite a large post, I hope I was clear enaugh, but if you have any questions do not hesitate to contact me.

see ya soon!


References:

TinyLine Utils. 2010. TinyLine [online].
Available on Internet: http://www.tinyline.com/utils/index.html
[accessed on December 11 2010].

Using JavaScript Object Notation (JSON) in Java ME for Data Interchange. Agosto 2008. Oracle [online].
Available on Internet: http://java.sun.com/developer/technicalArticles/javame/json-me/
[accessed on October the 26th 2010].

meapplicationdevelopers: Subversion. 2007. Oracle [online].
Available on Internet: https://meapplicationdevelopers.dev.java.net/source/browse/meapplicationdevelopers/demobox/mobileajax/lib/json/
[accessed on October the 27th 2010].

The Legion of the Bouncy Castle. bouncycastle.org [online].
Available on Internet: http://www.bouncycastle.org/
[accessed on December 28 2010].

Saturday, December 25, 2010

Servlet-GSON vs JSONME-JavaME II

This is the third and last part of our compression saga in Java Me (J2ME). Right until now, we have seen two libraries: TinyLine Utils (Decompress only) and JAZZLIB (Compress/Decompress). You can use whichever you want based on the requirements of your project.

In this post, we are going to improve our Servlet-GSON vs JSONME-JavaME example. We are adding compression support to the server side and decompression support on mobile client side. We will calculate the size of the data to be sent over the network before and after compression. This is helpful, when you don't have unlimited data connection on the mobile client and you need to reduce costs.

So, let's begin with the server side, the following code is the compression method used in our servlets project:

import java.util.zip.GZIPOutputStream;
...
    /**
     * Compress the String parameter
     * @param message
     * @return byte array of compressed data
     * @throws IOException if any error
     */
    public static byte[] compress(String message) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gz = new GZIPOutputStream(out);

        gz.write(message.getBytes());
        gz.flush();
        gz.close();

        out.flush();
        byte[] array = out.toByteArray();
        out.close();

        return array;

    }
...


As we are on server side, we can use the java.util.zip.GZIPOutputStream class in order to compress the data. Next, is the servlet code we have seen before but now it uses the compress method:

...
    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //sets the type of response and the charset used
        //Be careful, on your mobile client, read the response using the same charset
        response.setContentType("application/json; charset=UTF-8");

        //create some clients and add them to the vector
        Vector vClients = new Vector();

        Client clientOne = new Client();
        clientOne.setName("Alexis");
  //Note de special characters to be transmitted
        clientOne.setLastName("López Ñ");
        clientOne.setId("123456");

        vClients.add(clientOne);

        Client clientTwo = new Client();
        clientTwo.setName("Second");
        clientTwo.setLastName("Client");
        clientTwo.setId("987534");

        vClients.add(clientTwo);

        Client clientThree = new Client();
        clientThree.setName("Colombia");
        clientThree.setLastName("JUG");
        clientThree.setId("555555");

        vClients.add(clientThree);

        //convert the clients vector to JSON using GSON, very Easy
        Gson gson = new Gson();
        String jsonOutput = gson.toJson(vClients);

        System.out.println("*****JSON STRING TO RESPONSE*****");
        System.out.println(jsonOutput);
        System.out.println("*********************************");

  System.out.println("Length of String in bytes = " 
                                    + jsonOutput.getBytes().length);
        //compress de data
        byte[] compressed = compress(jsonOutput);
        System.out.println("Length of compressed bytes = " 
                                    + compressed.length);

        //send the binary response
        ServletOutputStream out = response.getOutputStream();
        out.write(compressed, 0, compressed.length);
        out.flush();
        out.close();
    }
...


As you can see, is the same method used in the first version of our Servlet-GSON vs JSONME-JavaME example, but now it uses the compress method before writing the response. Another difference, is that this time we are writing binary data so we use the javax.servlet.ServletOutputStream. As we are writing binary data, there is no need to be careful with the charset when reading the response on the mobile client side. Following is the output from the previous code:


INFO: *****JSON STRING TO RESPONSE*****
INFO: [{"name":"Alexis","lastName":"López Ñ","id":"123456"},{"name":"Second","lastName":"Client","id":"987534"},{"name":"Colombia","lastName":"JUG","id":"555555"}]
INFO: *********************************

INFO: Length of String in bytes = 157
INFO: Length of compressed bytes = 114


From the previous output, you can see that there is some compression. To be more accurate you can use a sniffer or protocol analizer (such as wireshark) in order to detect the packages sent over the network and to get the exact amount of bytes sent from the server to the mobile client.

Now let's check the decompression method on the mobile client side:

import com.tinyline.util.GZIPInputStream;
//You can also use net.sf.jazzlib.GZIPInputStream;
...
   /**
     * Decompress the data received in the Stream.
     * @param in <code>InputStream</code> of the connection where the
     * compressed data is received.
     * @return <code>String</code> representing the data decompressed
     * @throws IOException if there is any error during reading
     */
    public static String deCompress(InputStream in) throws IOException {
        StringBuffer sb = new StringBuffer();

        GZIPInputStream gz = null;
        try {
            gz = new GZIPInputStream(in);
            int c = 0;
            while ((c = gz.read()) != -1) {
                sb.append((char) c);
            }
        } finally {
            if (gz != null) {
                gz.close();
            }
        }

        return sb.toString();
    }
...

As we are only decompressing data, you can use any of the two libraries we have evaluated before. Next is the method that connects to the servlet, opens the InputStream and maps JSON Strings to Objects:

...
   /**
     * Connects to the server in order to obtain information of the clients
     * @return Vector of <code>Client</code> objects
     * @throws IOException if errors with the connections
     */
    public Vector getClients() throws IOException {
        //This is my servlet’s URL
        String URL = "http://localhost:8080/GSON_Servlet/ClientsServlet";
        Vector vClients = new Vector();

        HttpConnection http = null;
        InputStream in = null;
        try {
            //Open HttpConnection using POST
            http = (HttpConnection) Connector.open(URL);
            http.setRequestMethod(HttpConnection.POST);
            //Content-Type is must to pass parameters in POST Request
            http.setRequestProperty("Content-Type",
                                                 "application/x-www-form-urlencoded");

            //decompress the data
            in = http.openInputStream();


            //IMPORTANT: As the servlet wrote the data using a binary stream,
            //there is no need to use a special charset
            String jSonString = deCompress(in);

            System.out.println("*****JSON STRING RECEIVED*****");
            System.out.println(jSonString);
            System.out.println("******************************");

            //Parse the JSON String to obtain a Vector of clients
            vClients = Client.fromJSONs(jSonString);

        } //whatever happens, close the streams and connections
        finally {
            if (in != null) {
                in.close();
            }

            if (http != null) {
                http.close();
            }
        }

        return vClients;
    }
...

As said, the previous code connects to the server, reads the response, decompress the response and maps JSON Strings to Objects. The output is shown next, check that the special characters are ok even if we didn't used a special charset when reading the response from the server:


*****JSON STRING RECEIVED*****
[{"name":"Alexis","lastName":"López Ñ","id":"123456"},{"name":"Second","lastName":"Client","id":"987534"},{"name":"Colombia","lastName":"JUG","id":"555555"}]
******************************
*****CLIENT INFORMATION*****
Client 1 name = Alexis
Client 1 last name = López Ñ
Client 1 ID = 123456

Client 2 name = Second
Client 2 last name = Client
Client 2 ID = 987534

Client 3 name = Colombia
Client 3 last name = JUG
Client 3 ID = 555555


Ok, that's it for this post. I hope I was clear enough, if you have any comments or questions do not hesitate to contact me or to leave a comment.

see ya soon!


References:

A pure java implementation of java.util.zip library. 2010. SourceForge [online].
Available on Internet: http://jazzlib.sourceforge.net/
[accessed on December 20 2010].

Jazzlib Java Me. November 2010. STAFF [online].
Available on Internet: http://code.google.com/p/staff/downloads/list?q=jazzlib
[accessed on December 20 2010].

TinyLine Utils. 2010. TinyLine [online].
Available on Internet: http://www.tinyline.com/utils/index.html
[accessed on December 11 2010].

Using JavaScript Object Notation (JSON) in Java ME for Data Interchange. Agosto 2008. Oracle [online].
Available on Internet: http://java.sun.com/developer/technicalArticles/javame/json-me/
[accessed on October the 26th 2010].

meapplicationdevelopers: Subversion. 2007. Oracle [online].
Available on Internet: https://meapplicationdevelopers.dev.java.net/source/browse/meapplicationdevelopers/demobox/mobileajax/lib/json/
[accessed on October the 27th 2010].

Sunday, November 28, 2010

Servlet-GSON vs JSONME-JavaME

In this post I’m going to show you a simple example of data transmition between a mobile client using Java Me and a Server using Servlets. The data format to use in the transmition is JSON.

Requirements:

Servlets: On the server side, servlets will wait for requests. More info:
http://www.oracle.com/technetwork/java/index-jsp-135475.html

Java Me: Used to create mobile applications using Java. More info:
http://www.oracle.com/technetwork/java/javame/overview/index.html

JSON: Data interchange format. More info:
http://www.java-n-me.com/2010/10/json-without-fear-of-friday-13th.html

GSON: Google's library to create JSON Strings from and to objetos. More info:
http://www.java-n-me.com/2010/11/gson-json-in-just-two-lines-of-code.html

JSON Me: Library to work with JSON in Java ME. More info:
http://www.java-n-me.com/2010/11/java-me-and-json.html

Following is the Client class that is used on the server side which we will use to create the Objects to pass through the network in JSON format:


package model;

public class Client {

    /** Name of the client*/
    private String name;

    /** Last name of the client*/
    private String lastName;

    /** Identification of the client*/
    private String id;

    //Getters and Setters...
}


As shown, there is nothing strange. Just a simple class with some attributes. This is because on the server side we have GSON. On the mobile client side, this class has a little more lines of code as we shall see later.

The servlet will respond to POST requests, will create 3 Client objects, convert them to JSON Strings (using GSON) and send the response to the client:



package servlets;

import com.google.gson.Gson;
import java.io.*;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import model.Client;

public class ClientsServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      
        //sets the type of response and the charset used
        //Be careful, in your mobile client, read the response using the same charset
        response.setContentType("application/json; charset=UTF-8");

        //create some clients and add them to the vector
        Vector vClients = new Vector();

        Client clientOne = new Client();
        clientOne.setName("Alexis");
        clientOne.setLastName("Lopez");
        clientOne.setId("123456");

        vClients.add(clientOne);

        Client clientTwo = new Client();
        clientTwo.setName("Second");
        clientTwo.setLastName("Client");
        clientTwo.setId("987534");

        vClients.add(clientTwo);

        Client clientThree = new Client();
        clientThree.setName("Colombia");
        clientThree.setLastName("JUG");
        clientThree.setId("555555");

        vClients.add(clientThree);

        //convert the clients vector to JSON using GSON, very easy!
        Gson gson = new Gson();
        String jsonOutput = gson.toJson(vClients);

        System.out.println("*****JSON STRING TO RESPONSE*****");
        System.out.println(jsonOutput);
        System.out.println("*********************************");

        //print the response
        PrintWriter out = response.getWriter();
        out.println(jsonOutput);
        out.flush();
        out.close();
    }
}

When the servlet is run and it receives a POST request, the following message is shown on the console:

INFO: *****JSON STRING TO RESPONSE*****


INFO: [{"name":"Alexis","lastName":"Lopez","id":"123456"},{"name":"Second","lastName":"Client","id":"987534"},{"name":"Colombia","lastName":"JUG","id":"555555"}]
INFO: *********************************

As you can see, is the JSON representation of the newly created Vector of Client objects. Note that the servlet defines the type of response as JSON ("application/json") and also uses the "UTF-8" charaset. This is important to remember, because when the mobile client is reading the answer, you must use the same charset or you could get strange characters when reading accents or 'Ñ' letter, etc. You may also notice how easy it is to implement GSON to convert objects to JSON Strings. Only two lines of code.

That's it on the server side. Now we will see the Client class on the mobile side and will notice that it has more lines of code than the Client class on the server side. As mentioned before, this is because in Java ME there is NO Java Reflection API and therefore we can not use GSON but we use JSON ME and we have to manually map the attributes of the classes. For more information about the next class, check this previous post: http://www.java-n-me.com/2010/11/java-me-and-json.html


import java.util.Vector;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
public class Client {

    /** Name of the client*/
    private String name;

    /** Constant name of the attribute name*/
    private static final String ATT_NAME = "name";

    /** Last name of the client*/
    private String lastName;

    /** Constant name of the attribute last name*/
    private static final String ATT_LAST_NAME = "lastName";

    /** Identification of the client*/
    private String id;

    /** Constant name of the attribute id*/
    private static final String ATT_ID = "id";

    /**
     * Allows to get the JSON String from the Client passed as parameter
     * @param client Client object to convert to JSON
     * @return JSON String of the Client passed as parameter
     */
    public static String toJSON(Client client)
    {
        return toJSONObject(client).toString();
    }

    /**
     * This method should be used by this class only, that's why it is private.
     * Allows to get a JSONObject from the Client passed as parameter
     * @param client Client Object to convert to JSONObject
     * @return JSONObject representation of the Client passed as parameter
     */
    private static JSONObject toJSONObject(Client client) {
        JSONObject json = new JSONObject();
        try {
            json.put(ATT_NAME, client.getName());
            json.put(ATT_LAST_NAME, client.getLastName());
            json.put(ATT_ID, client.getId());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return json;
    }

    /**
     * Allows to get JSON String from a Vector of Clients
     * @param clients Vector of clients to convert to JSON
     * @return JSON String of the Vector of clients
     */
    public static String toJSONs(Vector clients) {
        JSONArray clientsArray = new JSONArray();
        for (int i = 0; i < clients.size(); i++) {
            Client client = (Client)clients.elementAt(i);

            JSONObject jsonObject = toJSONObject(client);
            clientsArray.put(jsonObject);
        }
        return clientsArray.toString();
    }

    /**
     * Allows to get a Client Object from a JSON String
     * @param jsonText JSON String to convert to a Client Object
     * @return Client Object created from the String passed as parameter
     */
    public static Client fromJSON(String jsonText) {
        Client client = new Client();
        try {
            JSONObject json = new JSONObject(jsonText);

            //check if the JSON text comes with the attribue Name
            if (json.has(ATT_NAME)) {
                //asign the String value of the attribute name to the client
                client.setName(json.getString(ATT_NAME));
            }

            //check if the JSON text comes with the attribue last name
            if (json.has(ATT_LAST_NAME)) {
                client.setLastName(json.getString(ATT_LAST_NAME));
            }

            //check if the JSON text comes with the attribue id
            if (json.has(ATT_ID)) {
                client.setId(json.getString(ATT_ID));
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return client;
    }

    /**
     * Allows to get a Vector of Clients from a JSON String
     * @param jsonArray String that contains Clients JSON Text
     * @return Vector of Clients from the String passed as parameter
     */
    public static Vector fromJSONs(String jsonArray) {
        Vector clients = new Vector();
        try {
            JSONArray jsonArr = new JSONArray(jsonArray);
            for (int i = 0; i < jsonArr.length(); i++) {
                //get a single client JSON Object
                JSONObject json = jsonArr.getJSONObject(i);

                //pass the JSON String to the method in order
                //to get the Client Object
                Client client = fromJSON(json.toString());

                clients.addElement(client);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return clients;
    }

    //Getters and Setters...
}


Finally we'll see the MIDlet that runs and creates an HTTP connection to access the servlet. The connection is established using POST method and we read the data using the same charset that was used when creating the http response to avoid getting strange characters. Then the console displays the values of the attributes of the clients received:



import java.io.*;
import java.util.Vector;
import javax.microedition.io.*;
import javax.microedition.midlet.*;

public class JSONMIDlet extends MIDlet {

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void startApp() {
        try {

            //ask for the clients
            Vector clients = getClients();

            //show information of the clients
            System.out.println("*****CLIENT INFORMATION*****");
            for (int i = 0; i < clients.size(); i++) {
                Client cl = (Client) clients.elementAt(i);

                System.out.println("Client " + (i + 1) + " name = " + cl.getName());
                System.out.println("Client " + (i + 1) + " last name = " + cl.getLastName());
                System.out.println("Client " + (i + 1) + " ID = " + cl.getId());
                 System.out.println("");
            }
        } catch (IOException ex) {
            //manage the exception
        }
        //close the application
        destroyApp(true);
    }

    /**
     * Connects to server in order to obtain information of the clients
     * @return Vector of <code>Client</code> objects
     * @throws IOException if errors with the connections
     */
    public Vector getClients() throws IOException {
        //This is my servlet’s URL
        String URL = "http://localhost:8080/GSON_Servlet/ClientsServlet";
        Vector vClients = new Vector();

        HttpConnection http = null;
        DataInputStream din = null;
        try {
            //Open HttpConnection using POST
            http = (HttpConnection) Connector.open(URL);
            http.setRequestMethod(HttpConnection.POST);
            //Content-Type is must to pass parameters in POST Request
         http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            //Open InputStream to read the response
            din = new DataInputStream(http.openInputStream());
            int i = 0;
            StringBuffer str = new StringBuffer();
            while ((i = din.read()) != -1) {
                str.append((char) i);
            }

            //IMPORTANT: Read the response in the same charset that it was written
            String jSonString = new String(str.toString().getBytes(), "UTF-8");

            System.out.println("*****JSON STRING RECEIVED*****");
            System.out.println(jSonString);
            System.out.println("******************************");


            //Parse the JSON String to obtain a Vector of clients
            vClients = Client.fromJSONs(jSonString);

        } //whatever happens, close the streams
        finally {
            if (din != null) {
                din.close();
            }

            if (http != null) {
                http.close();
            }
        }

        return vClients;
    }
}


When we run the MIDlet the console prints the following lines:

*****JSON STRING RECEIVED*****
[{"name":"Alexis","lastName":"Lopez","id":"123456"},{"name":"Second","lastName":"Client","id":"987534"},{"name":"Colombia","lastName":"JUG","id":"555555"}]
****************************** *****CLIENT INFORMATION***** Client 1 name = Alexis Client 1 last name = Lopez Client 1 ID = 123456 Client 2 name = Second Client 2 last name = Client Client 2 ID = 987534 Client 3 name = Colombia Client 3 last name = JUG Client 3 ID = 555555


Obviously, in a business application the servlet would connect to a database in order to get the clients information, then the servlet would send that information to the mobile client using JSON. The mobile client would have a graphical interface that would allow the user to view the information received from the server... and so many other improvements. But this simple example, shows you the possibilities offered by JSON for communication between server and mobile client in a standard way without having to invent new formats for data exchange.

This concludes the communication Servlets/GSON vs JSON Me/Java Me. I know there have been a lot of code, it may be difficult to read. For the following posts I will emphasize on the really important parts of the code and let the entire source code to be downloaded.

see you soon!


References:

Using JavaScript Object Notation (JSON) in Java ME for Data Interchange. Agosto 2008. Oracle [online].
Available on Internet: http://java.sun.com/developer/technicalArticles/javame/json-me/
[accessed on October the 26th 2010].

meapplicationdevelopers: Subversion. 2007. Oracle [online].
Available on Internet: https://meapplicationdevelopers.dev.java.net/source/browse/meapplicationdevelopers/demobox/mobileajax/lib/json/
[accessed on October the 27th 2010].