One to One Chat using sockets


Moving from the article Hello TCP sockets

We want to make a continuous one-to-one chat between two nodes
the two nodes at the beginning would be client and server
after connection establishment they should act similar as two nodes

Hello TCP sockets


A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.
It has two types UDP and TCP

  • UDP
    It’s a connectionless protocol.
    It doesn’t required initializing a connection before doing communication.
    there is no guarantee that the datagrams you have sent will be received in the same order by the receiving socket. You can’t know also if it’s received without defects or if it reached its destination
  • TCP
    It’s a connection-oriented protocol.
    a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client).
    It is guaranteed that the packets you send will be received in the order in which they were sent.


Start Activity For Result


We want to make an application that in some time needs to get image from phone
The ordinary thinking is to make a complex activity that searches for the images on the phone and displaying them in a list or grid and enabling selecting a photo
This is a hard option
another solution is to use intents to make so
We just would say “I want to pick an image” and the phone would Search for me for an application that can handle this request

We would use a sample activity that displays a text, if you touch screen you would be directed to pick an image and when you finished it would be displayed on your view.

Dynamic breadcrumb / navigation menu


A breadcrumb / navigation menu should track all the moves of the user from a page to another

the concept of making a dynamic navigation menu is to save every visited page in a list so that it contains all the visited pages from the home page until the current page
when i click on any menu item (return to a previous page on my navigation) i should clear all the pages after this page from the list.

user visits history should be stored among all user requests and should be unique to every user, so we would store navigation information in Session scope


Using Tag interface


The Tag interface is the main interface in custom tags
we can make our own custom tag by implementing this interface

When the container evaluates the custom tag, it first pass the PageContext object of the current page and the parent Tag of this custom tag.
Then the container calls the doStartTag that decides to include or skip the body of the tag then it calls the doEndTag that decides to continue parsing the rest of the page or to stop.
The tag is used many times by the container before it got out of service then the containers calls release method.


Hello JSF


Java Server Faces (JSF) is component-based framework that facilitates using HTML elements because you will not responsible anymore for writing parts of your HTML code

An example is to make a login page

we will start by making a bean represents our data
it will contain two properties represents the name and the password (two attributes, two setters and two getters )

also we’ll include a method that checks if that login details is valid or not
for simplicity we’ll choose a simple rule
if the name equals password then it’s correct login information
we can change this lines of code by what you want as for example connection to our database to check for these details

package beans;

public class User {

    private String name;
    private String pass;

    public void setName(String name) { = name;

    public void setPass(String pass) {
        this.pass = pass;

    public String getName() {
        return name;

    public String getPass() {
        return pass;

    public String check()
            return "welcome";
            return null;


there is something missing
till now it’s just a bean
we need to tell the framework that it’s a managed bean providing a name for it and a scope to be put into

Managed Bean
JSF framework uses some kind of beans called Managed Beans to store form data
so we will start to make our own ManagedBean


Discover remote bluetooth devices MIDlet


going from the previous sample
we are now going to make an advanced sample

we want MIDlet Application that can turn on/off Bluetooth discovery, and can make a search for devices and can stop the search any time

we need some attributes like a refrence to the local device and the discovery agent and a vector to store the discovered remote devices in.

also we will need commands to turn on/off discovery and one to start searching and another to stop it.

our class implements DiscoveryListener interface to listen to the actions generated by DiscoveryAgent
also it implements CommandListener to listen to the events of commands


Discover Remote bluetooth devices


To make a useful bluetooth client in J2ME it’s important to discover remote devices
first thing in making any bluetooth-enabled application is to get a reference to the Local device and after that turning on the bluetooth

        LocalDevice localDevice;
        try {
            localDevice = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException ex) {


Complex class


Operator overloading is one type of polymorphism
we can use different operators like =, >, + to act differently according to their operands (arguments)

suppose we will start with the complex class
which has two data members real represents the real part and img for the imaginary part
in addition to setters and getters for the data


Simple Web Service client using java


We are now going to make a simple connection to a web service

suppose we made a web service called Hello

the first thing we should have is an interface of that service class to be able to call its services using java code
After that we ‘d write some code to connect to that service

fortunately there is a tool in JDK called wsimport that can do all of that if you just provided it with a valid WSDL URL


Go to Top