Thursday, 24 February 2011

Operators used in java

Java provides rich operator enviroment. Most of it's operartors can be divided into the following four groups: Arithmetic, bitwise, relational and logical.

Arithmetic operators:
Arithmetic operators are used in mathematical expressions in same awy as they are used in algebra.
The following table lists the arithmetic operators:

Arithmetic Operators in java

The operands of the arithmetic operators must be of numeric type. you cannot use them on Boolean types, but you can use them on  char types. Since the char type in java is subset of int type.

Bitwise Operators:
Java defines several bitwise operators which can be applied to integer types, long,int,short,char and byte. These operators act upon individual bits of their operands.
They are summarized in following table:
                                                                                  Bitwise Operators in Java
Relational Operators:
The relational operators determine the relationship that one operand has to the other. Specifically they determine equlatity and ordering . The relational operators are shown here:

                                                                       Realationa Operators in Java
The outcome of these operators is a boolean value. The relational operators are most frequently used in the expressions that control the if statement and the various loop statements.
  Boolean Logical Operators:
The Boolean logical opertators are shown here operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value.

Boolean Logical operators

Wednesday, 23 February 2011

OOP Principles

All object-oriented programming laanguages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, and polymorphism.

ENCAPSULATION:
Encapsulation is the emchanism that binds together the code and the data it manuplates, and keeps safe from outside interference and misuse. It is a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well defined interface.

In java, the basis of encapsulation is Class. Since the purpose of a class is to encapsulate complexity. A class defines the structure and behaviour (code and data) that will be shaared by sset of objects . Each object of given class contains the structure and behaviour defined by the class. For this reason, objects are sometimes referred to as INSTANCES OF A CLASS. Thus, a class is a logical construct ; an object has physical reality.

INHERITANCE:
Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification

There is one SUPERCLASS and one SUBCLASS in the process of inheritance.
Superclass is the main class or Base class from which another class or Child class is inherited. and Subclass is the derived class wwhich inherits all the properties if the main class.

POLYMORPHISM:
Ploymorphism ( from the reek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions . The specific action is determined by the exact nature of the situation.
More generally. the concept of polymorphism is often expressed by the phrases, "one interface , multiple methods." This means it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is complier's job to select the specific action as it applies to each situation.

Example:
Consider a stack ( which is last in first out list ). you might have a program that requires three types of stacks. one stack is used for integer values , one for floating point values, and one for characters. The algorithm that implement each stack is same, even though the data being stored differs. In a non object oriented language, you would be required to create three different sets of stack routines , with each set using different  names. However, because of polymorphism, in java you can specify a general set of stack routines that all share the same names. 

Servlets

Servlets are miniature programs that must be run inside another program called a container. The container is responsible for brokering communication between the servlet and the outside world.
Apart from that servlets are like every Java Application. They can import packages, write to the hard drive, and even install viruses on your machine. By default, servlets are trusted code. The reason is that if someone has enough permission on your computer to install a servlet, he already has enough power to tie the whole machine in knots anyway.

Servlets are not java appliactions. They must run inside a container instead of form a command line.
Servlets are not the only way for the server to communicate with a browser.  A web Server is an HTTP application. It receives communication via the HTTP protocol and it communicates with the browser via HTTP also. You can write your own HTTP server-side application that processes its own requests, bypassing both servlets and servlet container


HOW SERVLETS WORK:
Servlets are really parts of an application and require a servlet container to run. This servlet container is responsible for instantiating your servlet and calling the appropriate methods in the servlet at the appropriate times.

When you type the name of a servlet, you are really making a call to a program that is located on a server and not on your machine.
Figure below shows the process graphically:

How Servlet works
 The Servlet container is responsible for instantiating your servlets.
  • You type in the URL of a servlet that you want to call.
  • The browser creates a request that contains the servlet and name of you machine so that the server will know who to send feedback to.
  • The server receives the request and hands it to the servlet container. The servlet container is a program that knows how to run servlets.
  • The servlet container checks to see if any instances of this servlet are already in memory. if not, it loads an instance and run the servlet init() method.
  • The servlet container waits for the init() method to finish. it then calls the service() method. in your servlet from a new thread.
  • The service() method calls the doGet() or doPost() method depending on what the request type is.
  • A second user's browser requests that the same servlet be run on its behalf.
  • The servlet container notices that an instance of the servlet is already in memory. so, it creates a new thread and starts running the same servlet that you are running.
  • This new thread calls the service() method.
  • If this is an HTTP servlet, the service() method calls the doGet() or doPost() methods.
  • The first thread finishes and a response is sent to  the web server, which forwards it to your browser.
  • The second thread finishes and a response is sent to the web server, which forwards it to the second user's browser.
  • At a certain point in future, the servlet container decides to deinstantiate the servlet. At that point it calls the destroy() method once for each instance in memory
The servlet lifecycle is represented in following figure.
Servlet Lifecycle



Tuesday, 22 February 2011

J2EE Components

J2EE applications are made up of components. A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with it's related classes and files and that communicates with other components. The J2EE specification defines the following J2EE components:
  • Application clients and applets are components that run on the client.
  • Java servlet and Java server pages(JSP) technology components are web component that run on server.
  • Enterprise Java Beans (EJB) components (enterprise beans) are business components that run on the server.

The structure of an HTTP response

An HTTP message sent by a server to a client is called an HTTP response.The initial line of HTTP response is called the status line. It has three parts, separated by spaces: the HTTP version, a response status code that tells the result of the request, and an english phrase describing the status code.

HTTP defines many status codes: common ones that you have noticed are 200 and 404. Here are two examples of a status line that could be sent in the response:
HTTP/1.0 200 OK
HTTP/1.0 404 Not Found

When the browser receives a status code that implies a problem, it displays an appropriate message to the user. if some data is associated with the response, headers like Content-Type and Content-Length that describe the data may also be present. A typical HTTP response looks like this:

HTTP/1.0 200 OK
Date: Tue, 10 jan 2002 23:56:12 GMT
Content-Type: text/html
Content-Length: 52

Monday, 21 February 2011

The Structure of HTTP Request

An HTTP message sent by client to server is called HTTP request.
The initial line for an HTTP request has three parts, separated by spaces.
  • A method name.
  • The local path of the requested resource(URI)
  • The version of HTTP being used.
A typical request is : GET /reports/sales/index.html HTTP/1.0

Here GET is the method name, /reports/sales/index.html is the resouce URI, and HTTP/1.0 is the HTTP version of the request.

The method name specifies the action that the client is requesting the server to perform.
HTTP 1.0 requests can have only one of the following three methods GET, HEAD, or POST.
HTTP 1.1 adds five more : PUT, OPTIONS, DELETE, TRACE, and CONNECT.

GET:
The HTTP GET method is used to retrieve a resource. It means " GET THE RESOURCE IDENTIFIED BY THIS URI". The resource is usually passive resource . A GET method request may be used for an active resource if there are few or no parameters to be passed.
If parameters are required, they are passed by appending a query string to the URI. For example,

GET method of HTTP Request
HEAD:
An HTTP HEAD request is used to retrieve the meta-information about a resource. Therefore, the response for a HEAD request contains only the header.
The Structure of HEAD request is exactly same as that of a GET request.
HEAD is commonly used to check the time when the resource was last modified on the server before sending it to the client. A HEAD request can save a lot of bandwidth, especially if the resource is very big.

POST:
A POST request is used to the server in order to be processed. It means, " POST THE DATA TO THE ARCHIVE RESOURCES IDENTIFIED BY THIS URI." The block of data is sent in message body.

HTML pages use POST to submit the HTML FORM data. Figure below shows the example of HTTP POST request generated by a typical form submission.

Example of HTTP POST request


PUT:
A PUT Request is used to add a resource to the server. It means"PUT THE DATA SENT IN THE MESSAGE BODY AND ASSOCIATE IT WITH THE GIVEN REQUEST-URI."

For Example, when we PUT a local file named sample.html to the server myhome.com using the URI http://www.myhome.com/files/example.html, the file becomes a resource on that server and is associated with the URI
http://www.myhome.com/files/example.html.






Introduction to J2EE architecture

J2EE is a technology that aims to simplify the design and implementation of enterprise applications

An enterprise application is an application which probably has legacy existing applications and database that you want to continue using them while adding or migrating to a new set of applications that exploit internet, e-commerce and other new technologies.