Sunday, 20 April 2014

Creating Stateless Session Bean Using NetBeans 7.4 with Companion Video on YouTube

In this article we will learn how to create a Stateless Session Bean in NetBeans 7.4. There is a screen cast video for this article on YouTube (Creating Sample Stateless Session Bean in NetBeans 7.4)

Session beans are the part of Enterprise Java Beans (EJB) which are based on RMI as basic foundation technology. Good Introduction to Enrerprise Java Beans its types and application is been provided by Oracle Inc on its site at  J2EE Tutorial (PDF)

A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean like Remote Method Invocation , the bean’s instance variables may contain a state specific to that client but only for the duration of the invocation. When the method is finished, the client-specific state should not be retained.

Most Often Stateless Session beans are more scalable due to their stateless nature.It is to be noted that it is developers responsibility that he/she does not include any code which may turn out to be a non-transient data for clients request for invocation of session beans business method.

When Creating stateless session beans in NetBeans we need to follow the following steps in general.
As like RMI where every client has a copy of interface containing  remote method's declaration , Session Bean's Remote interface is distributed in form of Java Class Library , a .file jar. So before we start creating session bean its good we create a blank Java Class Library Project.

We will be creating a Session Bean with business method "int getSquare(int var)" which would return us square value of argument passed. Finally we will create a java servlet to access Session Bean's business method as a client.

Step 1: Create a blank Java Class Library Project. with a proper name and remember the name of the the project created you need select this project while creating Remote interface  for Session Bean in Step 2

Step 2: Create an Enterprise application with EJB Module and Web Project. Ensure that you are selecting GlassFish as a Server ,since Apache Tomcat does not come with EJB Container.

After you are done with creating  Enterprise application you will find that two projects have been created one with EJB module and Other with Web Module in which you write a Servlet to act as Client.

Add a new Session Bean to the EJB module , the type of Session bean should be Stateless and also create Remote and Local Interfaces for the created Bean

While creating Remote interface you need to select the Java Class Library Project which you have created in Step 1 to hold Remote interface which would be containing Business method's declaration.

 Add a Business method to your Session Bean

Step 4: Create a Servlet in Web Project and call EJB's Business method

Step 5: Deploy the Enterprise Project and call servlet in browser by using its context path.

Your Servlet's output should look somewhat like below screen

Happy Coding !!! :)

Tuesday, 15 April 2014

Creating your First Ruby on Rails (RoR) application using scaffolding on Windows with Rails in 7 Steps

We will use Bitnami Ruby stack on Windows Environment through out this article

If you are absolute beginner and need to start from knowing what is Ruby and Rails try below links  
Once you get acquaint with basic syntax of Ruby try out basic ruby code snippets in an interactive shell like web environment here at

Prerequisites for this article:
Bitnami Ruby Stack: (You can download the Bitnami installer at URL).

Once installed it starts appearing in your start menu as 
Start > All Programs > Bitnami Ruby Stack

Bitnami Ruby Stack is an Ruby environment with Rails and other software stacks like Apache , MySQL , PostgreSQL. It is good starting point when you start learning RoR development since here you need not struggle with RoR installation and other configurations, Remember that Bitnami Ruby Stack is for development purpose only. And I further feel that Production RoR environment should be using Linux and nginx as Web Container.
So lets start developing our first application in RoR by using scaffolding.

But what is Scaffolding in RoR ?? 
Rails scaffolding is a quick way to generate models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job to be get it done quickly. Scaffolding should be used as a starting point but for creating production scale application many developers prefer writing their own code. Let’s use scaffolding here to generate a simple application in which we will model an application which stores names and their emails.

Let’s begin
Step 1: Open your Ruby console
Go to Start > All Programs > Bitnami Ruby Stack > Use Bitnami Ruby Stack.

Step 2: You will find a console into the directory where you have installed Bitnami Ruby Stack.
In my case its “C:\Bitnami\rubystack-1.9.3-22”

type “ruby –v“ to get verison of Ruby and “rails –v ” to get version of Rails

Through this console you will be creating a new application and will also be generating scaffold application.

Step 3: Type following command to create a new Rails project. Ensure that at this point of time you have active internet connection.
rails new projects/emailcontactsapp
this command creates entire project structure specific to RoR in directory C:\Bitnami\rubystack-1.9.3-22\projects\emailcontactsapp and installs ruby gems required for the application . It stops for a moment , typically 5-10 minutes depending upon your internet bandwidth , till all required gem files are installed.
The output window after successful execution of command will be as

Step 4:  The command executed in Step 3 creates blank project structure with essential files for an RoR application. Once the project has been created its time to create a model and scaffold for the model created. We need to take note that RoR apps are MVC apps so creating model implies that you need to create View and Controller entities. Here since we are using scaffolding approach we only specify the model name and Controllers, Views are generated as to allow basic CRUD style operations.
Lets generate the model with name contacts which would store name and email, hence we will choose our database to contain two fields namely “Name as string” and “Email as string”
cd into the application directory with command
cd  projects\emailcontactsapp
And the command to generate scaffold will be
rails generate scaffold contacts Name:string Email:string
Your command prompt screen should look somewhat like screenshot below after the command successfully executes.

You can observe that we have specified that we are using scaffold and contacts as model name and “Name” of type string and “Email” of type string as two fields of database. 
Here it’s important to know that we are not creating database, it will be created in further steps by issuing command “rake db:migrate” . Also we are using default sqlite3 as database backend for our model. In case you wish to use Database servers like MySQL , Oracle , and others you need to install required gems and specify database connection and access credentials in file
<project-path>/config/database.yml. Let’s stick to sqlite3 as of now.  
Following are further types of fields supported in scaffolding 

Step 5: Now that we have generated Model, Views and Controllers , the only thing remaining is to create database  and appropriate fields in the database which were specified as a part of scaffold command. Inour case those fields were Name and Email.
Its quite easy just execute command
rake db:migrate
Output should be stating that “created migrating …..” look at the screenshot below

Step 6: Now its all done , just we need to start the development server and your contacts RoR app is ready and functioning
To start the development server type the command
rails server
Note down the port number at which development started , in most cases the port number would be 3000. In case if ports are already in use try killing the process which is using the concerned port and retry the command to start development server. 
Alternatively you can also specify the port number at which you intend to start the development server
by command rails server -p <port number>

You can see that in my case development server started at port number 3000
Let’s browse the URL http://localhost:3000/
you will find the default webpage which we are not interested rather we are interested in the Views created by scaffolding.
To view those Views
Go the URL http://localhost:3000/contacts
you will see that I appended the model name which I have created during Step 4.
Below are the screenshots when you visit various links on the application. You can see that you are able to add new contact , delete, modify, and show the contacts.
Step 7: To shutdown the development server hit Cntrl+C

Hope you got your first RoR app running !!! Happy coding !! 

Screenshots of output of application running on development server.