Thursday, May 30, 2013

Reading cookies from browser using core java

Cookie:

A cookie is a piece of information sent by server(web program) to client(browser).

A cookie will have a name and a value which will be stored in browser's cache.
There are two types of cookies available.

--> Persistent cookies and 
--> Non Persistent cookies

Persistent cookies will be stored in browser's cache even though we have closed the browser until some time specified in the web program in which they got created.

Non Persistent cookies will be removed from the browser's cache whenever we close the browser.

In JSE edition, java.net package provides the feature of reading cookies from browser.

This can be illustrated as shown.

CookieManager will manages all the cookie related tasks and will be helpful in getting cookies.

As part of CookieManager interface we have some final static fields available to set the cookie policy.

Content policy describes the acceptance state of cookies. i.e, which cookies to be accepted and which should be rejected.

The cookie policy has predefined policies namely ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER

ACCEPT_ALL is to accept all cookies , ACCEPT_NONE  is to reject all cookies and ACCEPT_ORIGINAL_SERVER is to accept cookies from original server.




A sample program to read the cookies stored in browser with java net package is as shown.


Once if you run the application, CookieManager will set the cookie policy to ACCEPT_ALL so that it can accept any cookie. A CookieHandler is used by the http protocol and can be registered by using setDefault().

Once we have opened the connection to the specified URL by using CookieManager we can get the CookieStore which contains a bunch of cookies related to the url. 

By using CookieStore we can get the list of cookies with their name and values

The output of the application will display list of cookies in the browser related to the url with cookie name and value.



Wednesday, May 29, 2013

Marker Interfaces , Serialization and serialVersionUID


Marker Interfaces:

An interface contains only abstract methods where as a marker interface contains no methods at all.

yeah, Marker inerface is an interface which doesn't have anything (variables or methods) in it.

examples:

Some example of Marker interfaces are..

java.io.Serializable,

java.lang.Cloneable,

javax.servlet.SingleThreadModel etc..


Why Marker Interfaces:

As Marker interfaces contains no methods why we need to use marker interfaces..? The answer is to instruct the compiler

A marker interface is used to give instructions to the java compiler. Classes that implement these marker interfaces  indicates their special behaviour to compiler.  

As of Java 1.4 Marker inerfaces exists . From Java 5 onwards, we have the concept of annotations which are used to give instructions to java compiler. So marker interfaces are deprecated later.

For e.g, if we write a class which implement Serializable interface (marker), that means we are giving instruction to compiler such that this class is eligible for serialization.


What is Serialization:

Serialization is the process of making an object to be persisted in a stream and can make that object to be tranferred through the network.

Not clear...

Here is the simple definition..

Writing (storing) an object into a stream (e.g file stream) is called Serialization.


Example:

Create a class SerialObject which implement Serializable interface. We can observe that compiler will show the warning The serializable class SerialObject does not declare a static final serialVersionUID field of type long




What is serialVersionUID:

A serialVersionUID is an unique id used by the compiler while in the process of deserialization.

By using serialVersionUID  compiler will check the class that was already serialized is same or not while deserialising it.

i.e to confirm that the class is same while serializing (writing object into stream) and deserialising (reading object from streams).

If we have not provided the serialVersionUID serialization run time will assume some id to the class.

However, as per JAVA specification it is strongly recommended to declare a serialVersionID which should be a static final of type long to avoid unexpected exceptions like InvalidClassException.

Also it should be a private member so that even it's sub classes can't access it.

Our SerialObject class with serialVersionUID and some properties is as shown.



We have declared transient variables, name and notTrransferable.

Transient Variable:

A transient variable is a variable that can't be transferred through a network or we can say , a variable which can't be serialized. That means, we can't write this variable into streams. 

To limit writing data into streams we will use transient variables in serializable classes.


Serializing :

SerializingApp is the class which is showing the process of serialization ( writing object content into file stream).

To write an object we will take the help of java.io.ObjectOutputStream which will accept java.io.OutputStream as an argument.


If the file is available in the specified location it will use that, otherwise FileoutputStream will create a file with the specified name in the specified location.

writeObject() is used to write the object's content into stream.

If we observe the output in the created file write_here.txt, it will only contain objectName and objectId since the remaining are transient and can't be transferred though a network (can't be serialised).


DeSerializing:

Reading the content from a stream and representing it again in the form of an object is called DeSerialization.

DeserialisngApp is the class which is showing process of reading the object from specified file (deserialization).

To write an object we will take the help of java.io.ObjectInputStream which will accept java.io.InputStream as an argument.


we need to specify the location of the file where we have stored our object (SerialObject) to the FileInputStream.

You can clearly observe that the class (SerialObject) which we have stored in the file stream in serialization is the same as the class which we got in deserialization.

serialVersionUID is used by the run time, to confirm that the class is same in Serialization and Deserialization.





Monday, May 27, 2013

Can we create object to Interface and Abstract class ..?


Yes...(but not directly)

We can create the object to An Interface / Abstract class.

As per the java specification we are not allowed to create object to interface and abstract class directly. But we can do that indirectly with the help of anonymous inner type.

Interface:

An interface is one which contains only abstract methods( methods don't have body).
Any variable declared inside an interface are by default public static final and should be initialised

Syntax:



Abstract Class:

An Abstract class is a class that can contain abstract methods or concrete methods( methods those have body) or both.

Syntax:



What happens when we try to create object:

We have an interface SampleInt which contains an abstract method.




SampleAbs is the abstract class with a concrete method showSampleAbs().




Now we are trying to create object to the above defined interface and abstract class in the class App




So , as the compiler is not allowing us to create the object to interface and abstract class we can say We can't create object to an interface or abstract class directly .


How to create object to interface and abstract class:

We can create the object to interface or abstract class indirectly by using anonymous inner type.

App class shows creating object to SampleInt and SampleAbs.




By providing anonymous inner class to Abstract class or interface we can say they have intantiated indirectly.

We can call showSampleAbs() by using SampleAbs object sampleAbs aswellas we can call sampleIntMethod() defined in interface by using it's instance sampleInt.


Calling methods defined inside anonymous type:


Suppose we have defined some more methods inside the implementation (anonymous class), then we can't call them by using instance since the methods not available at the time of object creation.




Calling the methods that are defined inside anonymous inner type will be as shown. 




Note:

Anonymous inner type is simply providing implementation to the corresponding abstract class / interface. 

So we are able to create object to abstract class and an interface by providing implementation in the form of anonymous inner type.

You can observe the behaviour by practising this code.