Saturday, September 3, 2016

Drag and Drop example in HTML5

HTML5 has made dragging and dropping objects from one place to another very easy.

We will see an example of the HTML5 API’s that can be used to drag and drop objects
  1. How to make the object draggable
    • For making object draggable, we need to first set the draggable attribute of that object to true.
    • Then use the onDragStart method to capture the data that needs to be dragged
  2. How to make the object Droppable
    • We use 3 methods that will be used to define a object is droppable
      • onDrop
      • onDragEnter
      • onDragOver

Please see the sample code


Author -

I am Ketan and have a blog KSCodes that is developed to share some of the common examples that we come across in web development. You can get examples on java tutorials and spring tutorials.

Saturday, March 12, 2016

Have you ever wish to learn about AngularJS framework for java web development?

Though online article directories and blogs of Java web development professionals are feed with distinct stories and articles about different products; still you can consider this post to learn every important fact about AngularJS. The author will explain the framework, its features, architecture, and the steps to build an AngularJS application. Read every point thoroughly to get a clear vision about the framework.

AngularJS is an open source javascript framework for web application. It was originally developed in 2009 by Misko Hevery and Adam Abrons. It can be added to an HTML page with a <script> tag.

Definition of AngularJS:

AngularJS is a structural dynamic web apps framework. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. Angular's data compulsory and dependency injection eliminate much of the code you generally have to write. And it all occurs within the browser, making it an excellent partner with any server technology.

Features of AngularJs:
  • AngularJS is a powerful JavaScript based web development framework which is useful to develop powerful web application.
  • AngularJS provides developers options to write client side application in a clean MVC architecture.
  • Application written in AngularJS is cross-browser compliant. AngularJS automatically handles JavaScript code for each browser.
  • AngularJS is open source, completely free, and used by thousands of developers around the world.

Overall, AngularJS is a framework to build large scale and high performance web application while keeping them as easy-to-maintain.

AngularJs is divided in three major parts which are called ng-directives. Those are listed below.
  1. ng-app - This directive defines and links an AngularJS application to HTML.
  2. ng-model - This directive binds the values of AngularJS application data to HTML input controls.
  3. ng-bind - This directive binds the AngularJS Application data to HTML tags.

Tuesday, August 25, 2015

Java program to convert numbers to words

Output -

78='Seventy Eight'
456='Four Hundred Fifty Six'
1,000='One Thousand '
99,999='Ninety Nine Thousand Nine Hundred Ninety Nine'
199,099='One Lakh Ninety Nine Thousand Ninety Nine'
10,005,000='One Crore Five Thousand '

Tuesday, June 23, 2015

JSch - Upload/download files from remote server

We will using JSch to connect our unix box. Once connected you should be able to upload or download the required files.

JSch is a pure Java implementation of SSH2. To know more click here.

For running below code you need to add jsh jar file (e.g. jsch-0.1.53.jar) in your applications classpath. Download jsch-0.1.53.jar from here.

Please see the self explanatory code below -

Tuesday, June 9, 2015

Serialization problem with Singleton

In previous example we have seen the basics of singleton. In this example we will see how Serialization can effect the Singleton design pattern.

Sometimes in distributed systems, we may need to implement Serializable interface in Singleton class so that we can store it's object state in a file system and retrieve it at later point of time. Let's take an exammple of Clipboard class which implements Serializable interface.

Output :
c1 hashCode = 28117098
c2 hashCode = 13495805

From the above hashCode output we can see the intent of singleton pattern is simply blown up, to overcome this problem we need to provide the implementation of readResolve() method as shown below.

Now if u see the hashCode output, it will be same.

The readResolve method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.

Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signature - ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;