Saturday 10 November 2012

Web Servers

Hi, Today we have a quick look on how our web servers really works.We started with a very simple example which we use daily. Nowdays we are using a client-server model for our web applications.Our client means browser request to server for a particular query and according to that our server responds. When we put a URL(Uniform Resource Locator ) in our browser than how it actually works?
We have our URL such as:

http://www.google.com/index.html

Our web- browser divide that URL into three parts.
1. http: It is a protocol which provide connection between client and server. There are many more such as https for secure connection, ftp for file transfer,ftps for secure file transfer , mms for microsoft streaming and many more.
2.www.google.com: It is a address of the server by which our computer contacts when you try to access some information or a web page.
3.index.html: It is a resources which was requested by our computer.It is a particular web page.

So,when your type a URL and press enter than request is sent to the server in this manner.When server gets your request than usually server replies with HTML code.Our browser takes that HTML and interpreted with graphics and shows us the result.There are various server packages who serves for our web pages such as Apache.Our browser sends a standard request to server and our server serves according to the request in standard manner. Here we discuss about HTML respond which is static in nature but it could be a dynamic such as php,jsp etc.

Auto-Wiring in Spring

Auto-wiring is a way of resolving the beans that need to be injected by inspecting the elements in ApplicationContext. Auto-wiring reduces the configuration for the properties and constructor.If configuration is changed for the property bean than it automatically updated.There are 4 kind of auto-wiring :
1.No (Default).
2.byName.
3.byType.
4.Constructor.

In 'default' auto-wiring means there is no wiring and we are still using the ' ref ' keyword for connecting beans. In 'byName' auto-wiring Spring uses the property 'name' and searches a bean with same name as property and  wired it .In 'byType' auto-wiring Spring uses the property 'type' and searches beans with same type as our property and wired it.If there are more than one bean with same type than it will give us a error as :

" Error creating bean with name 'person' defined in class path resource [Dependency.xml]: Unsatisfied dependency expressed through bean property 'name': : No unique bean of type [studyMaterial.Name] is defined: expected single matching bean but found 2: [name1, name2];"

So when you are using 'byType' than there should be only one bean of that type.In the same way if we are using constructor and in our constructor we are using property than it can also be auto-wired by using '   'autowire ="constructor"'.Here is a simple picture for representing auto-wiring.

No auto-wiring:
Auto-wiring :

Here we can see by using 'autowire' we can reduce the configuration of our xml file. Person is a class having property 'name' which is a type of  'Name' , so Spring searches for 'Name' type and bean 'name1' is a Name type because we are implementing 'Name' interface in our 'studyMaterial.DisplayThisName' class.So it connect name1 with person implicitly.One more thing I like to share in this topic which is annotation based wiring, so lets have a look.


Annotation Based Bean-wiring: 

 

Till now for wiring we are using "autowire" attribute. Ok how It would be if didn't need that anymore in our xml file and Spring automatically wire your beans.sounds good......na, ya we can do it by using annotation in our application. Some things needed for configure the annotation.
1.We have to add some files such as :
  •  xmlns:context="http://www.springframework.org/schema/context" .
  •  http://www.springframework.org/schema/context .
  • http://www.springframework.org/schema/context/spring-context-2.5.xsd .
  •  <context:annotation-config/>
Here is our configured file for our person and name example,

                                                                                                 
Now we can see that we need no attribute for wiring it automatically searches for you beans and wired it.One more thing, in our Person class, just above the property declaration we have to put a "@Autowired" keyword just like as:


Now it is complete and you can see that still our program running successfully without using autowire keyword.So enjoy with annotations.


Your mail are always welcomed at java.ansh@gmail.com.


Lets Play with Spring Framework

Hi guys,now we are going to learn about Spring Framework .
Spring Framework is a open source framework so don't worry about money...hmm just kidding.So whats new in Spring lets talk about it...
Spring give us few features which are  really very interesting. but two of them means a lot.
1 . Loose Coupling, and
2 . Dependency Injection.
So lets discuss about each of them.
Since we are using MAVEN for the application development so please add the following dependency in your pom.xml file.

Loose Coupling:

 

For understanding loose coupling here we took an Example from our conventional JAVA programming. Suppose we have two classes Indian and Car such as :
Our Indian Class is such as:
 Our Car Class:


 Here we can see that  in our Indian class we are creating an instance of Car class by using the new operator and if somehow we wants to change our Car object  by any other object such as Bike , Bus , Motor  or any object than we need to change in our JAVA class which is not a better idea. We have to put Bike or anything else in place of Car. Such kind of situation is called tight coupling between the classes.Our Person class is tightly coupled with Concrete Car class.So Here Spring provides a better solution for that.Lets have a look:


Spring came with introducing a xml file.It is very simple to make a beam of a class and than call it in our Indian class such as:

 We write a ApplicationContext which tell us about the xml file  and by simply using refrence of this application context we can use our bean of car class by its ID name.So here we can see that there is no need to create a Car object by new operator in our Indian class.This work is now done by our xml file.But Still we are not getting fair enough results.So if we wants some more flexible and loosely coupled classes it is always a better idea to use Interface, for example if we want to use Bike move method instance of Car than first we should make a Interface Vehicles which was implemented by both of our Classes Car and Bike.
Indian Class :


Now if we look our Indian class than we can see that if we want to change our Class from Car to Bike than there is no nee to change in JAVA file. we can achieve it only by changing in XML file such as:
LooseCoupling.xml:


So by using Spring we can make our JAVA classes move independent and loosely coupled.
Here by using xml configuration we get the loose coupling.

Now have a look on second advantage:

Dependency Injection :

 

Suppose we have a person class which have a property name which was inherited from name class but for that we again create name class object and than we use that object for getting name.Spring came here with a solution so that there is no need to use the new operator in our JAVA class.
So here is our solution :
1.First we make a Interface of name which have a method   myName() which is used by a class DisplayThisName() and provides its implementation.
Name Interface:
DisplayThisName class:




2.Now in Our Person class we write the setter and getter methods for the values of name.
Person Class:


3.Now write the main class for executing the our application.Here is our main class:
 Main class:
 4.After writing JAVA classes we configure the xml files for all classes.As:
Dependency.xml 


This is all about the loose coupling and dependency injection later on we will discuss on bean wiring with annotation.


Friday 2 November 2012

Simple Chat box by using jQuery in JMVC framework

Ok so today we are going to make a simple Chat box by using jQuery.

Step 1: Create a apps by using C:/xampp/htdocs/jmvc> js  jquery\generate\apps mychat in console.

Step 2 : Generate the model by using C:/xampp/htdocs/jmvc> js  jquery\scaffold Mychat.Models.Chat in console.

Step 3 : Create the HTML in mychat.html file such as with body ID: #chat.


Step 4 : Write css for each class and ID for better look.

Step 5 : Write the controller logic in chat_controller.js file of controller folder of your apps such as:


There is nothing much about to discuss here, we introduce a new function here undelegate(), when out textarea1 or textarea2 are empty that at that time we do not want event hadler in our buttons, so this function simply remove the handler from the post button when our textarea is empty.

Step 6 : Now we connect our all files in mychat.js file by adding name 'chat' in controllers class (.controllers('chat')).

Step 7 : Now you can run your apps by opening mychat.html in any web browser.which look like as: