Sunday 10 November 2013

Web Storage

Web storage is the simplest form of data storage. Data storage is a way to store data on user's hard drive. There are some non critical data such as user's preferences, recent searches etc. that do not need to be sent to the server or received from the server at every request. These non critical data can be stored at client side to boost the performance of the web site. There are three ways to store data on the client side: Web Storage, Web SQL Database and IndexedDB. In this post we will talk about Web storage.

Web Storage

In web storage a key is associated with a value. Though it looks like cookies, but it is more secure and faster than cookies. Unlike cookies, it allows to define a sessionStorage that only refers to the particular window/tab the user is currently in. Once window/tab is closed, the data disappears. Data doesn't stick around until the entire browser is closed.

There are two forms of Web storage: sessionStorage and localStorage.

When data is created in sessionStorage, it is available only to that window until the window is closed (or session is ended). So when you open another window on the same domain, previous session data will not be available. This way leak of session data is avoided across different windows. Let's see some examples of sessionStorage:

 sessionStorage.itemSelected = 'Game';
 alert("You have selected : "+ sessionStorage.itemSelected);

You can set data in sessionStorage using setItem method. The setItem method takes a key and a value.

 sessionStorage.setItem('itemSelected', 'Reading');
 alert("You have selected : "+ sessionStorage.itemSelected);

Data can be retrieved from sessionStorage using getItem method. The getItem method takes the key and returns the value related to that key.

sessionStorage.setItem('itemSelected', 'Reading');
alert(sessionStorage.getItem(itemSelected));

You can remove data from sessionStorage using removeItem, clear method or directly using deleter. The removeItem method takes the key and deletes the entry, where as clear removes all entries.

sessionStorage.removeItem('itemSelected');
sessionStorage.clear();

When data is put in localStorage, it becomes available on any other window on the same domain. Data in localStorage remains available until it is explicitly deleted either by the web application or by the user. You can get back your saved data in localStorage even if you close the browser or reboot your machine. This way you can persist data without the need of cookies. localStorage has the exact same API like sessionStorage. So if you want to keep data in localStorage, you can use it this way:

localStorage.setItem('itemSelected', 'Reading');
alert(localStorage.getItem(itemSelected));

Web storage is supported on all modern browsers. You can see it here. API signatures are simple. Web storage typically has a limit of 5 MB. If data size goes beyond 5 MB, browsers generally ask the users whether they want to permit the website to go beyond this limit. Another thing to remember when using Web storage, it only supports string-to-string mapping at present. You can use JSON.stringify and JSON.parse to overcome this difficulty.

Sunday 27 October 2013

Exciting features of Java 8

Java 8 is scheduled to be released in Q1 next year. It looks really promising with many exciting features such as uniform platform, Lambda, enhancement of core libraries with Lambda, new Date and Time API, compact profiles, Nashorn JavaScript Engine and many more. These new features in Java will bring a paradigm shift by providing many great possibilities of writing good, concise and elegant code.

In Java 8 we are going to see the platform unification in terms of code portability, commonality of APIs, common tooling. Nandini Ramani, VP of Java Development of Java Platform for Oracle in her keynote speech in JavaOne Strategy Keynote 9-22-2013 mentioned that Java 8 will focus on bringing together different implementations of Java under one platform. Language features of Java SE 8 and Java ME 8 will be similar. Peter Utzschneider, VP of Java Product Management for Oracle also mentioned in that talk that there will one type of Java Developer in future. Beyond Java 8, Java SE will be shrink enough to work on embedded and mobile devices.

Lambda is the another most exciting feature of Java 8, bringing closures to Java. As per Mark Reinhold, Chief Architect of the Java Platform Group:

Lambda is the single largest upgrade to the programming model. Ever. It's larger even than Generics. It's the first time since the beginning of Java that we've done a carefully coordinated co-evolution of the virtual machine, the language and the libraries, all together. Yet the result still feels like Java.

Lambda will reduce lot of boilerplate code, make the code concise and easy to understand. Core libraries of Java are enhanced by Lambda.

There will be new date/time API in Java 8. All the classes in java.time package are immutable and thread-safe. The date and time types include Instant, LocalDate, LocalTime, LocalDateTime and ZonedDateTime. There are also the Duration and Period types and additional value types include Month, DayOfWeek, Year, Month, YearMonth, MonthDay, OffsetTime and OffsetDateTime.

Compact profile of Java 8 will allow applications to use a subset of Java SE to run on resource-constrained devices. For example: an application that does not use the Swing/AWT/2D graphics stack can achieve considerable space savings by running on top of a Profile that does not include those APIs. Compact profile replaces CDC.

Nashorn is the new, lightweight, high-performance implementation of JavaScript integrated into the JDK. Nashorn is the successor to Rhino, with improved performance and memory usage. Nashorn will be made available to Java applications via the existing javax.script API. It will define a new command-line tool, jjs.

There are many other features, bug fixes and performance improvement in Java 8. Features list of Java 8 is available here. Java 8 Developer Preview is released and can be downloadable from here.