<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-5223788876950011016</atom:id><lastBuildDate>Mon, 26 Dec 2011 07:28:05 +0000</lastBuildDate><category>Personal</category><category>DU</category><category>Sorting</category><category>Decompiler</category><category>GWT</category><category>SQL</category><category>hashcode()</category><category>Cygwin</category><category>Media Queries</category><category>Overriding</category><category>uCertify</category><category>JBossWS</category><category>Hibernate</category><category>AJAX</category><category>Immutable</category><category>Threads</category><category>SCJP</category><category>pass-by-value</category><category>Tutorial</category><category>Delhi</category><category>RPC</category><category>Strings</category><category>C++</category><category>Collections</category><category>Sun CA</category><category>Chrome</category><category>RSL</category><category>OOPS</category><category>new year</category><category>Aadhar</category><category>Flex 3</category><category>DragNDrop</category><category>SSL</category><category>HTML5</category><category>Mobile</category><category>STM</category><category>JBoss</category><category>jQuery</category><category>Web Services</category><category>XML</category><category>Static Imports</category><category>Generics</category><category>NetBeans</category><category>Java</category><category>Google</category><category>Exception Handling</category><category>Open Source</category><category>Web-Apps</category><category>Double-checked Locking</category><category>Drupal</category><category>Tomcat</category><category>Firefox</category><category>Joomla</category><category>CSS3</category><category>equals()</category><category>Singleton</category><category>CMS</category><category>Sankalan</category><category>Authentication</category><category>Garbage Collection</category><category>Inner Classes</category><category>Interview Ques</category><category>JavaMail</category><category>JavaScript</category><category>Ant</category><title>Kreativity Personified</title><description>The creative side of me unleashed here....love to write about technology that excites and inspires me...</description><link>http://techno-cratic.blogspot.com/</link><managingEditor>noreply@blogger.com (Agraj)</managingEditor><generator>Blogger</generator><openSearch:totalResults>54</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/KreativityPersonified" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="kreativitypersonified" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-5992948521603658292</guid><pubDate>Sat, 17 Dec 2011 18:44:00 +0000</pubDate><atom:updated>2011-12-17T10:44:13.647-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">JavaScript</category><category domain="http://www.blogger.com/atom/ns#">HTML5</category><category domain="http://www.blogger.com/atom/ns#">DragNDrop</category><title>HTML5 Native Drag ‘n’ Drop</title><description>&lt;p&gt;HTML5 does not only mean a whole new world of tags (video, audio, header, footer, article, section etc.) and revolutionary technologies like &lt;a href="http://html5demos.com/geo"&gt;Geolocation&lt;/a&gt;, &lt;a href="http://www.html5rocks.com/en/tutorials/appcache/beginner/"&gt;Application cache&lt;/a&gt;, &lt;a href="http://www.canvasdemos.com/"&gt;Canvas&lt;/a&gt;, &lt;a href="http://dev.w3.org/html5/workers/"&gt;Web Workers&lt;/a&gt; etc. but it also brings along a rich set of JavaScript API to developers. &lt;/p&gt; &lt;p&gt;Today, we witness one of the interesting feature that it provides; the capability of dragging and dropping elements natively without using any JS framework. &lt;a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html"&gt;The HTML5 spec&lt;/a&gt; defines the new events that we should catch in order to handle operations like&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Start of drag operation ( &lt;strong&gt;dragstart&lt;/strong&gt; )  &lt;li&gt;Dragging content over another element ( &lt;strong&gt;dragover&lt;/strong&gt; )  &lt;li&gt;Entering the drag area of another element ( &lt;strong&gt;dragenter&lt;/strong&gt; )  &lt;li&gt;End of Drag operation ( &lt;strong&gt;drop&lt;/strong&gt; )&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;If you have worked with a &lt;strong&gt;RIA-friendly&lt;/strong&gt; language such as &lt;a href="http://www.adobe.com/devnet/actionscript.html"&gt;ActionScript&lt;/a&gt;, then you will found this to be very similar. Having said that, providing this drag and drop support in the browser itself is the reason behind the fame of HTML5.&lt;/p&gt; &lt;p&gt;Coming back, lets see some code in action. We have some images in our page which we want to drag and drop in div container. These images are tagged with class “&lt;strong&gt;dragMe&lt;/strong&gt;” (so that we can attach event listeners to them). Similarly, we have a div container in which we are going to drop these images with an id of “&lt;strong&gt;dropOnMe&lt;/strong&gt;”.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-6BZQkIZ8H5A/Tuzirqgt22I/AAAAAAAAA1I/DoXApKyJrJs/s1600-h/htmlCode%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="htmlCode" border="0" alt="htmlCode" src="http://lh3.ggpht.com/-DBStW3MlfTE/TuzitOjWm6I/AAAAAAAAA1Q/rAOVSs6sZhg/htmlCode_thumb%25255B3%25255D.png?imgmax=800" width="520" height="382"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Things to note &lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;draggable=true&lt;/strong&gt;&amp;nbsp; -&amp;nbsp; By default, images and links are draggable in nature but I have added this attribute just to convey that any HTML5 element can be selected, dragged and dropped if you add this attribute to it. Also add following CSS to make it work across different browsers&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-5Nq2dpAm-nI/Tuzit0St5XI/AAAAAAAAA1Y/W3nvfEen2pA/s1600-h/draggableTrueCss%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="draggableTrueCss" border="0" alt="draggableTrueCss" src="http://lh6.ggpht.com/-COZs9xDoa0Q/Tuzivf_Mr7I/AAAAAAAAA1g/9XlPbUdK4XI/draggableTrueCss_thumb.png?imgmax=800" width="244" height="127"&gt;&lt;/a&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Custom &lt;strong&gt;data-*&lt;/strong&gt; attributes&amp;nbsp; -&amp;nbsp; Again something, HTML5-blessed. It provides us the capability to define custom data-attributes on any element. Here we use it to define properties of individual images. More on custom data-attributes &lt;a href="http://html5doctor.com/html5-custom-data-attributes/"&gt;here&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Next, we add some event handlers to handle drag n drop related events we defined above:&lt;/p&gt; &lt;p&gt;1. &lt;strong&gt;Adding Drag Handlers&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/--yzabrkG90E/TuziwbzL2eI/AAAAAAAAA1k/r220lCkSxuY/s1600-h/dragStart%25255B8%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="dragStart" border="0" alt="dragStart" src="http://lh3.ggpht.com/-2SIYB0RxGz4/TuzixaclRAI/AAAAAAAAA1w/3CteoU3QNxE/dragStart_thumb%25255B6%25255D.png?imgmax=800" width="426" height="166"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;We took a hold of all the elements that are to be dragged and attach to each, a drag handler which will be called whenever you click and try to drag that elements. In the handler, we update the event’s&lt;a href="https://developer.mozilla.org/En/DragDrop/DataTransfer"&gt; dataTransfer&lt;/a&gt; object and set custom properties in the JavaScript object, which we defined in the markup&amp;nbsp; (yes you guessed it right, we are going to use them in the drop handler)&lt;/p&gt; &lt;p&gt;2. &lt;strong&gt;Adding Drop Handlers&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-4raVXKWGSv4/TuziyRfcrQI/AAAAAAAAA14/zMKTLVOgLyY/s1600-h/dropHandler%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="dropHandler" border="0" alt="dropHandler" src="http://lh5.ggpht.com/-ZRu9ndSrk8Y/Tuzi0AMYY9I/AAAAAAAAA2A/0wJ5XlcwwcU/dropHandler_thumb%25255B2%25255D.png?imgmax=800" width="520" height="204"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Note that we use both &lt;strong&gt;e.preventDefault()&lt;/strong&gt; and &lt;strong&gt;return false&lt;/strong&gt; in the function, to make it work across different browsers; this is needed to make sure that we cancel the default action that the browser will normally take, like surfing to that location (in case of a link). Apart from that, the code just retrieves the data set in dataTransfer object and display that using &lt;strong&gt;innerHTML&lt;/strong&gt; of the div.&lt;/p&gt; &lt;p&gt;3. &lt;strong&gt;Adding handlers for dragover &amp;amp; dragenter&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-UQdycuVIwMs/Tuzi042IjsI/AAAAAAAAA2I/Sk9UqQxR-x8/s1600-h/dragOverEnterHandlers%25255B7%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="dragOverEnterHandlers" border="0" alt="dragOverEnterHandlers" src="http://lh4.ggpht.com/-udts2OpE00U/Tuzi1vIRplI/AAAAAAAAA2Q/3UpmYnfgKgw/dragOverEnterHandlers_thumb%25255B5%25255D.png?imgmax=800" width="333" height="144"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Again for making the code work across different browsers (tested on Firefox, IE and Chrome), we need to use both preventDefault() and also return false from the handler in order to inform the browser that you can drop and release the dragged item on &lt;strong&gt;this &lt;/strong&gt;element for which the function is called.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;A Lot More…&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;..can be achieved like customizing the &lt;strong&gt;drag icon&lt;/strong&gt; and even drag and drop across different instances of same browser or even dragging from one browser and dropping in another browser. Stay tuned or check out the spec for more details.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Final Demo Looks like&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Too bad that I don’t have a proper hosting solution, so you have to do with images, until you are passionate enough to write the above code yourself.&lt;/p&gt; &lt;p&gt;Initial Screen&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-ZYVDT7qdd04/Tuzi3ia0wDI/AAAAAAAAA2Y/Cq5_FWCeaac/s1600-h/image%25255B14%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-LdFE7CcyPl0/Tuzi5HovYeI/AAAAAAAAA2g/RSzOfFHeH5E/image_thumb%25255B8%25255D.png?imgmax=800" width="441" height="274"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Dragging an Item&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-vykjTAsfk0Q/Tuzi6_SAubI/AAAAAAAAA2o/bhSS_ey1NNI/s1600-h/image%25255B7%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-V_j9MnBGTks/Tuzi8OqnPCI/AAAAAAAAA2w/FbjR7g-DTX4/image_thumb%25255B3%25255D.png?imgmax=800" width="435" height="255"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Dropped it !!&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-2MJilS2YbcI/Tuzi9jr0cFI/AAAAAAAAA24/_V6YSB4msi0/s1600-h/image%25255B12%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/-ld5lgoTowHU/Tuzi-4R8vhI/AAAAAAAAA3A/Fz9tTbxOoHo/image_thumb%25255B6%25255D.png?imgmax=800" width="437" height="263"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;We can add more spice to the tut by adding CSS and some other effects but in the hope that it would overshadow the concept, I emphasize on the minimal code needed for it.&lt;/p&gt; &lt;p&gt;Any suggestions, improvements or bugs are welcome as comments.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-5992948521603658292?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2011/12/html5-native-drag-n-drop.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-DBStW3MlfTE/TuzitOjWm6I/AAAAAAAAA1Q/rAOVSs6sZhg/s72-c/htmlCode_thumb%25255B3%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-2805654786408385353</guid><pubDate>Sat, 19 Nov 2011 16:27:00 +0000</pubDate><atom:updated>2011-11-19T08:32:53.071-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CSS3</category><category domain="http://www.blogger.com/atom/ns#">Media Queries</category><title>CSS3 Media Queries</title><description>&lt;p&gt;&lt;strong&gt;Need of Media Queries&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Gone were those days when designers had the liberty to design or layout a web page for fixed/minimum resolution as most people would just use their desktop for browsing the web. In this mobile/tablet era, we cannot assume that the desktop would be the only &lt;strong&gt;user agent&lt;/strong&gt;. A large number of users today use their “smartphones” or “tablets” for browsing the web and so it becomes crucial that your website looks as good when viewed from such a device.&lt;/p&gt; &lt;p&gt;Many websites today have different version of websites, one each for desktop and mobile. By detecting the user agent when the user visits the website for the first time, it can redirect them to the mobile version if he is browsing the web using his smartphone. But this solution comes with its own headache of maintaining separate presentation layers for separate mediums and fixing bugs in both !!&lt;/p&gt; &lt;p&gt;This is where Media Queries come into picture. They provide an elegant solution to the problem by providing the ability to switch the CSS based on the user agent or device’s width or orientation. In short, they make your web design adaptive and responsive to change. &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Wow, that’s new !!&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Not really !!. They were very much a part of CSS2 and HTML4 specification where they were limited to ‘print’ and ‘screen’ only (meaning using CSS2, you can only specify different style-sheets for rendering and printing a web page). But with CSS3, more media types are supported and different style-sheets can be applied based on&lt;/p&gt; &lt;ul&gt; &lt;li&gt;device-width and height  &lt;/li&gt;&lt;li&gt;viewport-width and height  &lt;/li&gt;&lt;li&gt;orientation  &lt;/li&gt;&lt;li&gt;aspect-ratio  &lt;/li&gt;&lt;li&gt;device aspect-ratio  &lt;/li&gt;&lt;li&gt;resolution etc..&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Without wasting more time, lets see an example. We will create a layout for a widescreen (desktop layout) and using media queries, we will change the layout and color the markup when we resize the browser (to simulate tablet and mobile size) &lt;/p&gt; &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-CkmWjXlXYPI/TsfZcxx08sI/AAAAAAAAA0U/oOmkrn9XJig/s1600-h/wideLayout3.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="wideLayout" border="0" alt="wideLayout" src="http://lh4.ggpht.com/-TTbQ69hM_RQ/TsfZdzZBAqI/AAAAAAAAA0c/Sx_sYjyvQSM/wideLayout_thumb1.png?imgmax=800" width="340" height="221" /&gt;&lt;/a&gt;     Layout for Wide Screen&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-FRKprleQfPQ/TsfZeuZTyrI/AAAAAAAAA0k/8blcRLX9WjM/s1600-h/tabletLayout2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="tabletLayout" border="0" alt="tabletLayout" src="http://lh3.ggpht.com/-QVDKuOr5SKs/TsfZfdn6XLI/AAAAAAAAA0s/PWr4Ye6l7rg/tabletLayout_thumb.png?imgmax=800" width="244" height="226" /&gt;&lt;/a&gt; Layout for Tablet or Not So Wide Screen&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-dbbEa7LxFpY/TsfZgVfbwQI/AAAAAAAAA0w/7-9gkuEnwmM/s1600-h/phoneLayout2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="phoneLayout" border="0" alt="phoneLayout" src="http://lh5.ggpht.com/-SjNxKutJkP4/TsfZhIeN-UI/AAAAAAAAA04/q0dPJeVOMxo/phoneLayout_thumb.png?imgmax=800" width="244" height="237" /&gt;&lt;/a&gt;  Layout for a Mobile Device&lt;/p&gt; &lt;p&gt;In this &lt;strong&gt;lame-example&lt;/strong&gt;, we have simple created a &lt;strong&gt;three-column layout&lt;/strong&gt; for a widescreen or desktop, and changed it to a &lt;strong&gt;two-column&lt;/strong&gt; layout for a tablet and coloured the sidebar blue (not to show my design skills of course,  but to distinguish between the layouts) and then a &lt;strong&gt;stacked layout&lt;/strong&gt; for a mobile device with red coloured div. and All this by simply switching the CSS for the same HTML file. All the code is explained below:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;index.html&lt;/strong&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;&lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;DOCTYPE&lt;/span&gt; &lt;span class="attr"&gt;HTML&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;meta&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="viewport"&lt;/span&gt; &lt;span class="attr"&gt;content&lt;/span&gt;&lt;span class="kwrd"&gt;="width=device-width, initial-scale=1"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;meta&lt;/span&gt; &lt;span class="attr"&gt;charset&lt;/span&gt;&lt;span class="kwrd"&gt;="UTF-8"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (min-width: 800px)"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet" &lt;/span&gt;&lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/desktop.css"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (min-width: 400px) and (max-width: 800px)"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/tablet.css"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (max-width: 400px)"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/phone.css"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="header"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Header &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="sideBar"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Side Bar &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="content"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Content &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="extra"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Extra &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;desktop.css&lt;/strong&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;div&lt;br /&gt;{&lt;br /&gt;   border: 1px solid;&lt;br /&gt;}&lt;br /&gt;body&lt;br /&gt;{&lt;br /&gt;   width:1000px;&lt;br /&gt;   margin: auto;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#sideBar&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   float:left;&lt;br /&gt;   width:250px;&lt;br /&gt;   height:600px;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#content&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   float:left;&lt;br /&gt;   width:448px;&lt;br /&gt;   height:600px;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#extra&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   margin-left:700px;&lt;br /&gt;   width:296px;&lt;br /&gt;   height:600px;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;tablet.css&lt;/strong&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;div&lt;br /&gt;{&lt;br /&gt;   border: 1px solid;&lt;br /&gt;}&lt;br /&gt;body&lt;br /&gt;{&lt;br /&gt;   width:700px;&lt;br /&gt;   margin: auto;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#sideBar&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   float:left;&lt;br /&gt;   width:200px;&lt;br /&gt;   height:600px;&lt;br /&gt;   background-color:blue;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#content&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   margin-left:202px;&lt;br /&gt;   height:600px;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#extra&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   clear:both;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;phone.css&lt;/strong&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;div&lt;br /&gt;{&lt;br /&gt;   border: 1px solid;&lt;br /&gt;}&lt;br /&gt;body&lt;br /&gt;{&lt;br /&gt;   width:350px;&lt;br /&gt;   margin: auto;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#sideBar&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   height:100px;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#content&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   height:100px;&lt;br /&gt;   background-color:red;&lt;br /&gt;}&lt;br /&gt;&lt;span class="rem"&gt;#extra&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;   height:100px;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;This HTML files simple contains 4 Div’s in the &lt;em&gt;body&lt;/em&gt; and &lt;em&gt;head &lt;/em&gt;contains links to some style-sheets with media queries using &lt;em&gt;link&lt;/em&gt; tag, lets see what these queries mean&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/desktop.css"&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (min-width: 800px)"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;It can be read as include the “&lt;strong&gt;desktop.css&lt;/strong&gt;” file if and only if the user agent has a screen and the minimum-width of the device is 800 pixels. Similarly,&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/tablet.css"&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (min-width: 400px) and (max-width: 800px)"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;it says that include the “&lt;strong&gt;tablet.css&lt;/strong&gt;” when the user agent has a screen and its width is between 400 to 800 pixels, something that we think should be suitable for a tablet. &lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/phone.css"&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (max-width: 400px)"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;On the same lines, in this rule, we ask the browser to use “&lt;strong&gt;phone.css&lt;/strong&gt;” if the width is less than 400 pixels. Note that these rules are mutually exclusive because we want them to be like so. Each rule must evaluate to a boolean value and depending on that value, the CSS file is downloaded to the client machine and is used for rendering the HTML markup.&lt;/p&gt;&lt;p&gt;You can also embed the media-query inline in the CSS as follows:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;style&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/css"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;       &lt;br /&gt; @media only screen and (max-width: 400px)&lt;br /&gt;  {&lt;br /&gt;   #content&lt;br /&gt;   {&lt;br /&gt;       height:100px;&lt;br /&gt;       background-color:red;&lt;br /&gt;   }&lt;br /&gt;}   &lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;style&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Orientation&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Similarly the CSS for phone.css can be broken down into two parts: phonePortrait.css and phoneLandscape.css by applying the following media queries&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (max-width=400px) and (orientation=portrait)"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="phonePortrait.css"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;media&lt;/span&gt;&lt;span class="kwrd"&gt;="only screen and (max-width=400px) and (orientation=landscape)"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="phoneLandscape.css"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Width Vs Device-width&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;"width" refers to the width of the viewport width (or the browser’s width) whereas the "device-width" refers to the width of the device (for desktop users, it is the dimensions of the monitor used, for mobile users, it is the dimensions of the smartphone used). Common sense says we should use device as the browser size may be less than the dimension of the device (esp on desktop)&lt;/p&gt;&lt;p&gt;The complication comes in case of mobiles, where the likes of iDevices and Androids incorrectly reports the viewport-width and also neglects the orientation of the device while calculating the width. To make things work, Apple introduced a new &lt;a href="http://developer.apple.com/library/IOs/#documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html"&gt;meta-tag: Viewport&lt;/a&gt; which is usually used as follows:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;meta&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="viewport"&lt;/span&gt; &lt;span class="attr"&gt;content&lt;/span&gt;&lt;span class="kwrd"&gt;="width=device-width, initial-scale=1"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;This instructs the device to report the viewport width equal to the actual width of the device and also take orientation into account while making that calculation. More details can be read &lt;a href="http://developer.apple.com/library/IOs/#documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Browser Support&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Surprisingly, majority of browsers do support media queries but for those who don’t, a &lt;strong&gt;JavaScript workaround&lt;/strong&gt; can be applied which based on the width of the browser, changes the &lt;em&gt;link&lt;/em&gt;’s &lt;em&gt;href&lt;/em&gt; value to point to the correct CSS file.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Spec&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;A lot more can be achieved by media queries, all of which has been laid out in the spec &lt;a href="http://www.w3.org/TR/css3-mediaqueries/"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The HTML &amp;amp; CSS used above in the example are merely for demonstrating the power of Media Queries and hence have been intentionally kept lame. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-2805654786408385353?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2011/11/need-of-media-queries-gone-were-those.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-TTbQ69hM_RQ/TsfZdzZBAqI/AAAAAAAAA0c/Sx_sYjyvQSM/s72-c/wideLayout_thumb1.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-4926878917844832634</guid><pubDate>Tue, 15 Nov 2011 18:07:00 +0000</pubDate><atom:updated>2011-11-15T10:07:42.064-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Personal</category><category domain="http://www.blogger.com/atom/ns#">DU</category><category domain="http://www.blogger.com/atom/ns#">Sun CA</category><title>Remembering the old Sunny Days</title><description>&lt;p&gt;Although my tenure with Sun Microsystems (now Oracle Corp), working as a &lt;a href="http://techno-cratic.blogspot.com/2008/01/my-sunny-days.html"&gt;Sun Campus Ambassador&lt;/a&gt; was numbered (just 6 months), but I consider that to be an important phase in my life, a beginning of a new journey, something that motivated me to work on open-source technology and learn new and exciting stuff. &lt;/p&gt; &lt;p&gt;The bad, rather the &lt;strong&gt;ugly&lt;/strong&gt; part is that I cannot access my &lt;a href="http://blogs.sun.com/agraj"&gt;Sun Blog&lt;/a&gt; anymore !! Yes, I have written to &lt;a href="http://www.oracle.com/index.html"&gt;Oracle&lt;/a&gt; about it, but nothing has happened so far. Still the sole purpose of writing that post was to re-live moments of the past and&amp;nbsp; thanks to &lt;a href="http://blogs.oracle.com/arungupta/"&gt;Arun Gupta&lt;/a&gt;, I’m re-posting one of my favourite &lt;a href="http://blogs.oracle.com/roller-ui/bsc/spider.jsp?entry=c1bc2259-3218-455e-a8f4-f93a39a26e6c"&gt;entries&lt;/a&gt; again: A poem for me from one of my favourite professor: &lt;a href="http://cs.du.ac.in/pkh.htm"&gt;Dr. P.K. Hazra&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Under the SUN-MICROSYS SunRay&lt;br&gt;Has bloomed a Lovely Rose,&lt;br&gt;Over Joyful in Java &amp;amp; Solaris&lt;br&gt;He is SUN CAMPUS AMBASSADOR Agraj.&lt;br&gt;Only one request I want to make&lt;br&gt;to the SUN's shining flower,&lt;br&gt;To remain in serene silence&lt;br&gt;During my lecture hour&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Quoted by Dr. P.K Hazra  &lt;p&gt;and As they say,&amp;nbsp; “I’m really gonna miss this place, I’m gonna my college days…”&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-4926878917844832634?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2011/11/although-my-tenure-with-sun.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-47250143867176024</guid><pubDate>Sat, 12 Nov 2011 09:51:00 +0000</pubDate><atom:updated>2011-11-12T09:18:01.090-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Mobile</category><category domain="http://www.blogger.com/atom/ns#">jQuery</category><title>jQuery Mobile: Hello Mobile World</title><description>&lt;p&gt;After more than one year of online-hibernation, I’m more than happy to be back and today would be writing about creating a basic mobile application using &lt;a href="http://jquerymobile.com/"&gt;jQuery Mobile&lt;/a&gt;.  &lt;/p&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;Overview&lt;/strong&gt; &lt;/u&gt; &lt;/p&gt;&lt;p&gt;jQuery Mobile provides a robust framework to develop mobile applications on the fly and is as simple as creating a HTML page and including jQuery mobile specific JS and CSS files. The framework is not only pretty straightforward to use, but also provides a familiar approach for web developers (who are already comfortable with HTML, CSS &amp;amp; Javascript/&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;). It also provides 5 predefined themes to choose from and recently &lt;a href="http://jquerymobile.com/themeroller/"&gt;ThemeRoller&lt;/a&gt; has been launched which can be used to create a custom theme for your app. Fortunately for you guys, I’m not going to rant about how awesome jQuery Mobile is, as all that can be found &lt;a href="http://jquerymobile.com/"&gt;here&lt;/a&gt;. :-)  &lt;/p&gt;&lt;p&gt;&lt;u&gt;&lt;strong&gt;Getting Started&lt;/strong&gt; &lt;/u&gt; &lt;/p&gt;&lt;p&gt;In this post, we will be creating a basic contacts app which looks like below:  &lt;/p&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/-bAmQiWpa4ck/Tr1yyOTC5YI/AAAAAAAAAzc/q6Wkfm5yd0o/s1600-h/homePage%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="homePage" border="0" alt="homePage" src="http://lh5.ggpht.com/-sXuIasEs5-8/Tr1yyw7QL1I/AAAAAAAAAzk/mZ3H6L1qFs0/homePage_thumb%25255B1%25255D.png?imgmax=800" width="183" height="244" /&gt;&lt;/a&gt;         &lt;a href="http://lh5.ggpht.com/-a-X3i3UQMhg/Tr1yz5MMSpI/AAAAAAAAAzs/aRxA4Yhg6Qk/s1600-h/allFriendsView%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="allFriendsView" border="0" alt="allFriendsView" src="http://lh6.ggpht.com/-imcnyMI3aj0/Tr1y1KCQjnI/AAAAAAAAAz0/lwb47a-r3Lg/allFriendsView_thumb.png?imgmax=800" width="173" height="244" /&gt;&lt;/a&gt;  &lt;/p&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/-LWCt9SXc1V8/Tr1y10isLdI/AAAAAAAAAz8/UjLY4JqHOI0/s1600-h/addNewFriend%25255B2%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="addNewFriend" border="0" alt="addNewFriend" src="http://lh4.ggpht.com/-h4RVq0SciqM/Tr1y2vxsO0I/AAAAAAAAA0E/NuGMvqDccJI/addNewFriend_thumb.png?imgmax=800" width="183" height="244" /&gt;&lt;/a&gt;  &lt;/p&gt;&lt;p&gt;Lets start by creating a basic html file (&lt;strong&gt;index.html&lt;/strong&gt;) for the home page/the first view and add the code below to it: &lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;&lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;DOCTYPE&lt;/span&gt; &lt;span class="attr"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;meta&lt;/span&gt; &lt;span class="attr"&gt;charset&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;My Friends&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="jquery-mobile/jquery.mobile-1.0a3.min.css"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/css"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="jquery-mobile/jquery-1.5.min.js"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/javascript"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &amp;lt;script src=&lt;span class="str"&gt;"jquery-mobile/jquery.mobile-1.0a3.min.js"&lt;/span&gt; type=&lt;span class="str"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="css/styles.css"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/css"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="page"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="page"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="header"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;My Friends&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="content"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;   &lt;br /&gt;       &lt;span class="rem"&gt;&amp;lt;!-- Logo --&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="logo"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="listview"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="allFriends.html"&lt;/span&gt; &lt;span class="attr"&gt;data-transition&lt;/span&gt;&lt;span class="kwrd"&gt;="pop"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;All Friends&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="addFriend.html"&lt;/span&gt; &lt;span class="attr"&gt;data-transition&lt;/span&gt;&lt;span class="kwrd"&gt;="fade"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Add Friend&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;       &lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="footer"&lt;/span&gt; &lt;span class="attr"&gt;data-position&lt;/span&gt;&lt;span class="kwrd"&gt;="fixed"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h4&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;span class="attr"&gt;&amp;amp;copy;&lt;/span&gt; Copyright 2011&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h4&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;In the &lt;em&gt;head&lt;/em&gt; section, we have included jQuery mobile specific CSS and JS files. We have also included our custom CSS file: &lt;strong&gt;styles.css&lt;/strong&gt; which is primarily used to position the logo for our application and consists of the following code:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;&lt;span &gt;#page div #logo {&lt;/span&gt;&lt;br /&gt;   width: 300px;&lt;br /&gt;   height: 100px;&lt;br /&gt;   background-image: url(../images/jQueryLogo.jpg);&lt;br /&gt;   background-repeat: no-repeat;&lt;br /&gt;   background-position: center 0px;&lt;br /&gt;   margin-top: 0px;&lt;br /&gt;   margin-right: auto;&lt;br /&gt;   margin-bottom: 0px;&lt;br /&gt;   margin-left: auto;&lt;br /&gt;&lt;span &gt;}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;The &lt;em&gt;body&lt;/em&gt; contains various div’s which form one view/page of the mobile app. The “page” div usually contains “&lt;a href="http://jquerymobile.com/demos/1.0rc2/docs/toolbars/docs-headers.html"&gt;header&lt;/a&gt;”, “content” and a “&lt;a href="http://jquerymobile.com/demos/1.0rc2/docs/toolbars/docs-footers.html"&gt;footer&lt;/a&gt;”. These div’s can then contain anything that you want to put inside those sections of the page (following some conventions).&lt;br /&gt;&lt;/p&gt;&lt;p&gt;As the name suggests, header and footer are the blocks at top and bottom of the page and the “content” div is used to lay out the elements to be shown in the rest of page. Here, we show a logo and  list view of 2 elements.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;As you might have noticed we have made extensive use of &lt;a href="http://dev.w3.org/html5/spec/elements.html#embedding-custom-non-visible-data-with-the-data-attributes"&gt;HTML5 custom data-attributes&lt;/a&gt; (the data-* attributes on HTML tags) throughout our tutorial. jQuery mobile leverages this uber cool feature of HTML5 to add capabilities to its framework. These custom data-attributes (like data-role, data-transition, data-position etc) are defined in jQuery Mobile framework and support predefined values.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Lets create another pages/html files, which we will then link to our main page (index.html). Creating the second view/page (allFriends.html) in which we will show all the friends in our list.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;&lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;DOCTYPE&lt;/span&gt; &lt;span class="attr"&gt;HTML&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;meta&lt;/span&gt; &lt;span class="attr"&gt;charset&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;All Friends&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="jquery-mobile/jquery.mobile-1.0a3.min.css"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/css"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="jquery-mobile/jquery-1.5.min.js"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/javascript"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &amp;lt;script src=&lt;span class="str"&gt;"jquery-mobile/jquery.mobile-1.0a3.min.js"&lt;/span&gt; type=&lt;span class="str"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="page"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="header"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="index.html"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="home"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Home &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; All Friends&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="index.html"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="back"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Back &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="content"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="listview"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face1.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Andrea &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Office Colleague &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                       &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face2.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Aria &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Childhood Friend &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face3.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Tom &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Brother &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face4.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Home &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Danger &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face5.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Melinda &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Close Friend &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face6.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Taanya &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; College &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face7.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Micheal &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Rockstar &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="friendDetails.html"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;img&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="images/face8.jpg"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Maria &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h3&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Friend &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;p&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                 &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="footer"&lt;/span&gt; &lt;span class="attr"&gt;data-position&lt;/span&gt;&lt;span class="kwrd"&gt;="fixed"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;       &lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="navbar"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="rem"&gt;&amp;lt;!-- Add icons here for Delete, Edit, Call Friend --&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="#"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="info"&lt;/span&gt; &lt;span class="attr"&gt;data-iconpos&lt;/span&gt;&lt;span class="kwrd"&gt;="top"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Edit&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="#"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="delete"&lt;/span&gt; &lt;span class="attr"&gt;data-iconpos&lt;/span&gt;&lt;span class="kwrd"&gt;="top"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Delete&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="#"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="alert"&lt;/span&gt; &lt;span class="attr"&gt;data-iconpos&lt;/span&gt;&lt;span class="kwrd"&gt;="top"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Call&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;--&lt;/span&gt; /&lt;span class="attr"&gt;navbar&lt;/span&gt; &lt;span class="attr"&gt;--&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;--&lt;/span&gt; /&lt;span class="attr"&gt;footer&lt;/span&gt; &lt;span class="attr"&gt;--&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span class="kwrd"&gt;   &amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;By writing this small piece of code, we can show the thumbnails in the &lt;em&gt;listview&lt;/em&gt; along with some description of each element. To make things simpler, this is simply some static data hardcoded into HTML. A real-world application would read this data from some persistence store and using jQuery would create this markup dynamically with that data.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;jQuery Mobile also provides a rich set of form elements as can be read &lt;a href="http://jquerymobile.com/demos/1.0rc2/docs/forms/forms-all.html"&gt;here&lt;/a&gt;. We create a simple form using the html below (&lt;strong&gt;addFriend.html&lt;/strong&gt;) to show some basic form elements to create the third page/view of our app in which we provide a simple form to add a new friends in the existing contacts.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span &gt;&lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="html"&gt;DOCTYPE&lt;/span&gt; &lt;span class="attr"&gt;HTML&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;meta&lt;/span&gt; &lt;span class="attr"&gt;charset&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Add Friend&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="jquery-mobile/jquery.mobile-1.0a3.min.css"&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;="stylesheet"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/css"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt; &lt;span class="attr"&gt;src&lt;/span&gt;&lt;span class="kwrd"&gt;="jquery-mobile/jquery-1.5.min.js"&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text/javascript"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;script src=&lt;span class="str"&gt;"jquery-mobile/jquery.mobile-1.0a3.min.js"&lt;/span&gt; type=&lt;span class="str"&gt;"text/javascript"&lt;/span&gt;&amp;gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;script&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;head&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="page"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="header"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="index.html"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="home"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Home &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Add Friend &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="index.html"&lt;/span&gt; &lt;span class="attr"&gt;data-icon&lt;/span&gt;&lt;span class="kwrd"&gt;="back"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; Back &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="content"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;form action=&lt;span &gt;”submit.jsp”&lt;/span&gt;&lt;/span&gt; &lt;span class="attr"&gt;method&lt;/span&gt;&lt;span class="kwrd"&gt;="post"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="fieldcontain"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt; &lt;span class="attr"&gt;for&lt;/span&gt;&lt;span class="kwrd"&gt;="name"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name: &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="name"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="name"&lt;/span&gt; &lt;span class="attr"&gt;placeholder&lt;/span&gt;&lt;span class="kwrd"&gt;="Enter Name"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="fieldcontain"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt; &lt;span class="attr"&gt;for&lt;/span&gt;&lt;span class="kwrd"&gt;="gender"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Gender:&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;select&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="gender"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="gender"&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="slider"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="MALE"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Male&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="FEMALE"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Female&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;option&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;select&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="fieldcontain"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt; &lt;span class="attr"&gt;for&lt;/span&gt;&lt;span class="kwrd"&gt;="age"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Age&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;label&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="range"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="age"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="age"&lt;/span&gt; &lt;span class="attr"&gt;min&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt; &lt;span class="attr"&gt;max&lt;/span&gt;&lt;span class="kwrd"&gt;="100"&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;="0"&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;fieldset&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;="ui-grid-a"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;="ui-block-a"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="index.html"&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="button"&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="cancel"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Cancel&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;class&lt;/span&gt;&lt;span class="kwrd"&gt;="ui-block-b"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;button&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="submit"&lt;/span&gt; &lt;span class="attr"&gt;data-theme&lt;/span&gt;&lt;span class="kwrd"&gt;="a"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Submit&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;button&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;               &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;           &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;fieldset&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;form&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt; &lt;span class="attr"&gt;data-role&lt;/span&gt;&lt;span class="kwrd"&gt;="footer"&lt;/span&gt; &lt;span class="attr"&gt;data-position&lt;/span&gt;&lt;span class="kwrd"&gt;="fixed"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;       &lt;br /&gt;       &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h4&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;span class="attr"&gt;&amp;amp;copy;&lt;/span&gt; Copyright 2011&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h4&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Again the markup is pretty much similar to a normal HTML page, just we have used certain specific &lt;em&gt;data-roles&lt;/em&gt;, specific &lt;em&gt;classes&lt;/em&gt; which are defined in the framework to layout the elements in a certain fashion and apply a theme to our UI. Since this is just for introducing the framework and not on developing a fully functional app, so the form submission does not work, rather it gives an elegant error message on click of submit button as we have not provided a &lt;em&gt;submit.jsp&lt;/em&gt; page to handle the request. (Done intentionally to show the elegant error handling of the framework)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Single-page template Vs Multiple-page templates &lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Here we have used one html file per page. We also have an option to keep multiple pages inside one html file. In that case, we need to give id to our pages and link them to each other accordingly using that id. But that can soon explode &amp;amp; become unmanageable, and is only recommended when there is some static data to be shown&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;CSS Layout Magic&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;We didn’t write much CSS still our HTML pages looks just like a mobile site and the “header”, “footer” and “content” data-roles are layout perfectly nice. It’s so because of the framework’s CSS file (jquery.mobile-1.0a3.min.css) which we have included in our HTML pages. Again proves what amazing things CSS can do :-)  &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Dreamweaver Integration with jQuery Mobile&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Adobe has done a fantastic job in integrating jQuery Mobile with &lt;a href="http://www.adobe.com/products/dreamweaver.html"&gt;Dreamweaver&lt;/a&gt; and provides several templates to get you started. The editor also supports some level of code-completion for jQuery Mobile and even some support for Javascript.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Want to learn more ?&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Even after writing such a long post, its hard to explain each line of code written above so the best way to learn more is to refer to the &lt;a href="http://jquerymobile.com/demos/1.0rc2/"&gt;Docs&lt;/a&gt;. Or I might just write another post to explain some more.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is just an introduction to jQuery Mobile and might not follow the best practices like normally you would refer to jQuery framework files from some CDN than to include them with your own code. Also, the above code is static in nature, but it can be easily integrated with any database using some script and can form a fully-fledged application.&lt;/p&gt;&lt;p&gt;As always, comments and corrections are welcomed and appreciated.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-47250143867176024?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2011/11/jquery-mobile-hello-mobile-world.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-sXuIasEs5-8/Tr1yyw7QL1I/AAAAAAAAAzk/mZ3H6L1qFs0/s72-c/homePage_thumb%25255B1%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-3806098644175944878</guid><pubDate>Fri, 24 Sep 2010 17:39:00 +0000</pubDate><atom:updated>2010-10-21T09:11:53.782-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Flex 3</category><category domain="http://www.blogger.com/atom/ns#">RSL</category><title>Runtime Shared Library (RSL) – Flex 3</title><description>&lt;p&gt;The best way to reduce the size of your &lt;a href="http://www.adobe.com/products/flex/?sdid=GXZOA"&gt;Flex&lt;/a&gt; application (or the resultant SWF file)  is to use &lt;strong&gt;Runtime Shared Library&lt;/strong&gt; which uses the concept of dynamic linking.&lt;/p&gt;  &lt;p&gt;Normally a Flex application consists of your application code as well as the framework code and the both of these codes are &lt;strong&gt;statically merged&lt;/strong&gt; together in a single &lt;strong&gt;SWF&lt;/strong&gt; file. It makes the resultant size of your application (the SWF file) larger by adding some 500 KB’s of data to it, which has to be downloaded each time the application is downloaded to a client.&lt;/p&gt;  &lt;p&gt;On the other hand, RSL allows you to &lt;strong&gt;decouple&lt;/strong&gt; your application code with the framework code and allows you to &lt;strong&gt;dynamically link&lt;/strong&gt; both of them as and when needed (while running the application). Without wasting time, lets try  and configure our project to use RSL instead of static linking. It can be done either using command line Compiler Options or by changing certain settings in Flex Builder. We’ll go the easy way:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Setup in Flex Builder&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Following Steps are needed to Compile and Build a Project in Flex Builder with RSL in it:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Select your Flex Project. Click on Project –&amp;gt; Properties &lt;/li&gt;    &lt;li&gt;Go to Flex Build Path &lt;/li&gt;    &lt;li&gt;Open Library Path Tab. By default the framework linkage is statically linked --&amp;gt; Merged into Code as shown below: &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TJzt3sqSOWI/AAAAAAAAAoQ/wOPbjb86j-A/s1600-h/clip_image002%5B7%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="clip_image002" alt="clip_image002" src="http://lh5.ggpht.com/_QosbmeyDVxU/TJzt4Z9vdwI/AAAAAAAAAoU/RqFhTg5cp0M/clip_image002_thumb%5B4%5D.jpg?imgmax=800" width="420" border="0" height="384" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Change the drop down to Runtime Shared Library (RSL) for dynamic binding.&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TJzt5Tgju2I/AAAAAAAAAoY/gXC9d8v-hDY/s1600-h/clip_image004%5B7%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="clip_image004" alt="clip_image004" src="http://lh6.ggpht.com/_QosbmeyDVxU/TJzt6O91FgI/AAAAAAAAAoc/fzz7us0t770/clip_image004_thumb%5B4%5D.jpg?imgmax=800" width="416" border="0" height="368" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Make sure that Verify RSL digests checkbox is checked. Click Ok to complete the changes and the project will be recompiled with updated compiler settings.&lt;/p&gt;  &lt;p&gt;This will create 2 files for &lt;a href="http://livedocs.adobe.com/flex/3/html/help.html?content=rsl_01.html"&gt;RSL&lt;/a&gt; in the Output Folder of your project:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;framework_3.X.X.XXX.swf (Unsigned/Failover RSL) &lt;/li&gt;    &lt;li&gt;framework_3.X.X.XXX.swz (Signed RSL) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;By default, the Flash player will try and use the Signed RSL to link with your application code on loading but the signed RSL works only with &lt;strong&gt;Flash Player 9.0.115&lt;/strong&gt; and later. If your clients are using a lower version, then it will be unable to load the &lt;strong&gt;Signed RSL (swz)&lt;/strong&gt; and will automatically switch over to the &lt;strong&gt;failover RSL (swf)&lt;/strong&gt; and use it in place of Signed RSL.  &lt;/p&gt;  &lt;p&gt;A Major benefit of using Framework RSL is &lt;strong&gt;caching&lt;/strong&gt;. RSLs are cached when they are first used. When they are needed by another application, they can be loaded from the cache rather than across the network. The signed framework RSL (RSL’s can be signed by Adobe only) can be cached in the &lt;strong&gt;Flash Player cache&lt;/strong&gt; as well. This benefits by reducing the initial download time as there is no need to download the framework code every time you need to run your Flex Application. And since the caching is done in the special cache of the Flash Player and not in the browser cache, so this signed RSL can be used for some other Flex Application (served from a different domain than yours) On the other hand, the failover RSL is cached in the &lt;strong&gt;browser’s cache&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;u&gt;Compiler Settings&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Apart from RSL, while building the project for Production Environment, the debug option must be set to &lt;strong&gt;false&lt;/strong&gt;, else the resultant SWF file will also contain the debug information, which is unnecessary for the production environment. To do so, right click the project, go to Project Properties and open the Flex Compiler View and add&lt;/p&gt;  &lt;p&gt;&lt;b&gt;-debug=false&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;in Additional Compiler Arguments as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/TJzt7D9-7bI/AAAAAAAAAog/k3kzM6c7jaY/s1600-h/clip_image002%5B8%5D%5B5%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="clip_image002[8]" alt="clip_image002[8]" src="http://lh4.ggpht.com/_QosbmeyDVxU/TJzt777YZKI/AAAAAAAAAok/KRn-BJ5Z-m0/clip_image002%5B8%5D_thumb%5B3%5D.jpg?imgmax=800" width="422" border="0" height="312" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Without doing this also, it will work but the resultant SWF file will have debug information with it. The default value of this parameter is true.&lt;/p&gt;  &lt;p&gt;Go Check the output folder of your web application and you will definitely see a reduced SWF file there Happy Flexing :-)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-3806098644175944878?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/09/runtime-shared-library-rsl-flex-3.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_QosbmeyDVxU/TJzt4Z9vdwI/AAAAAAAAAoU/RqFhTg5cp0M/s72-c/clip_image002_thumb%5B4%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-7011733214753983068</guid><pubDate>Fri, 20 Aug 2010 18:07:00 +0000</pubDate><atom:updated>2010-08-20T11:17:09.516-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Threads</category><category domain="http://www.blogger.com/atom/ns#">Generics</category><title>Thread-safe Generic Class in Java</title><description>&lt;p&gt;Assuming that you are a Java Developer, I can say that you have at some point or the other must have used &lt;strong&gt;Generics&lt;/strong&gt; in your code, be it using a Generic Collection (if you have worked with &lt;strong&gt;Java5+&lt;/strong&gt;) or in any other way. Even if you are not a Java developer, you must have played with &lt;strong&gt;Templates&lt;/strong&gt; (in C++) in your school/college days. &lt;/p&gt;  &lt;p&gt;Today, we are going to do something similar in Java. We are going to create a &lt;strong&gt;Generic Stack&lt;/strong&gt; class in Java, which you can use to create a Stack of Integers, Floating Point Numbers, or a Stack of Students or any custom type that you have. Just to make code a bit more interesting and tough, I have also made it&lt;strong&gt; thread-safe&lt;/strong&gt; so that multiple threads can use this stack without leaving it in an inconsistent state.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:375910a4-3b99-4810-bb8f-3752bf7db606" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;public class GenericStack&amp;lt;T&amp;gt; {&lt;br /&gt;&lt;br /&gt;private List&amp;lt;T&amp;gt; items;&lt;br /&gt;private int top;&lt;br /&gt;private int size;&lt;br /&gt;private static final int INIT_SIZE = 100;&lt;br /&gt;&lt;br /&gt;public GenericStack(){&lt;br /&gt;    size = INIT_SIZE;&lt;br /&gt;    items = new ArrayList&amp;lt;T&amp;gt;(INIT_SIZE);&lt;br /&gt;    top = -1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public GenericStack(int size){&lt;br /&gt;    this.size = size;&lt;br /&gt;    items = new ArrayList&amp;lt;T&amp;gt;(size);&lt;br /&gt;    top = -1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private boolean isEmpty(){&lt;br /&gt;    return (top==-1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private boolean isFull(){&lt;br /&gt;    return (top == size-1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt; // Push an Item in Stack&lt;br /&gt; public void push(T item){&lt;br /&gt;    synchronized(items){&lt;br /&gt;       // While stack is Full&lt;br /&gt;       while(isFull()){&lt;br /&gt;          try{&lt;br /&gt;             // Wait till Stack is Full&lt;br /&gt;             items.wait();&lt;br /&gt;          }&lt;br /&gt;          catch(InterruptedException e){&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;          }&lt;br /&gt;       }&lt;br /&gt;       // Produce Item&lt;br /&gt;       items.add(++top, item);&lt;br /&gt;       // Notify other threads&lt;br /&gt;       items.notify();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt; // Pop an item out of stack&lt;br /&gt; public T pop(){&lt;br /&gt;     T item = null;&lt;br /&gt;     synchronized(items){&lt;br /&gt;        // While Stack is Empty&lt;br /&gt;        while(isEmpty()){&lt;br /&gt;           try{&lt;br /&gt;             // Wait if its empty&lt;br /&gt;             items.wait();&lt;br /&gt;           }&lt;br /&gt;           catch(InterruptedException e){&lt;br /&gt;             e.printStackTrace();&lt;br /&gt;           }&lt;br /&gt;        }&lt;br /&gt;        // Consume an item&lt;br /&gt;        item = items.get(top--);&lt;br /&gt;        // Notify other threads&lt;br /&gt;        items.notify();&lt;br /&gt;     }&lt;br /&gt;     return item;&lt;br /&gt;  }&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;Most of the above code is self-explanatory but lets highlight some of the major concepts used here:&lt;ul&gt;  &lt;li&gt;To create a Generic class, one must specify the &lt;strong&gt;Type&lt;/strong&gt; along with the class name (as done using &lt;strong&gt;T&lt;/strong&gt;).  &lt;/li&gt;&lt;li&gt;When you initialize it, all the occurrences of &lt;strong&gt;T&lt;/strong&gt; will be replaced by that particular type. &lt;/li&gt;&lt;li&gt;We have also made the above Stack class &lt;strong&gt;thread-safe&lt;/strong&gt;, i.e. it can be shared and updated by multiple threads in a consistent manner using &lt;strong&gt;synchronized&lt;/strong&gt; blocks. &lt;/li&gt;&lt;li&gt;We have checked before &lt;strong&gt;pushing&lt;/strong&gt; for a &lt;strong&gt;Full Stack&lt;/strong&gt; and we also check for an &lt;strong&gt;Empty Stack&lt;/strong&gt; before &lt;strong&gt;popping&lt;/strong&gt; an element &lt;/li&gt;&lt;li&gt;We have not made the &lt;strong&gt;isFull()&lt;/strong&gt; and &lt;strong&gt;isEmpty()&lt;/strong&gt; methods as synchronized as they are &lt;strong&gt;private&lt;/strong&gt; (hence not visible to clients) and are always called from an synchronized context from within the class. &lt;/li&gt;&lt;li&gt;If you need isFull() and isEmpty() to be &lt;strong&gt;public&lt;/strong&gt; then make them also &lt;strong&gt;synchronized&lt;/strong&gt; to make them thread-safe &lt;/li&gt;&lt;li&gt;It is always better to have synchronized blocks rather than synchronized methods as it is always better to synchronize as less code as possible. Although, here we could not make much use of this concept but rather than obtaining the &lt;strong&gt;lock&lt;/strong&gt; on the calling object, we obtain a lock on the shared list of items. &lt;/li&gt;&lt;li&gt;Calling &lt;strong&gt;synchronized(items)&lt;/strong&gt; means if some other thread has not yet acquired a lock on items (By the way, each Object in Java has an associated Lock with it), then this current thread acquires the associated &lt;strong&gt;lock&lt;/strong&gt; and proceeds. Otherwise if some other thread has currently acquired the synchronized block, then it must wait in the &lt;strong&gt;wait list&lt;/strong&gt; of the object (items) in question. The thread then waits until it receives a &lt;strong&gt;notification&lt;/strong&gt; that the lock has become available for acquisition and then it &lt;strong&gt;competes&lt;/strong&gt; with other &lt;strong&gt;waiting threads&lt;/strong&gt; to acquire the lock. &lt;/li&gt;&lt;li&gt;Calling &lt;strong&gt;items.wait()&lt;/strong&gt; results in the current thread being placed in the wait list of items after releasing the lock. You can only call object.wait() after you have acquired the lock on the object. &lt;/li&gt;&lt;li&gt;Calling &lt;strong&gt;items.notify()&lt;/strong&gt; results in sending the notification to one of the threads in the wait pool of threads which can then compete for the lock. &lt;/li&gt;&lt;li&gt;Note that &lt;strong&gt;items.notify()&lt;/strong&gt; does &lt;strong&gt;NOT&lt;/strong&gt; &lt;strong&gt;releases&lt;/strong&gt; the lock. Only exiting the synchronized block/method releases the lock that was acquired when the thread entered the synchronized block/method. &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;You can easily create two or more threads (viz. Producer and Consumer) and pass them the same Stack and play with them to randomly create and consume data and check that you Stack class is indeed thread-safe.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Generic Methods&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Talking more about generics, you can also make a &lt;strong&gt;method&lt;/strong&gt; accept &lt;strong&gt;Generic parameters&lt;/strong&gt; without the enclosing class declaring the Generic type as done in the following example:&lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:752048ee-87aa-4ab5-9e87-2bc2407c7355" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;public &amp;lt;T&amp;gt; List&amp;lt;T&amp;gt; createArrayList(T t){&lt;br /&gt;  List&amp;lt;T&amp;gt; arr = new ArrayList&amp;lt;T&amp;gt;();&lt;br /&gt;  return arr;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This &lt;strong&gt;weird&lt;/strong&gt; looking syntax has a meaning:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;This method basically lets you create an arraylist of any type, type is passed to it as an argument.&lt;/li&gt;&lt;li&gt;Since you are not defining the type at Class-level, then there must be a place where you should define the &lt;strong&gt;type&lt;/strong&gt; of your method and &lt;strong&gt;distinguish&lt;/strong&gt; it from other non-generic methods for the compiler. &lt;/li&gt;&lt;li&gt;So, we declare the type&lt;strong&gt; &amp;lt;T&amp;gt;&lt;/strong&gt; immediately after &lt;strong&gt;public&lt;/strong&gt; modifier to say that we use a &lt;strong&gt;Generic Type T&lt;/strong&gt; in this method, which like others will be replaced at runtime with any real type. (just like in case of Generic classes) &lt;/li&gt;&lt;li&gt;We call this method passing to it a valid reference to any Object of any type (Integer, Float, or you own custom class Student). For example, we can do:&lt;/li&gt;&lt;/ul&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:65e66dfc-3757-49eb-90ba-30643c853218" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span"   style=" white-space: normal;  font-family:Georgia, serif;font-size:16px;"&gt;&lt;div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:da7b8fb9-88dc-4070-ac0c-9c7992ee2188" class="wlWriterEditableSmartContent" style="padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px; "&gt;&lt;pre class="brush: java;"&gt;Integer i = 23;&lt;br /&gt;List&amp;lt;Integer&amp;gt; intList = createList(i);&lt;/pre&gt;&lt;/div&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;or&lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:da7b8fb9-88dc-4070-ac0c-9c7992ee2188" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span"  style="font-size:medium;"&gt;Student st = new Student();&lt;br /&gt;// Do anything with st here&lt;br /&gt;List&amp;lt;Student&amp;gt; students = createList(st);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Ok, I agree this is not one of really useful examples for showcasing the power of a generic method but I hope that the intent is clear and so is the syntax. As always, your valuable comments are welcome and desired. Any corrections must also found place in comments. Sayonara :-)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-7011733214753983068?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/08/thread-safe-generic-class-in-java.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-1862080923046957640</guid><pubDate>Sun, 15 Aug 2010 08:03:00 +0000</pubDate><atom:updated>2010-08-20T10:36:23.335-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Generics</category><title>Mixing Generic &amp; Non-generic code in Java</title><description>&lt;p&gt;Can you spot the error in the following code:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:0a0e3f43-7abf-492b-8be9-acb32a69164b" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;public class MixingGenerics{&lt;br /&gt;&lt;br /&gt;public static void main(String[] args){&lt;br /&gt;    List&amp;lt;Integer&amp;gt; arr = new ArrayList&amp;lt;Integer&amp;gt;();&lt;br /&gt;    arr.add("String 1");&lt;br /&gt;    arr.add("String 2");&lt;br /&gt;&lt;br /&gt;   // add something more&lt;br /&gt;   addToList(arr);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void addToList(List arr){&lt;br /&gt;   arr.add(new Integer(1));&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I’m sure you are able to point out the mistake that we are adding an &lt;strong&gt;Integer&lt;/strong&gt; to an ArrayList of &lt;strong&gt;Strings&lt;/strong&gt;. That’s definitely not done. But can you tell without compiling this code that we get an error at &lt;strong&gt;compile-time&lt;/strong&gt; or at&lt;strong&gt; run-time&lt;/strong&gt; ?&lt;/p&gt;&lt;p&gt;Unfortunately and even more &lt;strong&gt;surprisingly&lt;/strong&gt;, this code doesn’t give any compilation error or runtime exception. Everything works fine and an Integer is added into your ArrayList (of Strings) !! Yes, you do get a &lt;strong&gt;compiler warning&lt;/strong&gt; that says something about &lt;strong&gt;Type Safety&lt;/strong&gt; of your code. We will get back to this point later on. &lt;/p&gt;&lt;p&gt;But first examine how is possible that you can actually store an Integer in a String Arraylist? Actually, it is an unfortunate outcome of &lt;strong&gt;mixing Generic &amp;amp; Non-generic code&lt;/strong&gt;. The reason why we  get no error is due to a property of Generics called &lt;strong&gt;Type-Erasure&lt;/strong&gt;. We’ll get to it soon but first lets see when the previous code will throw an Error/Exception. This code will explode as soon as you would try and iterate the ArrayList of Strings, in which you have put an Integer unknowingly as shown below:&lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:a07ecee1-29c1-4fc1-88bd-f5a1a0674453" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;public class MixingGenerics{&lt;br /&gt;&lt;br /&gt;public static void main(String[] args){&lt;br /&gt;    List&amp;lt;Integer&amp;gt; arr = new ArrayList&amp;lt;Integer&amp;gt;();&lt;br /&gt;    arr.add("String 1");&lt;br /&gt;    arr.add("String 2");&lt;br /&gt;&lt;br /&gt;   // add something more&lt;br /&gt;   addToList(arr);&lt;br /&gt;&lt;br /&gt;  // print the list&lt;br /&gt;  printMyList(arr);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void addToList(List arr){&lt;br /&gt;   arr.add(new Integer(1));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void printMyList(List&amp;lt;String&amp;gt; arr){&lt;br /&gt;   for(String s:arr){&lt;br /&gt;       System.out.println("String : "+s);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So we get a runtime exception now on iterating the list. It prints the first two strings and then throws &lt;strong&gt;ClassCastException&lt;/strong&gt; as we are trying to cast an Integer into a String s. That’s okay but why does it allows you to add an Integer into List of Strings. &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Type-Erasure&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Generic code in Java exhibit a very important property: Type-erasure. What it means is that the so-called type-safe collection of yours (the generic collection here: &lt;strong&gt;ArrayList&amp;lt;String&amp;gt;&lt;/strong&gt;) loses its &lt;strong&gt;type information&lt;/strong&gt; at run-time and acts as if its just a List (like pre-Java 5 lists, the non-generic list). This has been made such so that we can integrate pre-Java 5 (the non-generic code) with newly written Generic code (Java 5 or later), but then while mixing them, you must be careful. Very careful indeed, else you could happen to write a code that will explode unpredictably.&lt;/p&gt;&lt;p&gt;So the &lt;strong&gt;answer&lt;/strong&gt; to our question that why it allows an Integer to a list of Strings is because at runtime we only have a list, just like we had a list in pre-Java 5 days. Without generics, we had to take care while taking object out of the list and casting them but with Java 5 and above code, that casting is implicitly inserted by the compiler. So when we say &lt;/p&gt;&lt;p&gt;String s = arr.get(0) gets converted into String s = (String) arr.get(0) &lt;/p&gt;&lt;p&gt;Doing exactly the same with our list, it gives an error when we try and cast an Integer into a String and hence the error: &lt;span style="color:#ff0000;"&gt;java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Apart from giving the error, t&lt;span style="color:#000000;"&gt;he compiler does his best and warns you when you are trying to mix Generic code with Non-Generic Code. &lt;/span&gt;&lt;span style="color:#000000;"&gt;If you are very sure about your code and do not want any compiler warning, then you can do by using following Annotation (valid with Java5+ code)&lt;/span&gt;&lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:31baa196-ad65-49f3-9a39-62d62f349a52" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: java;"&gt;&lt;span class="Apple-style-span"  style="font-size:large;"&gt;@SuppressWarnings("unchecked")&lt;br /&gt;public static void addToList(List arr){&lt;br /&gt;arr.add(new Integer(1));&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This lets the compiler know that you are pretty sure about the code that you have written and do not give me any warnings with this method. Generally, it is not recommended to use such Annotations and if you really need to use one, you must do proper documentation of your code. &lt;/p&gt;&lt;p&gt;Any comments and corrections are welcome :-)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-1862080923046957640?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/08/mixing-generic-non-generic-code-in-java.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-954363143802612187</guid><pubDate>Thu, 15 Jul 2010 17:45:00 +0000</pubDate><atom:updated>2010-10-21T09:14:10.574-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Authentication</category><category domain="http://www.blogger.com/atom/ns#">Web-Apps</category><category domain="http://www.blogger.com/atom/ns#">JBoss</category><title>JBoss – Basic &amp; Digest Authentication</title><description>&lt;p&gt;This blog entry is a replication of my previous blog entry: &lt;a href="http://techno-cratic.blogspot.com/2010/05/tomcat-container-managed-security.html"&gt;Tomcat – Container Managed Security&lt;/a&gt;. It is extending the same entry for JBoss, so I’m gonna skip all the introduction and straight-away get to work.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Basic Authentication&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; Open the &lt;strong&gt;login-config.xml&lt;/strong&gt; file located at &lt;strong&gt;${JBOSS_HOME}/server/${USER_PROFILE}/conf&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;This file sets up the configuration for the &lt;strong&gt;security domains&lt;/strong&gt; available to applications running in the server. &lt;strong&gt;JBoss&lt;/strong&gt; uses &lt;a href="http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/guide/security/jaas/JAASRefGuide.html"&gt;JAAS&lt;/a&gt; for the underlying security infrastructure, and JAAS interacts with a security store for authenticating credentials. &lt;/p&gt;  &lt;p&gt;In simple words, we specify an &lt;strong&gt;application policy&lt;/strong&gt; in this file, which tells the Login Module that we will use (Like Tomcat, here also we have plenty of options like &lt;strong&gt;LDAP&lt;/strong&gt;, &lt;strong&gt;Database&lt;/strong&gt;, &lt;strong&gt;Property Files&lt;/strong&gt; etc for Login Modules). Basically, Login Module specifies the usernames, passwords and roles used for protecting your application. To start with, we will use &lt;strong&gt;UsersRolesLoginModule&lt;/strong&gt;, which uses a &lt;strong&gt;property file&lt;/strong&gt; to specify all the above mentioned information. Obviously, this isn’t secure and not really a choice for a system in production. Time to make changes:&lt;/p&gt;  &lt;p&gt;Make changes in &lt;strong&gt;login-config.xml&lt;/strong&gt; file : Add the following policy at the end of the file:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/TD9JSwlX04I/AAAAAAAAAks/Uu6jyQXuxFc/s1600-h/ApplicationPolicy%5B5%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="ApplicationPolicy" alt="ApplicationPolicy" src="http://lh6.ggpht.com/_QosbmeyDVxU/TD9JTpD0DpI/AAAAAAAAAkw/c_wk2A3P29g/ApplicationPolicy_thumb%5B3%5D.jpg?imgmax=800" width="435" border="0" height="284" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The "&lt;strong&gt;name&lt;/strong&gt;" attribute on the "&lt;strong&gt;application-policy&lt;/strong&gt;" element specifies the name of the &lt;strong&gt;security domain&lt;/strong&gt;.  This name is important because it is what will be used to tie the security domain to the web application later. We will mention it in application’s web.xml.  &lt;/li&gt;    &lt;li&gt;The "&lt;strong&gt;login-module&lt;/strong&gt;" element specifies the login module that this domain will use. &lt;/li&gt;    &lt;li&gt;The "&lt;strong&gt;module-option&lt;/strong&gt;" elements specify some values to pass into the login module's "initialize" method. For UserRolesLoginModule, we will specify the property files for user information and role information. These paths are relative to the &lt;strong&gt;${JBOSS_HOME}/server/${USER_PROFILE}/conf&lt;/strong&gt; directory.  &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;2. Open the &lt;strong&gt;${jboss.dist}/server/${&lt;/strong&gt;&lt;a href="http://server.name/"&gt;&lt;strong&gt;server.name&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;}/conf/props&lt;/strong&gt; and &lt;strong&gt;create 2 files&lt;/strong&gt;:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;web-users.properties&lt;/strong&gt;      (user information) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;web-roles.properties&lt;/strong&gt;       (role information) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;In web-users.properties property file, add&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/TD9JUSrCWZI/AAAAAAAAAk0/5nEiUv16tKU/s1600-h/userNamePassword%5B5%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="userNamePassword" alt="userNamePassword" src="http://lh4.ggpht.com/_QosbmeyDVxU/TD9JW9tpG2I/AAAAAAAAAk4/6gksQJzpZoE/userNamePassword_thumb%5B3%5D.jpg?imgmax=800" width="307" border="0" height="77" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In web-roles.properties, add&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/TD9JXeEdD2I/AAAAAAAAAk8/UCrRO5S-q-U/s1600-h/userRoleInfo%5B3%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="userRoleInfo" alt="userRoleInfo" src="http://lh3.ggpht.com/_QosbmeyDVxU/TD9JYbKKNVI/AAAAAAAAAlA/hhWkh0T5xWE/userRoleInfo_thumb%5B1%5D.jpg?imgmax=800" width="297" border="0" height="72" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Open &lt;strong&gt;web.xml&lt;/strong&gt; of your web application and make following changes -- add following &lt;strong&gt;security constraint&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TD9JZXrEjCI/AAAAAAAAAlE/qrV0OgwDax4/s1600-h/securityConstraints%5B5%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="securityConstraints" alt="securityConstraints" src="http://lh6.ggpht.com/_QosbmeyDVxU/TD9JaXa9RuI/AAAAAAAAAlI/U5BHUiHw3cA/securityConstraints_thumb%5B3%5D.jpg?imgmax=800" width="434" border="0" height="327" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Open &lt;strong&gt;jboss-web.xml&lt;/strong&gt; of your web application and add following line inside the &lt;strong&gt;&amp;lt;jboss-web&amp;gt;&lt;/strong&gt; element:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TD9KA9a5aZI/AAAAAAAAAlM/ANuWkY7hPUo/s1600-h/JAASSettigns%5B4%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="JAASSettigns" alt="JAASSettigns" src="http://lh3.ggpht.com/_QosbmeyDVxU/TD9K9ai-t2I/AAAAAAAAAlQ/Et6cuO_C2iI/JAASSettigns_thumb%5B2%5D.jpg?imgmax=800" width="401" border="0" height="54" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This element tells &lt;strong&gt;JBoss&lt;/strong&gt; to connect the web application to the "&lt;strong&gt;webPasswordProtection&lt;/strong&gt;" security domain (application policy) we defined in the &lt;strong&gt;login-config.xml&lt;/strong&gt; file earlier. JBoss exposes security domains via &lt;a href="http://java.sun.com/products/jndi/"&gt;JNDI&lt;/a&gt; by pre-pending "&lt;strong&gt;java:/jaas/&lt;/strong&gt;" to the name element in the &lt;strong&gt;application-policy&lt;/strong&gt; element in the login-config.xml file.&lt;/p&gt;  &lt;p&gt;Voila!! You are done now. Just restart the JBoss Server and your authentication should work fine. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;DIGEST Authentication&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Having already said that this basic authentication is not recommended for production purposes, we will try using a different authentication mechanism : Digest Passwords. We will save the passwords in a digest (&lt;strong&gt;hashed&lt;/strong&gt;) form in the properties file instead of saving them in clear-text form. Make following changes:&lt;/p&gt;  &lt;p&gt;1. Change in &lt;strong&gt;login-config.xml&lt;/strong&gt; file&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TD9K-FbA89I/AAAAAAAAAlU/M0Poo5yMxhU/s1600-h/Digest%5B3%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="Digest" alt="Digest" src="http://lh5.ggpht.com/_QosbmeyDVxU/TD9K_G-FZnI/AAAAAAAAAlY/ruTOeOhSZ3M/Digest_thumb%5B1%5D.jpg?imgmax=800" width="398" border="0" height="76" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;2. Find the new &lt;strong&gt;hashed&lt;/strong&gt; password using built-in RFC2617Digest class:&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TD9LAQVc8gI/AAAAAAAAAlc/db6C2HXChEk/s1600-h/digestpassword%5B5%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="digestpassword" alt="digestpassword" src="http://lh6.ggpht.com/_QosbmeyDVxU/TD9LBPeudqI/AAAAAAAAAlg/b3DfaMGY3E8/digestpassword_thumb%5B3%5D.jpg?imgmax=800" width="442" border="0" height="105" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;RFC2617Digest&lt;/strong&gt; class is present in jbosssx.jar which is present in &lt;strong&gt;${JBOSS_HOME}/server/default/lib&lt;/strong&gt; directory. So this jar file must be on your classpath when you execute the above command. If it is not so, then use &lt;strong&gt;–cp&lt;/strong&gt; option of java tool to set CLASSPATH as done above. The generated password is &lt;strong&gt;b63289befb5c9bb0509eed20b253e985&lt;/strong&gt;. Note that the arguments passed to this class are &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;User Name &lt;/li&gt;    &lt;li&gt;Realm Name &lt;/li&gt;    &lt;li&gt;Password &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;in that order. Now is the time to store this &lt;strong&gt;hashed password&lt;/strong&gt; in your &lt;strong&gt;web-users.properties&lt;/strong&gt; file.&lt;/p&gt;  &lt;p&gt;3. Change &lt;strong&gt;web-users.properties&lt;/strong&gt; file as follows:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/TD9LB8_veiI/AAAAAAAAAlk/LKX_oYkSmc4/s1600-h/digestpass%5B4%5D.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="digestpass" alt="digestpass" src="http://lh5.ggpht.com/_QosbmeyDVxU/TD9LC9G-SSI/AAAAAAAAAlo/hs4uWv0j6oI/digestpass_thumb%5B2%5D.jpg?imgmax=800" width="304" border="0" height="80" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, just restart JBoss to see the effect. And it is more secure than Basic authentication as you are not transmitting the password over the wire.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-954363143802612187?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/07/jboss-basic-digest-authentication.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_QosbmeyDVxU/TD9JTpD0DpI/AAAAAAAAAkw/c_wk2A3P29g/s72-c/ApplicationPolicy_thumb%5B3%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-7752603950735640660</guid><pubDate>Sun, 27 Jun 2010 18:54:00 +0000</pubDate><atom:updated>2010-07-04T11:55:27.483-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SSL</category><category domain="http://www.blogger.com/atom/ns#">Web-Apps</category><category domain="http://www.blogger.com/atom/ns#">Tomcat</category><title>SSL Configuration in Tomcat</title><description>&lt;p&gt;Today, we are going to learn about how to setup &lt;strong&gt;SSL&lt;/strong&gt; on your &lt;strong&gt;Tomcat&lt;/strong&gt; so that you can access your application on &lt;strong&gt;HTTPS&lt;/strong&gt; instead of &lt;strong&gt;HTTP&lt;/strong&gt; and provide a &lt;strong&gt;secure interface&lt;/strong&gt; to your clients. But before going into that, lets understand the basic &lt;strong&gt;difference&lt;/strong&gt; between HTTP &amp;amp; HTTPS. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Introduction to HTTPS&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;HTTPS &lt;/strong&gt;&lt;em&gt;(&lt;a href="http://en.wikipedia.org/wiki/HTTP_Secure"&gt;HyperText Transfer Protocol Secure&lt;/a&gt;)&lt;/em&gt; combines &lt;strong&gt;HTTP&lt;/strong&gt; protocol with &lt;strong&gt;SSL &lt;/strong&gt;&lt;em&gt;(&lt;a href="http://en.wikipedia.org/wiki/SSL"&gt;Secure Sockets Layer&lt;/a&gt;)&lt;/em&gt; or &lt;strong&gt;TLS &lt;/strong&gt;&lt;em&gt;(&lt;a href="http://en.wikipedia.org/wiki/Transport_Layer_Security"&gt;Transport Layer Security&lt;/a&gt;)&lt;/em&gt; and offers a secure channel to browse the web so that you can trust the connection to send &lt;strong&gt;sensitive&lt;/strong&gt; &lt;strong&gt;information&lt;/strong&gt; like &lt;em&gt;payment transactions&lt;/em&gt; details or your &lt;em&gt;bank account details&lt;/em&gt;. The difference between the two clearly shows in the address bar where you can see &lt;strong&gt;“&lt;em&gt;https://&lt;/em&gt;”&lt;/strong&gt; instead of &lt;strong&gt;“&lt;em&gt;http://&lt;/em&gt;”&lt;/strong&gt;. Also HTTPS uses port &lt;strong&gt;443&lt;/strong&gt; as the default port, whereas HTTP uses port &lt;strong&gt;80&lt;/strong&gt; as the default port for communication.&lt;/p&gt;  &lt;p&gt;Using HTTPS, the two-way communication that occurs between your web browser and the host server is now &lt;strong&gt;secure&lt;/strong&gt; and &lt;strong&gt;encrypted&lt;/strong&gt;. This means that the data being sent is encrypted by one side, transmitted, then decrypted by the other side before processing. &lt;/p&gt;  &lt;p&gt;When a web browser initially attempts to contact a web server over a secure connection, it is presented with a “&lt;strong&gt;certificate&lt;/strong&gt;” (set of credentials), as a proof of the site is who what it claims to be. These certificates are &lt;strong&gt;signed&lt;/strong&gt; (digitally verified) by certain &lt;strong&gt;authorities&lt;/strong&gt; like VeriSign and typically purchased by the web server. Remember that you need to buy a certificate for each port on which you want a secure connection. For this blog entry, we will sign our own certificate and make other changes to Tomcat configuration so that it starts accepting connection on port 443 using HTTPS protocol. &lt;/p&gt;  &lt;p&gt;But before we begin setting up SSL on Tomcat, its very important to iterate that this is valid only if you are using Tomcat as a &lt;strong&gt;stand-alone web server&lt;/strong&gt;, the other possibility being Microsoft IIS or Apache Server serving as the main interface to the outside world and Tomcat is serving as the &lt;strong&gt;JSP/Servlet Container&lt;/strong&gt; at the backend, communicating with one of these servers. In that case, you have to make SSL configuration changes in IIS or Apache, while Tomcat will continue to work normally sending cleartext responses (no encryption at Tomcat level).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Creating a Certificate&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Fortunately, Java provides us with a utility (&lt;strong&gt;keytool&lt;/strong&gt;) which can be used to create a self-signed certificate. This is not validated by any authority, hence not suitable for production use. But it can certainly be used for development purposes. So, lets&amp;#160; create a certificate from scratch for our application:&lt;/p&gt;  &lt;p&gt;Type in the following command and enter details(credentials) about your organization that users of your site/application would see in the certificate given to them when they use it. The default password for Tomcat is “changeit” (all in lower-case). It would be asked when you type the following command to generate your self signed certificate:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; keytool –genkey –alias –tomcat –keyalg RSA&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Note, you must enter the password (as told above) and then all the credentials as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TDDZBjnpphI/AAAAAAAAAkI/67PhlZEBrNQ/s1600-h/newCertificate%5B7%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="newCertificate" border="0" alt="newCertificate" src="http://lh4.ggpht.com/_QosbmeyDVxU/TDDZCnHxKII/AAAAAAAAAkM/XHY-x1bWmAU/newCertificate_thumb%5B5%5D.jpg?imgmax=800" width="438" height="333" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;After doing this correctly, a &lt;strong&gt;.keystore&lt;/strong&gt; file will be generated in your home folder (in the home folder of the user who did this). This is the certificate your tomcat would use and present to any user who visit the secure site.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Configuring Tomcat&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now, we make changes in Tomcat configuration to reflect this change. Tomcat uses 2 different implementations for SSL: &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/apr.html"&gt;Apache Portable Runtime&lt;/a&gt; (&lt;strong&gt;APR&lt;/strong&gt;) implementation and JSSE Connector implementation, provided by default with JRE (Java Runtime). Most of the users (like me) do not have APR installed by default, so following steps are valid for JSSE implementation&lt;/p&gt;  &lt;p&gt;We now open server.xml present in &lt;strong&gt;$CATALINA_HOME/conf&lt;/strong&gt;. An example &lt;strong&gt;&amp;lt;Connector&amp;gt;&lt;/strong&gt; element for SSL is included by default in most server.xml that comes with Tomcat. It looks something like this: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/TDDZDeaZsCI/AAAAAAAAAkQ/Qk6MgfrB7ec/s1600-h/SSLconnector%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SSLconnector" border="0" alt="SSLconnector" src="http://lh3.ggpht.com/_QosbmeyDVxU/TDDZENNOqEI/AAAAAAAAAkU/tsM020lZWqQ/SSLconnector_thumb%5B2%5D.jpg?imgmax=800" width="431" height="150" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;All you have to do is to &lt;strong&gt;uncomment&lt;/strong&gt; this connector and add to it the &lt;strong&gt;location&lt;/strong&gt; of &lt;strong&gt;your certificate (.keystore) &lt;/strong&gt;that we generated and the &lt;strong&gt;password&lt;/strong&gt; we used to generate it. So finally the entry for the connector looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/TDDZFJbGDsI/AAAAAAAAAkY/WcJU5oeIznA/s1600-h/NewConnector%5B14%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="NewConnector" border="0" alt="NewConnector" src="http://lh5.ggpht.com/_QosbmeyDVxU/TDDZFzhRPmI/AAAAAAAAAkc/tIiY4wc4RTc/NewConnector_thumb%5B12%5D.jpg?imgmax=800" width="438" height="219" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Also, make sure that the &lt;strong&gt;Non-SSL Connector&lt;/strong&gt; (the default one) which accepts connection on 8080, should have a &lt;strong&gt;redirectPort&lt;/strong&gt; attribute set to &lt;strong&gt;8443&lt;/strong&gt; or to the port that you specified for SSL Connection in the connector above. The entry looks like:&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TDDZG0Do_qI/AAAAAAAAAkg/GQ4ipLh5Zww/s1600-h/NonSSLConnector%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="NonSSLConnector" border="0" alt="NonSSLConnector" src="http://lh5.ggpht.com/_QosbmeyDVxU/TDDZHhJe3MI/AAAAAAAAAkk/W07hds61imE/NonSSLConnector_thumb%5B2%5D.jpg?imgmax=800" width="425" height="166" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;That’s all you need to do to make sure that Tomcat starts accepting secure connections on 8443 and redirects users who attempt to access a secure page with http to https. For more details regarding each attribute of the connector, you can refer &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/config/http.html"&gt;Java HTTP Connector&lt;/a&gt; configuration reference.&lt;/p&gt;  &lt;p&gt;Go ahead, try it out and feel the magic that Tomcat provides you. :-) Please post any bugs, queries and corrections as comments. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-7752603950735640660?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/06/ssl-configuration-in-tomcat.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_QosbmeyDVxU/TDDZCnHxKII/AAAAAAAAAkM/XHY-x1bWmAU/s72-c/newCertificate_thumb%5B5%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-4305272554986104494</guid><pubDate>Sat, 29 May 2010 19:07:00 +0000</pubDate><atom:updated>2010-05-29T12:07:38.185-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Web-Apps</category><category domain="http://www.blogger.com/atom/ns#">Tomcat</category><title>Tomcat – Container Managed Security</title><description>&lt;p&gt;If you are a web developer, then I will not think twice before saying that you must have used &lt;strong&gt;Tomcat&lt;/strong&gt; knowingly or unknowingly for deploying your web applications.&lt;/p&gt;  &lt;p&gt;For me, &lt;a href="http://tomcat.apache.org/"&gt;Tomcat&lt;/a&gt; is a pretty basic web server, which mainly serves as a &lt;strong&gt;JSP/Servlet Container&lt;/strong&gt;. Today, we explore some basic &lt;strong&gt;security&lt;/strong&gt; features that are provided by the container itself. There are different ways in which this &lt;strong&gt;authentication&lt;/strong&gt; can be provided. In this blog entry, we will try to understand and implement &lt;strong&gt;Basic&lt;/strong&gt; &amp;amp; &lt;strong&gt;Form&lt;/strong&gt;-based Authentication using &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.html#MemoryRealm"&gt;MemoryRealm&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;What’s a Realm?&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;According to the Tomcat specification, a &lt;strong&gt;Realm&lt;/strong&gt; is a &amp;quot;database&amp;quot; of &lt;em&gt;usernames&lt;/em&gt; and &lt;em&gt;passwords&lt;/em&gt; that identify valid users of a web application (or set of web applications), plus an enumeration of the list of &lt;em&gt;roles&lt;/em&gt; associated with each valid user. Obviously, there are many ways to specify the valid username-password combinations and the associated roles. We will use something known as &lt;strong&gt;MemoryRealm&lt;/strong&gt;, which uses a file: &lt;strong&gt;tomcat-users.xml&lt;/strong&gt; to specify this database.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Steps to Enable Authentication&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Change the configuration file (&lt;strong&gt;server.xml&lt;/strong&gt;) of the Tomcat Server to specify the realm that you will use &lt;/li&gt;    &lt;li&gt;Change the &lt;strong&gt;tomcat-users.xml&lt;/strong&gt; file to list the valid username-passwords and the associated roles &lt;/li&gt;    &lt;li&gt;Change the deployment descriptor (&lt;strong&gt;web.xml&lt;/strong&gt;) for your web application to reflect security constraints needed by the application. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Step1: Change server.xml of Tomcat&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;The default server.xml has the &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.html#UserDatabaseRealm"&gt;UserDatabaseRealm&lt;/a&gt; enabled as shown below:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/TAFlQpzO1EI/AAAAAAAAAig/_iopiAAwItY/s1600-h/serverXml4.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="serverXml" border="0" alt="serverXml" src="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlRvGUROI/AAAAAAAAAik/SdsJRl3bBS8/serverXml_thumb2.jpg?imgmax=800" width="413" height="100" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Note: This can change depending on the versions of the Tomcat, but for Tomcat 5.5 and Tomcat 6, it is the default one. We change it to use &lt;strong&gt;MemoryRealm&lt;/strong&gt; by commenting the previous line and adding the following declaration as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlSak9A9I/AAAAAAAAAio/giqvSJ9gkwo/s1600-h/serverXmlChanged6.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="serverXmlChanged" border="0" alt="serverXmlChanged" src="http://lh6.ggpht.com/_QosbmeyDVxU/TAFlTgJyKaI/AAAAAAAAAis/RA0-W4ONUHg/serverXmlChanged_thumb4.jpg?imgmax=800" width="434" height="197" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Step2: Change tomcat-users.xml of Tomcat&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The default tomcat-users.xml file, which is present at &lt;em&gt;$CATALINA_HOME/conf/tomcat-users.xml&amp;#160; &lt;/em&gt;where &lt;strong&gt;$CATALINA_HOME&lt;/strong&gt; is the directory in which your Tomcat server is installed, looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlUs1Q3YI/AAAAAAAAAiw/TO9qib-Sn9E/s1600-h/tomcatUsers4.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="tomcatUsers" border="0" alt="tomcatUsers" src="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlVnPw32I/AAAAAAAAAi0/bUlasyOndKs/tomcatUsers_thumb2.jpg?imgmax=800" width="432" height="133" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;To this, add your own role and associate with it, some valid combination of username and password as done below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlWZN1wuI/AAAAAAAAAi4/_bedeneKttE/s1600-h/tomcatUsersChanged5.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="tomcatUsersChanged" border="0" alt="tomcatUsersChanged" src="http://lh6.ggpht.com/_QosbmeyDVxU/TAFlXMMwwHI/AAAAAAAAAi8/OMmP8tI1_So/tomcatUsersChanged_thumb3.jpg?imgmax=800" width="443" height="170" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Step3: Change web.xml of your web application&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;So far, we have made changes in the Tomcat server itself, its time we must modify our application to reflect the security constraints that we want. To do so, we add the following to our web.xml file:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;a) Security Constraints as per the need&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlYKwuhxI/AAAAAAAAAjA/zbUypDnPQCE/s1600-h/securityConstraint4.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="securityConstraint" border="0" alt="securityConstraint" src="http://lh6.ggpht.com/_QosbmeyDVxU/TAFlY6gZOkI/AAAAAAAAAjE/gxPIuTY51_4/securityConstraint_thumb2.jpg?imgmax=800" width="422" height="248" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here, We specify that in order to access any URL in our application which comes after &lt;a href="http://{context_App}/jsp/"&gt;http://{context_App}/jsp/&lt;/a&gt;…&amp;#160; should be password-protected. Also, we associate a role with it, so users associated with that role can only access these URLs now.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;b) Declaring the Security Role&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TAFlZiJ9ozI/AAAAAAAAAjI/lP0rGJDNpGE/s1600-h/securityRole3.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="securityRole" border="0" alt="securityRole" src="http://lh3.ggpht.com/_QosbmeyDVxU/TAFlamU7g0I/AAAAAAAAAjM/5ntXxTXrDb0/securityRole_thumb1.jpg?imgmax=800" width="381" height="106" /&gt;&lt;/a&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;c) Specifying the Type of Authentication &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlbcPp-RI/AAAAAAAAAjQ/WcCVruKklLM/s1600-h/basicAuthntication5.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="basicAuthntication" border="0" alt="basicAuthntication" src="http://lh3.ggpht.com/_QosbmeyDVxU/TAFlcDAh5YI/AAAAAAAAAjU/5ftlKQCf7So/basicAuthntication_thumb3.jpg?imgmax=800" width="396" height="140" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We tell Tomcat that we want to use BASIC authentication for our web application and the realm that we will use will use your tomcat-users.xml file.&amp;#160; &lt;/p&gt;  &lt;p&gt;That’s it. All you need to do now, is to &lt;strong&gt;restart&lt;/strong&gt; &lt;strong&gt;Tomcat&lt;/strong&gt; and check whether this works or not. Obviously you need to create a basic web application to deploy onto the Tomcat , for which you have specified the security constraints. You can download my test web application here. (Provide a Link)…………………&lt;/p&gt;  &lt;p&gt;This was pretty basic stuff, but it has several &lt;strong&gt;limitations&lt;/strong&gt;. For one, this is not suitable for production environments and we must use FORM-based authentication instead of BASIC. Secondly, it is not a good practice to use clear-text passwords in tomcat-users.xml file. We can save the encrypted password (encrypted by any algorithm like SHA, MD2, MD5 etc) supported by the realm.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Changes to overcome limitations&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. Use Digested Passwords&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Obviously, storing passwords in clear-text readable format in tomcat-users.xml cannot be considered &lt;strong&gt;safe&lt;/strong&gt; or practical. In a production environment, it is essential to use some &lt;strong&gt;encryption&lt;/strong&gt; to provide security. Tomcat provides us with several digest algorithms (&lt;strong&gt;SHA&lt;/strong&gt;, &lt;strong&gt;MD2&lt;/strong&gt;, &lt;strong&gt;MD5&lt;/strong&gt; etc) supported by &lt;a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html"&gt;MessageDigest&lt;/a&gt; class. To enable it, change the realm declaration in server.xml of Tomcat to the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TAFlc4gc6sI/AAAAAAAAAjY/PwXxm0_q49E/s1600-h/memoryRealmMD54.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="memoryRealmMD5" border="0" alt="memoryRealmMD5" src="http://lh3.ggpht.com/_QosbmeyDVxU/TAFldaAEjrI/AAAAAAAAAjc/Ujrc736cuDo/memoryRealmMD5_thumb2.jpg?imgmax=800" width="432" height="85" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Also, change the password you specified in tomcat-users.xml to the encrypted version of the original password, by using &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/realm/RealmBase.html"&gt;RealmBase&lt;/a&gt; class. You can find this class in &lt;strong&gt;catalina.jar&lt;/strong&gt; in $CATALINA_HOME/lib directory. Use it as follows: &lt;strong&gt;RealmBase.digest(“password”, “MD5”, null)&lt;/strong&gt; . Now, restart your Tomcat and test your authentication.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2. Use FORM-based Authentication&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Since using &lt;strong&gt;BASIC&lt;/strong&gt; authentication, you have very little control over the authentication/login method used by Tomcat and it is not considered practical for &lt;strong&gt;production&lt;/strong&gt; environment. So we demonstrate another way: FORM-based authentication. The whole setup remains same, only you can provide your own custom login and error pages (with your company logo and whatever you want) instead of just popping up a window (as done by Tomcat in BASIC mode)&lt;/p&gt;  &lt;p&gt;We will change the &lt;strong&gt;&amp;lt;login-config&amp;gt;&lt;/strong&gt; element declared in your application’s &lt;strong&gt;web.xml&lt;/strong&gt; as follows:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TAFlefiyuWI/AAAAAAAAAjg/lzV02dom2pk/s1600-h/form-login-config%5B5%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="form-login-config" border="0" alt="form-login-config" src="http://lh5.ggpht.com/_QosbmeyDVxU/TAFlfTveheI/AAAAAAAAAjk/Q_J6GNx1fec/form-login-config_thumb%5B3%5D.jpg?imgmax=800" width="414" height="161" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We will now add &lt;strong&gt;login.jsp&lt;/strong&gt; and &lt;strong&gt;error.jsp&lt;/strong&gt; to our application. login.jsp is used to authenticate the user: hence it would provide a form while error.jsp is the page to which user is redirected in case of an error: invalid username-password. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;login.jsp &lt;/strong&gt;will have at least a login field with name as “&lt;strong&gt;j_username&lt;/strong&gt;” and password field with name as “&lt;strong&gt;j_password&lt;/strong&gt;”. This must be same so that tomcat can identify the password and login fields in the form. Also the action of the form must be “&lt;strong&gt;j_security_check&lt;/strong&gt;”. You can find the link to download the whole project at the end. My authentication page looks like this, which I believe is much better and safer than basic mode.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/TAFlgHyL35I/AAAAAAAAAjo/SVbHqRs2moE/s1600-h/authenticate%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="authenticate" border="0" alt="authenticate" src="http://lh6.ggpht.com/_QosbmeyDVxU/TAFlhl0ea-I/AAAAAAAAAjs/W2NVpFhUNb8/authenticate_thumb%5B2%5D.jpg?imgmax=800" width="276" height="206" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Still, we have one &lt;strong&gt;limitation&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;Any changes made to the file: &lt;strong&gt;tomcat-users.xml&lt;/strong&gt; will be reflected only when the &lt;strong&gt;Tomcat&lt;/strong&gt; server is &lt;strong&gt;restarted&lt;/strong&gt;. So, if you add a new username-password combination to it, you must restart Tomcat in order to have its effect. We can also overcome this limitation by specifying the username-password and roles in a database table and using a &lt;a href="http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.html#JDBCRealm"&gt;&lt;strong&gt;JDBCRealm&lt;/strong&gt;&lt;/a&gt; instead of &lt;strong&gt;MemoryRealm. &lt;/strong&gt;You can download entire project from &lt;a href="http://techno-cratic-blog.googlegroups.com/web/TomcatSecurity.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-4305272554986104494?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/05/tomcat-container-managed-security.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_QosbmeyDVxU/TAFlRvGUROI/AAAAAAAAAik/SdsJRl3bBS8/s72-c/serverXml_thumb2.jpg?imgmax=800" height="72" width="72" /><thr:total>3</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-4203408403313785201</guid><pubDate>Sun, 23 May 2010 10:08:00 +0000</pubDate><atom:updated>2010-05-23T03:08:13.974-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">RPC</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Flex 3</category><title>Flex Remoting with Java Part 2</title><description>&lt;p&gt;In the &lt;a href="http://techno-cratic.blogspot.com/2010/04/flex-remoting-with-java.html"&gt;last blog entry&lt;/a&gt;, I wrote about setting up a simple &lt;a href="http://www.adobe.com/products/flex/overview/"&gt;Flex&lt;/a&gt; Application and integrating it with Java so as to be able to make Remote Calls to Java Methods and display the result in Flex.&lt;/p&gt;  &lt;p&gt;Today, we are going to make those efforts valuable by seeing how a &lt;strong&gt;user-defined object&lt;/strong&gt; can be passed between Java &amp;amp; Flex. We will create a Employee List in Java and show it in a &lt;a href="http://livedocs.adobe.com/flex/3/html/help.html?content=dpcontrols_6.html"&gt;DataGrid&lt;/a&gt; in Flex. As Simple as that. To start with, you must have all the environment setup as explained &lt;a href="http://techno-cratic.blogspot.com/2010/04/flex-remoting-with-java.html"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;RPC with User Defined Objects&lt;/strong&gt; &lt;/u&gt;&lt;/p&gt;  &lt;p&gt;To be able to do so, we must create classes in both &lt;strong&gt;ActionScript&lt;/strong&gt; and &lt;strong&gt;Java&lt;/strong&gt; and &lt;strong&gt;bind them together&lt;/strong&gt; as shown below:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Employee.java&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/S_j9nh7cM6I/AAAAAAAAAhk/JYY4g0cAFPs/s1600-h/EmployeeJava%5B5%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="EmployeeJava" border="0" alt="EmployeeJava" src="http://lh3.ggpht.com/_QosbmeyDVxU/S_j9owG3QZI/AAAAAAAAAho/E4A3Hx-U91k/EmployeeJava_thumb%5B3%5D.jpg?imgmax=800" width="390" height="377" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Employee.as&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S_j9qOt5bjI/AAAAAAAAAhs/ni1I1mBVBzg/s1600-h/EmployeeAS%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="EmployeeAS" border="0" alt="EmployeeAS" src="http://lh3.ggpht.com/_QosbmeyDVxU/S_j9rSuFk0I/AAAAAAAAAhw/WY2kWzzAE68/EmployeeAS_thumb%5B1%5D.jpg?imgmax=800" width="372" height="227" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The Java and the ActionScript file have the same attributes and they map to each other using Flex Metadata: &lt;strong&gt;RemoteClass(alias=””) &lt;/strong&gt;which maps the java object returned from server to ActionScript object which can be displayed. This &lt;strong&gt;metadata tag&lt;/strong&gt; used in ActionScript Class (Employee.as) specifies the fully qualified name of the corresponding Java Class. We now create a Utility class for getting Employee details:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;EmployeeUtils.java&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S_j9st5lQlI/AAAAAAAAAh0/dbhRDXiwEMA/s1600-h/EmployeeUtils%5B7%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="EmployeeUtils" border="0" alt="EmployeeUtils" src="http://lh6.ggpht.com/_QosbmeyDVxU/S_j9tyZWA6I/AAAAAAAAAh4/u7zXPS7_Ulc/EmployeeUtils_thumb%5B5%5D.jpg?imgmax=800" width="426" height="474" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here, we have hard-coded the Employees Data, but it is as easy to get these employees from a database by firing a query. We now make a RPC call from Flex to get this list so that we can display it in a DataGrid.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S_j9uziWcmI/AAAAAAAAAh8/hKk6W5cnCeE/s1600-h/getAllEmployees%5B12%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="getAllEmployees" border="0" alt="getAllEmployees" src="http://lh5.ggpht.com/_QosbmeyDVxU/S_j9v2rJCJI/AAAAAAAAAiE/5J9rGie2kQ4/getAllEmployees_thumb%5B10%5D.jpg?imgmax=800" width="431" height="419" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We create a &lt;a href="http://livedocs.adobe.com/flex/3/langref/mx/rpc/remoting/mxml/RemoteObject.html"&gt;RemoteObject&lt;/a&gt; in Flex and specify a destination “&lt;strong&gt;employee&lt;/strong&gt;” which is present in a &lt;strong&gt;remoting-config.xml&lt;/strong&gt; to specify the Java Class file to look for methods called (&lt;strong&gt;getAllEmployees&lt;/strong&gt;) using this RemoteObject. Part of flex-config.xml which specifies this looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S_j9w62phVI/AAAAAAAAAiI/VJAcRjaUbEI/s1600-h/remotingConfig%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="remotingConfig" border="0" alt="remotingConfig" src="http://lh5.ggpht.com/_QosbmeyDVxU/S_j9yrtKtjI/AAAAAAAAAiM/09Oh1Ps8KC8/remotingConfig_thumb%5B2%5D.jpg?imgmax=800" width="387" height="91" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;We specify the &lt;strong&gt;result and fault handlers&lt;/strong&gt; with the remote object, which are called when result or fault occurs. In the result handler function, we save the result in a &lt;strong&gt;ArrayCollection&lt;/strong&gt;, which we &lt;strong&gt;bind with our DataGrid&lt;/strong&gt; as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/S_j9z-FTIWI/AAAAAAAAAiQ/cqvbv_mB93Q/s1600-h/employeeDatagrid%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="employeeDatagrid" border="0" alt="employeeDatagrid" src="http://lh6.ggpht.com/_QosbmeyDVxU/S_j91PO95SI/AAAAAAAAAiU/ov1wh_A5IMA/employeeDatagrid_thumb%5B4%5D.jpg?imgmax=800" width="426" height="303" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;We set the &lt;strong&gt;dataProvider&lt;/strong&gt; property of our &lt;strong&gt;DataGrid&lt;/strong&gt; component to &lt;strong&gt;__employeeList&lt;/strong&gt;, which is a bindable ArrayCollection which contains the result data. The advantage of making the ArrayCollection as &lt;strong&gt;bindable&lt;/strong&gt; is that any changes made to this ArrayCllection (due to any event) is reflected in our DataGrid automatically.&lt;/p&gt;  &lt;p&gt;When we run this example, we get a DataGrid as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S_j919t4wZI/AAAAAAAAAiY/kbLWVbkIYUQ/s1600-h/output%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="output" border="0" alt="output" src="http://lh6.ggpht.com/_QosbmeyDVxU/S_j93YMU8QI/AAAAAAAAAic/AWrGoKDlkX4/output_thumb%5B4%5D.jpg?imgmax=800" width="425" height="272" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Again this is &lt;strong&gt;not&lt;/strong&gt; a best-way to code your flex application. The intent was to show how user-defined objects can be passed between Java &amp;amp; Flex. In practice and in large scale application, we would use tested frameworks like &lt;a href="http://www.adobe.com/devnet/flex/articles/cairngorm_pt1.html"&gt;&lt;strong&gt;Cairngorm&lt;/strong&gt;&lt;/a&gt; or &lt;a href="http://puremvc.org/"&gt;&lt;strong&gt;PureMVC&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Wishing you very Happy Coding with Flex :-) &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-4203408403313785201?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/05/flex-remoting-with-java-part-2.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_QosbmeyDVxU/S_j9owG3QZI/AAAAAAAAAho/E4A3Hx-U91k/s72-c/EmployeeJava_thumb%5B3%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-7013278407881681103</guid><pubDate>Wed, 28 Apr 2010 16:17:00 +0000</pubDate><atom:updated>2010-05-03T07:47:00.721-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">RPC</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Flex 3</category><title>Flex Remoting with Java</title><description>&lt;p&gt;&lt;a href="http://www.adobe.com/products/flex/?promoid=AINA"&gt;&lt;strong&gt;Flex&lt;/strong&gt;&lt;/a&gt; is one of those technologies that has taken the &lt;strong&gt;RIA &lt;/strong&gt;(rich internet applications) world by storm and surprise. It has never been so easy to create interactive user interfaces before. Flex offers various ways to interact with languages like Java, PHP and Coldfusion:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;RPC Services&lt;/strong&gt; which enables Flex applications to make &lt;strong&gt;asynchronous&lt;/strong&gt; calls to remote services following a call and response model. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Data Management Services&lt;/strong&gt; provides data synchronization and &lt;strong&gt;real-time updates&lt;/strong&gt;, on-demand data paging and many other data integration features to Flex Applications. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Message Services&lt;/strong&gt; that provides Flex with a &lt;strong&gt;publish-subscribe mechanism&lt;/strong&gt; to create rich and collaborative real-time applications &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;All these capabilities are provided as a part of &lt;a href="http://www.adobe.com/ap/products/livecycle/dataservices/"&gt;Adobe’s LiveCycle Data Services Pack&lt;/a&gt;. But unlike the Flex SDK, the entire LiveCycle Data Services Pack is not free and open Source. Rather, Adobe has open sourced a part of it, basically the RPC services and Message Services known as &lt;a href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/"&gt;BlazeDS&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;Using the &lt;strong&gt;RPC Services&lt;/strong&gt;, one can &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Call and invoke remote Java Methods directly and bind the result to a Flex component. (using &lt;strong&gt;RemoteObject&lt;/strong&gt; Component ). It follows a &lt;strong&gt;request and response model&lt;/strong&gt; making asynchronous calls to the backend.  &lt;/li&gt;    &lt;li&gt;Invoke an already deployed &lt;strong&gt;SOAP-based Web Service&lt;/strong&gt; over HTTP using &lt;strong&gt;WebService&lt;/strong&gt; Component. &lt;/li&gt;    &lt;li&gt;Send and receive data using &lt;strong&gt;HTTP GET and POST&lt;/strong&gt; using &lt;strong&gt;HTTPService&lt;/strong&gt; Component. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Today, we are going to make use of &lt;strong&gt;RemoteObject&lt;/strong&gt; to invoke Java Methods and show the result in Flex. For this purpose, we are going to use &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+3"&gt;Flex 3 SDK&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://alist.adobe.co.uk/products/flex/"&gt;Flex Builder 3&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://opensource.adobe.com/wiki/display/blazeds/Downloads"&gt;BlazeDS&lt;/a&gt; for making &lt;strong&gt;asynchronous&lt;/strong&gt; calls to Java &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Open Flex Builder. Create a new &lt;strong&gt;Java&lt;/strong&gt; Project. Name it ‘&lt;strong&gt;testFlexRPC’&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Project Structure&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Structure of a typical flex application that we will use looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfZXBaxYI/AAAAAAAAAgM/sDALkJGUYN0/s1600-h/projectStructure2.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="projectStructure" border="0" alt="projectStructure" src="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfaOwjqbI/AAAAAAAAAgQ/it-btaQtDm8/projectStructure_thumb.jpg?imgmax=800" width="184" height="210" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;‘&lt;strong&gt;web’&lt;/strong&gt; is our output folder. We will deploy it as a WAR file later on. The output folder contains the &lt;strong&gt;WEB-INF&lt;/strong&gt; folder, in which we will add the deployment descriptor ‘&lt;strong&gt;web.xml&lt;/strong&gt;’ file and other folders such as classes. (the path web/WEB-INF/classes should be your default output folder in Java Build Path, which will store the class files). Make two other folders, flex and lib which will contain the flex files for remoting purposes (&lt;strong&gt;remoting-config.xml&lt;/strong&gt; and &lt;strong&gt;services-config.xml&lt;/strong&gt;) and the libraries needed for Flex Remoting (typically the &lt;a href="http://opensource.adobe.com/wiki/display/blazeds/Downloads"&gt;BlazeDS JARs&lt;/a&gt; for messaging).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Deployment Descriptor&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Lets add the deployment descriptor (&lt;strong&gt;web.xml&lt;/strong&gt;) to the project. It will typically contain an entry for the &lt;strong&gt;MessageBroker&lt;/strong&gt; Servlet (which is used by the flex framework for RPC communication) and specify the location of &lt;strong&gt;services-config.xml&lt;/strong&gt;, which is used to define the kind of RPC services to be used. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;web.xml&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S9hfbLeIS4I/AAAAAAAAAgU/_IQMIzYC0RY/s1600-h/deploymentDescriptor6.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="deploymentDescriptor" border="0" alt="deploymentDescriptor" src="http://lh5.ggpht.com/_QosbmeyDVxU/S9hfb4GtQBI/AAAAAAAAAgY/3g83sW1XIlg/deploymentDescriptor_thumb4.jpg?imgmax=800" width="426" height="355" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Services-Config.xml&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Line 14 of web.xml specifies the location of services-config.xml in your project. It is the &lt;strong&gt;top-level configuration file&lt;/strong&gt; that contains &lt;strong&gt;security-constraint definitions&lt;/strong&gt;, &lt;strong&gt;channel definitions&lt;/strong&gt;, &lt;strong&gt;logging settings&lt;/strong&gt; that each of the services can use. Create an empty XML file on that location and add the following to it:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S9hfdHj10sI/AAAAAAAAAgc/4j8IIC6xMOE/s1600-h/servicesConfig16.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="servicesConfig1" border="0" alt="servicesConfig1" src="http://lh3.ggpht.com/_QosbmeyDVxU/S9hfd-J_jdI/AAAAAAAAAgg/q2jOPY-e6Ao/servicesConfig1_thumb4.jpg?imgmax=800" width="430" height="372" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Firstly, it defines the kind of RPC services that we will use in this project. It includes another file: &lt;strong&gt;remoting-config.xml&lt;/strong&gt; which will contain the destinations for RPC Calls. &lt;/p&gt;  &lt;p&gt;Next, it defines the channels that are actually used by the flex application to transfer data between client and server. It defines two channels: &lt;strong&gt;HTTP channel&lt;/strong&gt; and &lt;strong&gt;AMF Channel &lt;/strong&gt;(AMF stands for Actionscript Messaging Format). These channels are used in remoting-config.xml as we will see in a moment. These channels are a key element of the &lt;strong&gt;destinations&lt;/strong&gt; that are specified and perform all work needed for the asynchronous communication that is required by the application.&lt;/p&gt;  &lt;p&gt;The other part of this file: services-config.xml specifies the logging information and looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S9hffb5zv3I/AAAAAAAAAgk/oG_zqYFP0BM/s1600-h/servicesConfig27.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="servicesConfig2" border="0" alt="servicesConfig2" src="http://lh3.ggpht.com/_QosbmeyDVxU/S9hfgUBdcYI/AAAAAAAAAgo/Tz28wX3xBz8/servicesConfig2_thumb5.jpg?imgmax=800" width="447" height="362" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Remoting-Config.xml&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Lets create another XML file, which will list the destinations that we will make use of, for making asynchronous calls to remote Java methods from Flex. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfhaB27LI/AAAAAAAAAgs/ERdMu0SBxuU/s1600-h/remotingConfig6.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="remotingConfig" border="0" alt="remotingConfig" src="http://lh6.ggpht.com/_QosbmeyDVxU/S9hfiTqATWI/AAAAAAAAAgw/-FZWxFxFPsI/remotingConfig_thumb4.jpg?imgmax=800" width="430" height="366" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It specifies the &lt;strong&gt;channels&lt;/strong&gt; (HTTP and AMF that we configured in services-config.xml) that these destinations will use to communicate between client and server. This file also lists the &lt;strong&gt;adapter&lt;/strong&gt; (server-side code) that interacts directly with the object or the service.&lt;/p&gt;  &lt;p&gt;Next, it defines the &lt;strong&gt;destinations&lt;/strong&gt; that can be used with &lt;strong&gt;&amp;lt;mx:RemoteObject&amp;gt;&lt;/strong&gt;, &lt;strong&gt;&amp;lt;mx:WebService&amp;gt;&lt;/strong&gt; and &lt;strong&gt;&amp;lt;mx:WebService&amp;gt;&lt;/strong&gt; tags in MXML or its equivalent in Actionscript code.&lt;/p&gt;  &lt;p&gt;Here, we define a destination (&lt;strong&gt;sayHelloToMe&lt;/strong&gt;) and bind it with the Java Class &lt;strong&gt;SayHello.java&lt;/strong&gt; present in java.myPackage package. &lt;/p&gt;  &lt;p&gt;Now, add the following &lt;strong&gt;libraries&lt;/strong&gt; in your project. You can find them in the &lt;strong&gt;BlazeDS&lt;/strong&gt; zipped file that you downloaded at the start. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S9hfjnlfU_I/AAAAAAAAAg0/r3P2Om4PXso/s1600-h/libraries2.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="libraries" border="0" alt="libraries" src="http://lh6.ggpht.com/_QosbmeyDVxU/S9hfkYVkqlI/AAAAAAAAAg4/UCKgtlb1g_o/libraries_thumb.jpg?imgmax=800" width="233" height="182" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, right click on the project node and go to Flex Project Nature –&amp;gt; Add Flex Project Nature&lt;/p&gt;  &lt;p&gt;A popup would appear asking for &lt;strong&gt;Flex Project Settings&lt;/strong&gt;. Choose the default settings: application server type to &lt;strong&gt;J2EE&lt;/strong&gt; and check the box Using remote object access service. Click Next to configure J2EE server for your application &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S9hflg8RIYI/AAAAAAAAAg8/W4gZG0pY1Lw/s1600-h/flexProjectNature3.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="flexProjectNature" border="0" alt="flexProjectNature" src="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfmUa6e3I/AAAAAAAAAhA/tbofPmQxv6w/flexProjectNature_thumb1.jpg?imgmax=800" width="331" height="343" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Enter your application deployment settings like your application server’s port number and the context root that you want for this application. Also specify the output folder, where you want the resulting SWF file (and the html wrapper) to be stored. Click Finish to successfully add the Flex Project Nature to your Java application.&lt;/p&gt;  &lt;p&gt;It will automatically create a &lt;strong&gt;default MXML application file&lt;/strong&gt; with the same name as the project to the source folder (configurable in Flex Build Path). We move this mxml file to flex folder that we created under &lt;strong&gt;src/flex&lt;/strong&gt; folder (not necessarily needed, but we have a thing for being organized :-) ). Though for this, we also have to change the Flex Build Path. So, right click on the project node, go to properties. Click on Flex Build Path tab and set the following settings:&lt;/p&gt;  &lt;p&gt;Click Add Folder and add “src”. Also change the &lt;strong&gt;main source folder from “src” to “src/flex”&lt;/strong&gt;. Click Ok to update the compiler settings. You still need to set the testFlexRPC.mxml as the default mxml application. so, right click Project node, go to Properties, Go to tab Flex Application. click on Flex folder and click add. Now add the &lt;strong&gt;testFlexRPC.mxml&lt;/strong&gt; file and click set on default button to make it &lt;strong&gt;default application file&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;If you check out, the project must be giving you a compile-time error since we have not created any &lt;strong&gt;html file (wrapper) &lt;/strong&gt;to embed the SWF file which contains the compiled flex application. So right click on the error and click ‘Re-create HTML Templates’ to create the default html wrapper which can be used as is.&lt;/p&gt;  &lt;p&gt;Now, your application is ready to be deployed (technically), but since we have not added any display element and made use of Flex Remoting (the long and tiring process that we have done so far), so there is no use of deploying it currently.&lt;/p&gt;  &lt;p&gt;Now, comes the easy part. Click the &lt;strong&gt;src/java&lt;/strong&gt; and add a new package: &lt;strong&gt;mypackage&lt;/strong&gt; and create a new Java File: &lt;strong&gt;SayHello.java&lt;/strong&gt; ( as specified in remoting-config.xml ) It looks like the following:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;SayHello.java&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S9hfnBy5SFI/AAAAAAAAAhE/NaEniiLJWhU/s1600-h/SayHelloToMe5.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SayHelloToMe" border="0" alt="SayHelloToMe" src="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfoPJDh6I/AAAAAAAAAhI/Z9BcpVui7wc/SayHelloToMe_thumb2.jpg?imgmax=800" width="410" height="233" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, this is a very simple example (because I think you already have had enough !!) and this is not something for which you have to go to Java from Flex. But it is enough to show you how things can be done and results calculated in Java can be passed to Flex (whether its a primitive or any other object: String or any user-defined object as well)&lt;/p&gt;  &lt;p&gt;Now, we will call this java Method in Flex. Add the following code in testFlexRPC.mxml file, already created.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;testFlexRPC.mxml&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfo-wgB1I/AAAAAAAAAhM/bhwd2W4II8Q/s1600-h/ApplicationMXML6.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ApplicationMXML" border="0" alt="ApplicationMXML" src="http://lh3.ggpht.com/_QosbmeyDVxU/S9hfpqL4VBI/AAAAAAAAAhQ/DOovLyBCBwk/ApplicationMXML_thumb4.jpg?imgmax=800" width="415" height="311" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Again, this is not a best-practice way to code, but since I’m running out of space and you are running out of patience, so I’m doing things quickly :-)&lt;/p&gt;  &lt;p&gt;Finally, we are ready for deployment and I’m using &lt;strong&gt;Tomcat&lt;/strong&gt; to deploy my web application. To accomplish this, we create the following XML file specifying the context.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;flexRPCJava.xml&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S9hfqLgoBfI/AAAAAAAAAhU/ztjPvTL3qk8/s1600-h/flexRPCJavaXML6.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="flexRPCJavaXML" border="0" alt="flexRPCJavaXML" src="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfq1EM2oI/AAAAAAAAAhY/VEVm_N0Csjg/flexRPCJavaXML_thumb4.jpg?imgmax=800" width="318" height="86" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;To learn more on how to deploy Web Applications on Tomcat, please read my previous &lt;a href="http://techno-cratic.blogspot.com/2009/11/deploying-web-apps-on-tomcat.html"&gt;blog entry about the same&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Now, all you need to do is to start Tomcat and check whether your application is working correctly or not by checking the HTML wrapper that Flex Builder created for you:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S9hfr0cyV9I/AAAAAAAAAhc/b5qiBtSMT5E/s1600-h/output4.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="output" border="0" alt="output" src="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfs8esE0I/AAAAAAAAAhg/gybn2nCs7B0/output_thumb2.jpg?imgmax=800" width="337" height="227" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I hope you are with me till now and would be &lt;strong&gt;happy&lt;/strong&gt; to know that this (perhaps the longest blog I ever wrote) has finished and you have successfully learned how to use Flex with Java.&lt;/p&gt;  &lt;p&gt;I could have taught the same in a very concise manner using the &lt;a href="http://opensource.adobe.com/wiki/display/blazeds/Release+Builds"&gt;&lt;strong&gt;BlazeDS Turnkey Server&lt;/strong&gt;&lt;/a&gt; and pre-configured examples, but that would have defeated the purpose of structuring and creating a web-application from scratch. Hope you had fun. Cheers !!&lt;/p&gt;  &lt;p&gt;Any suggestions, corrections or Comments are welcome and wanted :-)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-7013278407881681103?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/04/flex-remoting-with-java.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_QosbmeyDVxU/S9hfaOwjqbI/AAAAAAAAAgQ/it-btaQtDm8/s72-c/projectStructure_thumb.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-1837435412468013551</guid><pubDate>Wed, 24 Mar 2010 07:42:00 +0000</pubDate><atom:updated>2010-10-21T09:17:06.613-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Double-checked Locking</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Threads</category><category domain="http://www.blogger.com/atom/ns#">Singleton</category><title>Singletons, Threads &amp; Double Checked Locking</title><description>&lt;p&gt;This blog entry extends on the &lt;a href="http://techno-cratic.blogspot.com/2009/07/singleton-class-in-java.html"&gt;first entry&lt;/a&gt; I wrote about &lt;strong&gt;Singletons. &lt;/strong&gt;Today I try to explain the usage of Singletons in a &lt;strong&gt;multi-threaded&lt;/strong&gt; environment.&lt;/p&gt;  &lt;p&gt;A typical Singleton would look like the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S6nCfB-0JPI/AAAAAAAAAfk/JPqEJQ608fs/s1600-h/MySingletonJava3.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="MySingletonJava" alt="MySingletonJava" src="http://lh6.ggpht.com/_QosbmeyDVxU/S6nCf5YJuII/AAAAAAAAAfo/SA3nevBviKc/MySingletonJava_thumb1.jpg?imgmax=800" width="426" border="0" height="261" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This Singleton class would work perfectly fine in a single-threaded environment. But, &lt;strong&gt;how many Java based applications that you have worked on, are single-threaded? None, perhaps. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Can you see what’s terribly wrong with it when your application has multiple threads running simultaneously ??&lt;/p&gt;  &lt;p&gt;Well, the answer lies in the way the instance is lazy initialized. The &lt;strong&gt;null-checking&lt;/strong&gt; and &lt;strong&gt;initializing the instance&lt;/strong&gt; need to be an &lt;strong&gt;atomic operation&lt;/strong&gt;. Lets see what would happen if following sequence of events happen:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Thead A&lt;/strong&gt; calls &lt;strong&gt;getInstance()&lt;/strong&gt; for the first time. And checks whether instance is null or not. It finds it true and goes to execute line 11. &lt;/li&gt;    &lt;li&gt;At this point, it is pre-empted by another thread: &lt;strong&gt;Thread B&lt;/strong&gt;, which also calls &lt;strong&gt;getInstance()&lt;/strong&gt;, checks for the instance null check, since it is not yet initialized, the private constructor is called and instance is now initialized with a state. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Thread A&lt;/strong&gt; resumes control back and starts from where it left i.e line 11. Now the real problem occurs. Since it checked for the null-check on line 10 when it last run and found it to be true, so when it resumes execution, it will execute from line 11 and again initialize the instance and hence two objects would be created and the whole point of having a single object is violated. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Thus, we really need to do something about the situation. No problems, &lt;strong&gt;Synchronization&lt;/strong&gt; comes to the rescue. Most of us (naive programmers) would tend to get rid of this problem by writing code like below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S6nCgtysLII/AAAAAAAAAfs/Lm2dk66iqeE/s1600-h/MySynchronizedSingletonJava5.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="MySynchronizedSingletonJava" alt="MySynchronizedSingletonJava" src="http://lh6.ggpht.com/_QosbmeyDVxU/S6nChUGLQpI/AAAAAAAAAfw/iy48J6iG0uM/MySynchronizedSingletonJava_thumb3.jpg?imgmax=800" width="436" border="0" height="308" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;The only change that occurs here is that we have made our static factory as synchronized method, which makes sure that no possible combination of &lt;strong&gt;interleaving of threads&lt;/strong&gt; could create two objects like it did in the previous case. Although, this works fine but on close inspection, we find that synchronization is necessary only for the first time, and not needed later on (since instance will always be non-null later on) And &lt;strong&gt;synchronizing the whole method&lt;/strong&gt; is thus uncalled for and &lt;strong&gt;pretty expensive&lt;/strong&gt;. We must think of  other ways of accomplishing this.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. Do Not Lazy Initialize&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If we &lt;strong&gt;do NOT need&lt;/strong&gt; lazy initialization of the resource, then a perfect method to do away with synchronization would be:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S6nCiKPmKAI/AAAAAAAAAf0/P7AQ7BSXGn8/s1600-h/Singleton1Java4.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="Singleton1Java" alt="Singleton1Java" src="http://lh4.ggpht.com/_QosbmeyDVxU/S6nCjLK6guI/AAAAAAAAAf4/900HPSdWQP0/Singleton1Java_thumb2.jpg?imgmax=800" width="427" border="0" height="245" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Using this, we depend on the &lt;strong&gt;JVM’s capability to execute the static block as soon as the class is loaded first&lt;/strong&gt; (when it is accessed or referred in code). &lt;/p&gt;  &lt;p&gt;Thus, the VM makes sure that the instance is well initialized, before any thread tries to call &lt;strong&gt;getInstance()&lt;/strong&gt; method. And since static blocks are executed only once, so no chance of creating more objects. One scenario that I could fail this approach is when this class is loaded again (perhaps by a different &lt;strong&gt;classloader&lt;/strong&gt; in a different namespace)&lt;/p&gt;  &lt;p&gt;Another way to write the above code would be:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S6nCjwpBD-I/AAAAAAAAAf8/AqFeXD2v3Nk/s1600-h/Singleton2Java13.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="Singleton2Java" alt="Singleton2Java" src="http://lh4.ggpht.com/_QosbmeyDVxU/S6nCkihwIlI/AAAAAAAAAgA/_xkT1P0UpDU/Singleton2Java_thumb11.jpg?imgmax=800" width="420" border="0" height="229" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;However, I prefer the first way (with static blocks) that gives us more &lt;strong&gt;flexibility&lt;/strong&gt; by allowing us to write a block of code instead of just calling the constructor.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2. Use Double Checked Locking&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A very common, popular and misunderstood programming idiom that looks like the following and works with Java 1.5 and above only.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S6nClYHZfOI/AAAAAAAAAgE/OmiLsz3UM80/s1600-h/Singleton3Java8.jpg"&gt;&lt;img style="border-width: 0px; display: inline;" title="Singleton3Java" alt="Singleton3Java" src="http://lh3.ggpht.com/_QosbmeyDVxU/S6nCmUv8jaI/AAAAAAAAAgI/mK7QOQSMTUA/Singleton3Java_thumb4.jpg?imgmax=800" width="437" border="0" height="374" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;See how cleverly, we have &lt;strong&gt;limited&lt;/strong&gt; the scope of synchronization here. The synchronization occurs only for the first time (when the instance is null), after that the synchronization never occurs as the instance is not null. This is what we needed. But you must make this variable as volatile to work it correctly (due to &lt;a href="http://www.ibm.com/developerworks/library/j-jtp02244.html"&gt;Java Memory Model&lt;/a&gt; implementation) &lt;/p&gt;  &lt;p&gt;To better understand this programming idiom, I would suggest reading &lt;a href="http://www.ibm.com/developerworks/java/library/j-dcl.html"&gt;this&lt;/a&gt; and &lt;a href="http://www.javaworld.com/jw-02-2001/jw-0209-double.html"&gt;this&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I think its enough for one blog post. Any code corrections, new ideas and comments are welcome. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-1837435412468013551?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/03/singletons-threads-double-checked.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_QosbmeyDVxU/S6nCf5YJuII/AAAAAAAAAfo/SA3nevBviKc/s72-c/MySingletonJava_thumb1.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-1630752433203164566</guid><pubDate>Sat, 27 Feb 2010 11:51:00 +0000</pubDate><atom:updated>2010-02-27T03:53:40.749-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Immutable</category><title>Creating Immutable Objects in Java</title><description>&lt;p&gt;Immutable Objects are those objects, who &lt;strong&gt;cannot change their STATE&lt;/strong&gt; once they are created. Most common examples of such objects are String, Integer, Double etc.. (other Wrapper classes). By State, we mean the values assigned to the instance variables of the object in question. &lt;/p&gt;  &lt;p&gt;Immutable Classes are one of the most &lt;strong&gt;robust&lt;/strong&gt; classes that you can ever build and the reasons that we love them are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;They are &lt;strong&gt;Thread-safe&lt;/strong&gt;, hence no need to synchronize them for concurrent access. &lt;/li&gt;    &lt;li&gt;They are &lt;strong&gt;Easy&lt;/strong&gt; to construct, test and use &lt;/li&gt;    &lt;li&gt;They Serve as &lt;strong&gt;good&lt;/strong&gt; Map and Set &lt;strong&gt;keys&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;If such an object throws an exception, it is never left in &lt;strong&gt;undesirable&lt;/strong&gt; or &lt;strong&gt;inconsistent&lt;/strong&gt; state. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Strategy to Construct Immutable Objects&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If we follow the following steps, we can easily create Immutable objects or convert an existing Mutable class into Immutable:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Since we have to restrict our object to change its state once they are created, we must &lt;strong&gt;not provide&lt;/strong&gt; any &lt;strong&gt;setter&lt;/strong&gt; methods. &lt;/li&gt;    &lt;li&gt;If a object has references to other mutable objects, then do not provide getter methods that simply return a reference to the mutable object. Rather, create a new object containing the &lt;strong&gt;copy of the mutable object&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;Don't allow any subclass to override methods of the given class. This can be done by making the class as &lt;strong&gt;final&lt;/strong&gt;. Another way is to make the constructor as private and provide factory methods to construct the object. &lt;/li&gt;    &lt;li&gt;Make all the fields &lt;strong&gt;private and final&lt;/strong&gt;. This way only your class has&amp;#160; the control how these fields are to be manipulated. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S4kHrfF1G_I/AAAAAAAAAew/zvGvj1kgIxg/s1600-h/Person1%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Person1" border="0" alt="Person1" src="http://lh3.ggpht.com/_QosbmeyDVxU/S4kHsk3PW3I/AAAAAAAAAe0/ljIwh6ti8xo/Person1_thumb%5B2%5D.jpg?imgmax=800" width="448" height="373" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Also, do not provide any other methods that &lt;strong&gt;modify&lt;/strong&gt; the object. In case you have to, then do not modify the existing object, rather create a copy of the original object, modify it and return the modified object. &lt;/li&gt;    &lt;li&gt;Construct the whole object in one go, initialize the whole object (assign values to all the instance variables) while creating the object. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S4kHt2RH_TI/AAAAAAAAAe4/jXismk1HSyg/s1600-h/Person2%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Person2" border="0" alt="Person2" src="http://lh5.ggpht.com/_QosbmeyDVxU/S4kHuwvw-1I/AAAAAAAAAe8/rRCsY3pnxCw/Person2_thumb%5B4%5D.jpg?imgmax=800" width="456" height="333" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;One may argue that immutable objects calls for creation of &lt;strong&gt;unnecessary&lt;/strong&gt; objects every now and then, for example if we do &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/S4kHvnszotI/AAAAAAAAAfA/MnCwTTNzu64/s1600-h/Person3%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Person3" border="0" alt="Person3" src="http://lh3.ggpht.com/_QosbmeyDVxU/S4kHwyW-xCI/AAAAAAAAAfE/qn6CZt5MnZE/Person3_thumb%5B1%5D.jpg?imgmax=800" width="276" height="83" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;then a new String is created and the original string is not modified. For argument sake, we have 3 Strings created here. viz. “Agraj”, “ Mangal” and “Agraj Mangal”.&lt;/p&gt;  &lt;p&gt;But this so-called shortcoming is taken care of by the &lt;strong&gt;Garbage Collector&lt;/strong&gt; in Java, which marks any dead object for Garbage Collection (any object which has no active reference to it in any thread of execution)&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-1630752433203164566?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/02/creating-immutable-objects-in-java.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_QosbmeyDVxU/S4kHsk3PW3I/AAAAAAAAAe0/ljIwh6ti8xo/s72-c/Person1_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-7626782526759901039</guid><pubDate>Wed, 27 Jan 2010 16:53:00 +0000</pubDate><atom:updated>2010-01-29T09:37:32.220-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sankalan</category><category domain="http://www.blogger.com/atom/ns#">DU</category><title>Sankalan 2010 Goes Green….the Copenhagen Way !!</title><description>&lt;p&gt;&lt;strong&gt;&lt;a href="http://cs.du.ac.in/"&gt;The Department of Computer Science, University of Delhi&lt;/a&gt;&lt;/strong&gt; once again proudly presents to you its annual technical fest: &lt;strong&gt;Sankalan 2010: Compiling Innovations towards green future….&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The students at the department, understand the importance of global warming and its harmful effects and thus plan to create awareness and spread the word around. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S2BvbHa_GDI/AAAAAAAAAdY/VHHFNRfxmok/s1600-h/Sankalan%2710_magazine_poster%5B5%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Sankalan'10_magazine_poster" border="0" alt="Sankalan'10_magazine_poster" src="http://lh5.ggpht.com/_QosbmeyDVxU/S2BvcfsjBPI/AAAAAAAAAdc/ySZngFch7hY/Sankalan%2710_magazine_poster_thumb%5B3%5D.jpg?imgmax=800" width="298" height="404" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So here we are, with open arms welcoming students all over the country to come and join us in this effort and have fun along the way. &lt;strong&gt;Sankalan&lt;/strong&gt; ( a hindi word meaning Compilation ) has always been a tech-fest of its own kind, with students pouring in from everywhere and making it a success.&lt;/p&gt;  &lt;p&gt;You can find out more information like the events (both technical and non-technical) on the &lt;a href="http://sankalan2010-goinggreen.blogspot.com/"&gt;fest’s blog&lt;/a&gt;. You can check out the official site &lt;a href="http://cs.du.ac.in/sankalan2010/"&gt;here&lt;/a&gt; for registration and detailed information for the fest. So pack up your bags and get ready for entertainment and fun-packed stuff. Hope to see you there at &lt;strong&gt;20-21st Feburary 2010&lt;/strong&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-7626782526759901039?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/01/sankalan-2010-goes-greenthe-copenhagen.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_QosbmeyDVxU/S2BvcfsjBPI/AAAAAAAAAdc/ySZngFch7hY/s72-c/Sankalan%2710_magazine_poster_thumb%5B3%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-4234372965181830431</guid><pubDate>Fri, 15 Jan 2010 18:36:00 +0000</pubDate><atom:updated>2010-01-18T09:10:40.923-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Web Services</category><category domain="http://www.blogger.com/atom/ns#">JBoss</category><category domain="http://www.blogger.com/atom/ns#">JBossWS</category><title>Creating JBossWS Web Services with JBoss 4.2.3GA and JDK1.5</title><description>&lt;p&gt;Continuing from the last post, today we will learn about creating a Simple Web Service using &lt;strong&gt;jBossWS&lt;/strong&gt; (a JAX-WS implementation) and deploy it on JBoss Server. Also, we will see how can we create a WS-Client using in-build utilities and consume the web service with ease.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;:&lt;/u&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Intermediate knowledge of Java, some experience with XML would help, though not necessary. &lt;/li&gt;    &lt;li&gt;Basic Know-how of a web-application &lt;/li&gt;    &lt;li&gt;&lt;a href="http://techno-cratic.blogspot.com/2009/12/jax-ws-web-services-with-jboss-jbossws.html"&gt;Setting up the required environment.&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Getting Started:&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Create a New &lt;strong&gt;Java Project&lt;/strong&gt;. Name it anything you like. Create appropriate structure for a web application like we create an output folder named ‘&lt;strong&gt;web&lt;/strong&gt;’ and create ‘&lt;strong&gt;WEB-INF&lt;/strong&gt;’ in it, which will hold the deployment descriptor ‘&lt;strong&gt;web.xml&lt;/strong&gt;’. The initial structure of our web application looks as follows:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S1NY-p1jDcI/AAAAAAAAAcY/nUrJr3KHsfY/s1600-h/jbossws%20initial%20project%20structure%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws initial project structure" border="0" alt="jbossws initial project structure" src="http://lh4.ggpht.com/_QosbmeyDVxU/S1NY_UKLxYI/AAAAAAAAAcc/1OQpFb8-yHc/jbossws%20initial%20project%20structure_thumb.jpg?imgmax=800" width="211" height="188" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;With &lt;strong&gt;JBossWS&lt;/strong&gt;, to create a web service is as easy as creating a Java class and exposing a method and create its entry in the deployment descriptor. Lets first create an entry in &lt;strong&gt;web.xml&lt;/strong&gt; for the the &lt;strong&gt;EndpointServlet&lt;/strong&gt; class and configure it for the Java class containing the Web Service implementation as shown below:Here, we specify &lt;strong&gt;EndpointServlet&lt;/strong&gt; (comes bundled with JBossWS stack) which is used to publish/deploy your web service over JBoss. We also provide its mapping giving the &lt;strong&gt;url-pattern&lt;/strong&gt; of the web service. &lt;br /&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/S1SVANWKlaI/AAAAAAAAAc4/pysm4h7pGtA/s1600-h/jbossws%20webxml%5B10%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws webxml" border="0" alt="jbossws webxml" src="http://lh4.ggpht.com/_QosbmeyDVxU/S1SVBhxJSqI/AAAAAAAAAc8/_bs83ejT6bY/jbossws%20webxml_thumb%5B6%5D.jpg?imgmax=800" width="445" height="312" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Lets see how our Java Class which contains the web service would look like: &lt;/p&gt;  &lt;div&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/S1SVCd4CiZI/AAAAAAAAAdA/EHZ_6gQegwI/s1600-h/jbossws%20helloworld%5B3%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws helloworld" border="0" alt="jbossws helloworld" src="http://lh4.ggpht.com/_QosbmeyDVxU/S1SVDhLB_mI/AAAAAAAAAdE/sYxCROfW84w/jbossws%20helloworld_thumb%5B1%5D.jpg?imgmax=800" width="358" height="267" /&gt;&lt;/a&gt;    &lt;br /&gt;Notice that we have noticed &lt;strong&gt;JAX-WS Annotations&lt;/strong&gt; to decorate our class. The &lt;strong&gt;@WebService&lt;/strong&gt; annotations tells us that this class contains methods exposed by a web service and &lt;strong&gt;@WebMethod&lt;/strong&gt; annotation points out exactly which method is exposed.    &lt;br /&gt;  &lt;br /&gt;&lt;/div&gt;  &lt;div&gt;To make sure that your code compiles, you must include the &lt;strong&gt;JAR’s&lt;/strong&gt; that come with &lt;strong&gt;JBossWS native stack&lt;/strong&gt;. Therefore, we add the following JAR’s to the &lt;strong&gt;lib&lt;/strong&gt; folder of &lt;strong&gt;WEB-INF&lt;/strong&gt; and also configure it on the &lt;strong&gt;build-path&lt;/strong&gt; of the project so that you do not get compilation error in Eclipse. &lt;/div&gt;  &lt;ul&gt;   &lt;li&gt;jaxb-api.jar &lt;/li&gt;    &lt;li&gt;jbossws-native-jaxws-ext.jar &lt;/li&gt;    &lt;li&gt;jbossws-native-jaxws.jar &lt;/li&gt;    &lt;li&gt;jbossws-native-saaj.jar &lt;/li&gt; &lt;/ul&gt;  &lt;div&gt;So, thats all you need to do in order to expose your web service. Lets now publish this web service on JBoss. To do so, we will package it as a WAR file using ANT. I have used the following ANT script to create WAR for my web service. &lt;/div&gt;  &lt;div&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S1SVEfZ79cI/AAAAAAAAAdI/nMikO404lmo/s1600-h/jbosswss%20build%5B5%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbosswss build" border="0" alt="jbosswss build" src="http://lh3.ggpht.com/_QosbmeyDVxU/S1SVFVUVZ3I/AAAAAAAAAdM/lvSyFAe9mMA/jbosswss%20build_thumb%5B3%5D.jpg?imgmax=800" width="443" height="216" /&gt;&lt;/a&gt; &lt;/div&gt;  &lt;p&gt;Now, you can copy this war file to the deploy directory of your JBoss Installation. Restart your server and check the logs to see whether it is deployed successfully or not.&lt;/p&gt;  &lt;div&gt;   &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; padding-left: 5px; width: 450px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px" color="#fbfbfb"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;[TomcatDeployer] deploy, ctxPath=/jaxWS,&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;warUrl=.../tmp/deploy/tmp5621016708388909925jaxWS-exp.war/&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;[WSDLFilePublisher] WSDL published to: file:/E:/JBoss/jboss-4.2.3.GA&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 11px"&gt;/server/default/data/wsdl/jaxWS.war/HelloWorldService300665606.wsdl&lt;/pre&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;So, that tells you that your web service has been deployed. To double check, you can always check the JBoss web service console on this url: &lt;a href="http://localhost:8085/jbossws/services"&gt;http://localhost:8085/jbossws/services&lt;/a&gt; It shows you a list of all endpoints deployed currently, something like below: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S1NZAuhsF5I/AAAAAAAAAcg/gj105Y6M6QY/s1600-h/jbossws%20Services%20console%5B5%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws Services console" border="0" alt="jbossws Services console" src="http://lh4.ggpht.com/_QosbmeyDVxU/S1NZBoPqD4I/AAAAAAAAAck/RqYfJvwEu_g/jbossws%20Services%20console_thumb%5B3%5D.jpg?imgmax=800" width="434" height="312" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;Lets also have a look at a snippet of generated WSDL, though explaining this is out of scope of this entry. &lt;/p&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/S1NZC5rXq4I/AAAAAAAAAco/6UAIwXmU9_0/s1600-h/jbossws%20wsdl%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws wsdl" border="0" alt="jbossws wsdl" src="http://lh6.ggpht.com/_QosbmeyDVxU/S1NZD3NvkhI/AAAAAAAAAcs/lD_p7fUZ77g/jbossws%20wsdl_thumb%5B2%5D.jpg?imgmax=800" width="441" height="367" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt; JAX-WS provides supports for all Java in-build data types as well as for complex user-defined objects. The &lt;strong&gt;WSDL&lt;/strong&gt; that is generated on publishing the web service contains all the information about the parameters accepted by the web service and also about the format in which the data is returned. Thus, this WSDL is more of a &lt;strong&gt;contract&lt;/strong&gt; on which the communicating parties must agree before communicating.&lt;br /&gt;  &lt;br /&gt;&lt;span style="font-weight: bold" class="Apple-style-span"&gt;&lt;span style="text-decoration: underline" class="Apple-style-span"&gt;Client Generation:&lt;/span&gt;&lt;/span&gt; &lt;/p&gt;&lt;p&gt;Now, we consume this web service by creating client using utilities provided by JBossWS stack ( Examples include &lt;strong&gt;wsconsume&lt;/strong&gt;, &lt;strong&gt;wsget&lt;/strong&gt;, &lt;strong&gt;wsrunclient&lt;/strong&gt; etc ) &lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt;&amp;gt; wsconsume –k &lt;/strong&gt;&lt;a href="http://localhost:8085/jaxWS/prodsWS?wsdl"&gt;&lt;strong&gt;http://localhost:8085/jaxWS/prodsWS?wsdl&lt;/strong&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;&lt;strong&gt;wsconsume&lt;/strong&gt; will parse the WSDL and –k option will generate the java classes which will be used by the client using &lt;strong&gt;JAXB &lt;/strong&gt;framework.&lt;br /&gt;&lt;br /&gt;   Now, we will create a &lt;strong&gt;TestClient &lt;/strong&gt;(to test our webservice), which will finally consume the web service.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/S1SVGwNVz7I/AAAAAAAAAdQ/j3Otk8GpwwY/s1600-h/jbossws%20client%5B5%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws client" border="0" alt="jbossws client" src="http://lh4.ggpht.com/_QosbmeyDVxU/S1SVIXA-9fI/AAAAAAAAAdU/hCEsJE4bJPs/jbossws%20client_thumb%5B3%5D.jpg?imgmax=800" width="452" height="236" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt; As expected, on running this client, it will print “Hello My Name is Agraj” as output. Please note that in order to run this client successfully, you must add few more client jars to the project.&lt;br /&gt;&lt;br /&gt;The &lt;strong&gt;final structure&lt;/strong&gt; of the web application (&lt;strong&gt;web service + client&lt;/strong&gt;) looks like following: &lt;/p&gt;&lt;div&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/S1NZFMpnJeI/AAAAAAAAAcw/tiVSw5FH78U/s1600-h/jbossws%20final%20project%20structure%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossws final project structure" border="0" alt="jbossws final project structure" src="http://lh4.ggpht.com/_QosbmeyDVxU/S1NZGGdw5NI/AAAAAAAAAc0/a8eQnq3_Xw4/jbossws%20final%20project%20structure_thumb%5B4%5D.jpg?imgmax=800" width="358" height="476" /&gt;&lt;/a&gt; &lt;br /&gt;So, that’s all I want to talk about web services today, I hope it was of some help. Please post your views or corrections as comments. &lt;/p&gt;&lt;p&gt;Note: You may encounter a few problems while developing web services using JBossWS and JDK 1.6 since there are some compatibility issues and conflicting jars that create problems. Your best friend in such a case is Google :-)&lt;/p&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-4234372965181830431?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2010/01/creating-jbossws-web-services-with.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_QosbmeyDVxU/S1NY_UKLxYI/AAAAAAAAAcc/1OQpFb8-yHc/s72-c/jbossws%20initial%20project%20structure_thumb.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-6253909828135825340</guid><pubDate>Mon, 07 Dec 2009 17:31:00 +0000</pubDate><atom:updated>2009-12-07T09:31:38.349-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Web Services</category><category domain="http://www.blogger.com/atom/ns#">JBoss</category><category domain="http://www.blogger.com/atom/ns#">JBossWS</category><title>JAX-WS Web Services with JBoss (JBossWS)</title><description>&lt;p&gt;Today, &lt;strong&gt;Web Services&lt;/strong&gt; are an essential part of every developer’s toolkit not only because they have diverse and varied applications but because they are easy to create and consume and work like a charm. Whether it is about inter-communication between legacy applications and new ones or there might be a scenario in which two applications (programmed in different languages) need to interact, Web Services come to your rescue. In short, Web Services are everywhere and you cannot imagine your life without them, especially if you are a Web Developer.&lt;/p&gt;  &lt;p&gt;So, without wasting any more time on Web Services as there are many good resources for introducing them (&lt;strong&gt;see resources below&lt;/strong&gt;), lets see how to create the environment needed to develop these Web Services. (Sometimes, I feel that creating an environment for developing and deploying Java apps is more tedious process than actually coding for the application :-) )&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What is JAX-WS ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;JAX-WS stands of &lt;strong&gt;Java API for XML – Web Services&lt;/strong&gt;; it is the latest standard/specification created for development and maintenance of web services. It is a successor to &lt;strong&gt;JAX-RPC&lt;/strong&gt; and you can read &lt;a href="http://www.ibm.com/developerworks/webservices/library/ws-tip-jaxwsrpc.html"&gt;here&lt;/a&gt; about the differences between the two. JAX-WS bring along many improvements, but for one, it is too easy to create, deploy and consume than its earlier counterpart.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What is JBossWS ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We will use &lt;strong&gt;&lt;a href="http://www.jboss.org/"&gt;JBoss Application Server&lt;/a&gt;&lt;/strong&gt; to deploy our application and hence we will use their Web Service Stack. &lt;strong&gt;JBossWS&lt;/strong&gt; (JBoss Web Services) is in short, an implementation of JAX-WS specification.&lt;/p&gt;  &lt;p&gt;Software that we require:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;JDK &lt;/strong&gt;1.5 and above (We use JDK 1.6) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;JBoss&lt;/strong&gt; Application Server (We will use JBoss5.1.0 with JDK 1.6) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;JBossWS&lt;/strong&gt; Web Service Stack (We use JBossWS Native Stack) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Ant&lt;/strong&gt; (Java Build/Automation Tool) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So, lets set up the environment around us so that we can easily create new and happening web services. &lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://java.sun.com/javase/downloads/index.jsp"&gt;Download&lt;/a&gt; and Install JDK on your machine (if not already installed). Also set your environment variable &lt;strong&gt;JAVA_HOME&lt;/strong&gt; to the installed JDK location. Also add it to &lt;strong&gt;PATH &lt;/strong&gt;environment variable. To check if this step is done properly, check by typing on command line:     &lt;br /&gt;&lt;strong&gt;&amp;gt;&amp;gt;&amp;gt; java –version&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;It will show you something like this (your current java version) and because you set the PATH variable, you can do this from anywhere:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/Sx0757KzqtI/AAAAAAAAAUA/quY9LXwBmYk/s1600-h/javaVersion8.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="javaVersion" border="0" alt="javaVersion" src="http://lh5.ggpht.com/_QosbmeyDVxU/Sx076_McheI/AAAAAAAAAUE/ccUDiS88b8Y/javaVersion_thumb6.jpg?imgmax=800" width="378" height="97" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;Download and install &lt;strong&gt;ANT&lt;/strong&gt; (a Java based Build Tool). If you are new to ANT, check &lt;a href="http://techno-cratic.blogspot.com/2009/03/getting-started-with-ant.html"&gt;my earlier blog entry about ANT&lt;/a&gt; to gain more insight. Again, set &lt;strong&gt;ANT_HOME&lt;/strong&gt; to installation path and also include ANT_HOME in &lt;strong&gt;PATH&lt;/strong&gt; variable. To check, type on console:     &lt;br /&gt;&lt;strong&gt;&amp;gt;&amp;gt;&amp;gt; ant -version&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;It will show the current version of ANT installed, something like this&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/Sx077jb7LdI/AAAAAAAAAUI/e45JbFQuxaM/s1600-h/antVersion4.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="antVersion" border="0" alt="antVersion" src="http://lh5.ggpht.com/_QosbmeyDVxU/Sx078d_WtgI/AAAAAAAAAUM/tiCotjxO7Oc/antVersion_thumb2.jpg?imgmax=800" width="320" height="83" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.jboss.org/jbossas/downloads/"&gt;Download&lt;/a&gt; JBoss 5.1.0 with JDK 1.6 from here and install it on your system. Now, set &lt;strong&gt;JBOSS_HOME = {Installation path}&lt;/strong&gt;     &lt;br /&gt;check by starting and stopping server. Pretty simple so far. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Step 4&lt;/u&gt;&amp;#160; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Download &lt;strong&gt;JBossWS Web Service Stack&lt;/strong&gt;. Note that JBoss application server alone is not enough to deploy your web-services so you need additional jars that help you create, deploy, maintain and consume your JBossWS Web Services.&lt;/p&gt;  &lt;p&gt;Download the binary distribution of &lt;strong&gt;JBossWS-Native 3.2.1 GA&lt;/strong&gt; from &lt;a href="http://www.jboss.org/jbossws/downloads"&gt;here&lt;/a&gt;. Now, lets see how to install this pack.     &lt;br /&gt;Installing jbossWS pack requires: &lt;/p&gt;  &lt;p&gt;1. Copy ant.properties.example to &lt;strong&gt;ant.properties&lt;/strong&gt; (Creating a backup)&lt;/p&gt;  &lt;p&gt;2. Change following in ant.properties (this file is used by &lt;strong&gt;build.xml&lt;/strong&gt;)&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160; a) Location of your JBoss    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;jboss510.home=E:/JBoss/jboss-5.1.0.GA &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;(since we are using JBoss5.1.0 so replacing that constant with real value)&lt;/p&gt;  &lt;p&gt;3. Now run the ant script from command line specifying the target according to the version of JBoss you are using.&lt;/p&gt;  &lt;p&gt;&amp;#160;&amp;#160; &amp;gt;&amp;gt; ant deploy-jbossXXX&amp;#160; (replace XXX with your version of jboss)    &lt;br /&gt;&amp;#160;&amp;#160; &lt;strong&gt;&amp;gt;&amp;gt; ant deploy-jboss510&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;It will run the build.xml file and execute all the tasks mentioned in it in the specified manner. If at the end, it prints BUILD SUCCESSFUL, then it means that you have successfully installed the web service pack. :-)&lt;/p&gt;  &lt;p&gt;To confirm that it has installed correctly, check the following url (assuming that JBoss works on 8080 for you): &lt;a href="http://localhost:8080/jbossws/"&gt;http://localhost:8080/jbossws/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/Sx079KLoKTI/AAAAAAAAAUQ/Ueyrrpq4SSE/s1600-h/jbossWS%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="jbossWS" border="0" alt="jbossWS" src="http://lh3.ggpht.com/_QosbmeyDVxU/Sx07-DVN92I/AAAAAAAAAUU/BW0z-uiMvoA/jbossWS_thumb%5B2%5D.jpg?imgmax=800" width="437" height="306" /&gt;&lt;/a&gt; If you see a picture similar to what is embedded here, it means that JBossWS Web Service Pack is installed successfully on your system and you are ready to create more. This URL acts as a &lt;strong&gt;console to manage&lt;/strong&gt; your currently deployed web services.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;/p&gt;  &lt;p&gt;It is not necessary that you need JBoss to deploy JAX-WS Web Services. With the advent of JDK1.6, you can also use Tomcat server to host these web services as JDK1.6 is already shipped with JAX-WS jars, although the procedure to create and deploy such services will differ from that of JBossWS.&amp;#160; &lt;/p&gt;  &lt;p&gt;In the next blog entry, I would give an example of creating a simple web service using JBossWS Web Services. The actual purpose of this blog entry is to prepare you for the big picture !!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href="http://www.w3schools.com/webservices/ws_intro.asp"&gt;Introduction to Web Services&lt;/a&gt; (Must read for any Beginner) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/webservices/library/w-ovr/"&gt;Web Services Architecture&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="https://jax-ws.dev.java.net/"&gt;JAX-WS Reference Implementation&lt;/a&gt; (a really detailed one !!) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.ibm.com/developerworks/webservices/library/ws-tip-jaxwsrpc.html"&gt;Comparison between JAX-WS and JAX-RPC&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://java.sun.com/javase/downloads/index.jsp"&gt;Download JDK1.6&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://ant.apache.org/bindownload.cgi"&gt;Download ANT&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.jboss.org/jbossas/downloads/"&gt;Download JBoss Application Server&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.jboss.org/jbossws/downloads"&gt;Download JBossWS-Native (Web Service Stack)&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-6253909828135825340?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/12/jax-ws-web-services-with-jboss-jbossws.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_QosbmeyDVxU/Sx076_McheI/AAAAAAAAAUE/ccUDiS88b8Y/s72-c/javaVersion_thumb6.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-6675426913104919681</guid><pubDate>Sun, 15 Nov 2009 12:50:00 +0000</pubDate><atom:updated>2009-11-15T05:01:09.619-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Web-Apps</category><category domain="http://www.blogger.com/atom/ns#">Tomcat</category><title>Deploying Web-Apps on Tomcat</title><description>&lt;p&gt;Today, we are going to see various methods that can be used to deploy a web-application using &lt;a href="http://tomcat.apache.org/"&gt;Tomcat&lt;/a&gt;. These techniques are pretty simple and are a must for every web developer. However, most of us are only aware of a few of these.&lt;/p&gt;  &lt;p&gt;Lets say that on your machine, Tomcat is installed at &lt;strong&gt;E:/Tomcat/apache-tomcat-6.0.10/&lt;/strong&gt;. Hereafter, we will refer this location as &lt;strong&gt;{TOMCAT_HOME}&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;Also suppose that your web application is named as &lt;strong&gt;TestWebApp&lt;/strong&gt; and is located at &lt;strong&gt;E:/MyWorkSpace/Java/TestWebApp. &lt;/strong&gt;Hereafter, we will refer to this location as &lt;strong&gt;{WEBAPP_HOME}&lt;/strong&gt;, and the output folder is named as &lt;strong&gt;web&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;The Three Methods to deploy any web application on Tomcat are discussed below:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;First Method – Copy &amp;amp; Paste&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The simplest and the most straight-forward method to deploy any web application on Tomcat is by copying the &lt;em&gt;output folder&lt;/em&gt; or the &lt;em&gt;WAR&lt;/em&gt; (Web Application Archive) file of your application to &lt;strong&gt;webapps&lt;/strong&gt; folder of your Tomcat installation as shown below:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;a) &lt;u&gt;Copy the output folder&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Simply copy the output folder of your web application to &lt;strong&gt;{TOMCAT_HOME}/webapps/&lt;/strong&gt; folder. Note that you must take care that your output folder must have a folder named &lt;strong&gt;WEB-INF&lt;/strong&gt;, which further contains &lt;strong&gt;web.xml&lt;/strong&gt; file. This file is also known as &lt;strong&gt;deployment descriptor&lt;/strong&gt; as it governs how a web application should be deployed. Now restart tomcat to see the effect. &lt;/p&gt;  &lt;p&gt;FYI, the Tomcat Home, or the Tomcat Home page you see when you type &lt;a href="http://localhost:8080/"&gt;http://localhost:8080/&lt;/a&gt; is also deployed this way and the output folder is named as ROOT.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;b) &lt;u&gt;Copy the WAR file&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First, create a WAR file by going to &lt;strong&gt;{WEBAPP_HOME}/web&lt;/strong&gt; and then type:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&amp;gt; jar –cvf * TestWebApp.war&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This will create a WAR file for you, which you have to copy to &lt;strong&gt;{TOMCAT_HOME}/webapps/&lt;/strong&gt; folder. Now, restart tomcat to see the deployed application.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Second Method – Server.xml&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Another way to deploy your Web application is to add a context entry in Tomcat’s server.xml file, which can be found at &lt;strong&gt;{TOMCAT_HOME}/conf/server.xml&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Edit this file by adding the following entry:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&amp;lt;Context path=”/myApp” docBase=”E:/MyWorkSpace/Java/TestWebApp/web” debug=”true” reloadable=”false” /&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;where myApp is now the Context for you deployed application. To see the deployed application, simply restart tomcat and access the deployed application at &lt;a href="http://localhost:8080/myApp/"&gt;http://localhost:8080/myApp/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;To deploy more applications, simply add one Context entry per application. Note that the &lt;strong&gt;path&lt;/strong&gt; attribute should be unique for all deployed applications at one time.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Third Method – Context in an external XML file&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Create a XML file containing the Context Entry for your application&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&amp;lt;Context path=”/myApp” docBase=”E:/MyWorkSpace/Java/TestWebApp/web” debug=”true” reloadable=”false” /&amp;gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;You can give any name to this XML file, but a common practice is to name it in such a way that helps you identify it with the application it deploys. We name is testWebApp.xml. &lt;/p&gt;  &lt;p&gt;All that is left now, is to copy this testWebApp.xml file to &lt;strong&gt;{TOMCAT_HOME}/conf/Catalina/localhost/ &lt;/strong&gt;folder.&lt;/p&gt;  &lt;p&gt;Restart tomcat and voila, you are done. This way, we have one XML file per application, containing the context and location for that application. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Comparing Various Methods&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The first method is pretty &lt;strong&gt;simple and straight-forward&lt;/strong&gt; but it restricts you to copy the output folder to Tomcat Installation, something that you would not want to do every-time or you may not have enough access rights in some cases.&lt;/p&gt;  &lt;p&gt;On the other hand, the second method is &lt;strong&gt;flexible&lt;/strong&gt; in the sense that location of the output folder of your web application is independent of your Tomcat installation. The downside is that deploying and un-deploying applications again and again can be a head-ache as you have to edit the same server.xml file again and again. &lt;/p&gt;  &lt;p&gt;The third method rules !! It not only is &lt;strong&gt;flexible&lt;/strong&gt; as its second counter-part but also it deploys applications &lt;strong&gt;independent&lt;/strong&gt; of each other in the sense that each application has its own context file (xml file). In case you want to deploy some applications, while restrict some other, you simply have to move/delete the context files (xml) for the applications you do not want to deploy.&lt;/p&gt;  &lt;p&gt;Any Comments, Suggestions or Corrections are welcome &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-6675426913104919681?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/11/deploying-web-apps-on-tomcat.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-8673587930215441321</guid><pubDate>Thu, 24 Sep 2009 16:14:00 +0000</pubDate><atom:updated>2009-09-24T09:15:42.611-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Sorting</category><category domain="http://www.blogger.com/atom/ns#">Collections</category><title>Sorting Collections in Java</title><description>&lt;p&gt;Now this is a pretty basic requirement that Java developers often face. You may want to sort an array of Strings or an List of Complex user defined object based on any field of that object. Java provides you with several options for this purpose that we are going to explore.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Sorting An Array of Pre-defined Data Types&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;To sort simple objects like String, Integer or primitive Data types, you can simply use build-in functions like:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Collections.sort( ); &lt;/li&gt;    &lt;li&gt;Arrays.sort( ); &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Sorting List of Objects – Comparable Vs Comparator&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A more practical example would be to sort a User-defined Object on any field of that Object. Suppose we have a Person class that looks like the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/SruarBuW5nI/AAAAAAAAAO4/l6HI9uReSg4/s1600-h/Personjava%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Personjava" border="0" alt="Personjava" src="http://lh6.ggpht.com/_QosbmeyDVxU/SruasRxxMSI/AAAAAAAAAO8/W4gIu2GSlI0/Personjava_thumb%5B2%5D.jpg?imgmax=800" width="353" height="372" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, we want to sort an ArrayList of Persons based on their &lt;strong&gt;name&lt;/strong&gt;, we have 2 options:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Have Person Class Implement Comparable Interface &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Have your person class implement the Comparable Interface, which makes you implement &lt;strong&gt;compareTo(..) &lt;/strong&gt;method, which is actually used by build-in sorting methods to sort Persons. This method looks like following and compares two person objects, which determines which Person should come first in the sorted order.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/SruatGeRlcI/AAAAAAAAAPA/uW3mjjfWaQs/s1600-h/PersonComparable%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PersonComparable" border="0" alt="PersonComparable" src="http://lh4.ggpht.com/_QosbmeyDVxU/Sruat_YNBVI/AAAAAAAAAPE/X-oY3j5NJcs/PersonComparable_thumb%5B2%5D.jpg?imgmax=800" width="419" height="270" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This method &lt;strong&gt;compareTo(..) &lt;/strong&gt;is used to Compare two Person Objects based on their name. It returns an integer value: 0 if both of them are equal, positive value if the first object comes before the second one, and negative value if first object comes after the second one.&lt;/p&gt;  &lt;p&gt;Now call sort() on it as follows,&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/SruauXZorrI/AAAAAAAAAPI/T7P6KYWN4M0/s1600-h/sortingPersons%5B2%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sortingPersons" border="0" alt="sortingPersons" src="http://lh5.ggpht.com/_QosbmeyDVxU/SruavA7lOeI/AAAAAAAAAPM/_Z714opWz7E/sortingPersons_thumb.jpg?imgmax=800" width="235" height="33" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;where &lt;strong&gt;persons&lt;/strong&gt; is an ArrayList of Persons. As already said, the sort() method internally uses compareTo(..) method to sort these Person Objects&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Create a Separate Class (Comparator Class) implementing Comparator Interface&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A better way to sort these Persons is to create a separate comparator class as shown below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/SruawAmfA0I/AAAAAAAAAPQ/HBxFEOWs0I4/s1600-h/comparatorPerson%5B7%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="comparatorPerson" border="0" alt="comparatorPerson" src="http://lh6.ggpht.com/_QosbmeyDVxU/SruaxPP-ypI/AAAAAAAAAPU/7flQkgaBreA/comparatorPerson_thumb%5B5%5D.jpg?imgmax=800" width="441" height="425" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here we have created a static inner class which is used in sort() method as follows:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/SruaxsYd83I/AAAAAAAAAPY/kdQPZ5y1WhM/s1600-h/personComparator%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="personComparator" border="0" alt="personComparator" src="http://lh6.ggpht.com/_QosbmeyDVxU/Sruaye5BLpI/AAAAAAAAAPc/N1bEXFL1BVw/personComparator_thumb%5B2%5D.jpg?imgmax=800" width="413" height="42" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Comparable Vs Comparator&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I call it a better way as you can now sort your Person class on different fields: Suppose now you want to sort Persons based on their &lt;strong&gt;Age, &lt;/strong&gt;then all you need to do is create a new Comparator class based on age and call sort method with this comparator.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;The code shown above is compatible with Java 1.5 and above. The non-generic functions for Comparable and Comparators looks a bit different but inherently serve the same purpose.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-8673587930215441321?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/09/sorting-collections-in-java.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_QosbmeyDVxU/SruasRxxMSI/AAAAAAAAAO8/W4gIu2GSlI0/s72-c/Personjava_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-5233036347164559739</guid><pubDate>Sat, 05 Sep 2009 17:43:00 +0000</pubDate><atom:updated>2009-09-05T10:46:23.122-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tutorial</category><category domain="http://www.blogger.com/atom/ns#">Web-Apps</category><category domain="http://www.blogger.com/atom/ns#">Hibernate</category><title>Hibernate Basic Tutorial</title><description>&lt;p&gt;This blog post is meant for beginners who want to start with &lt;a href="https://www.hibernate.org/"&gt;Hibernate&lt;/a&gt; and could not find an easy way to set up the basic project. Pre-requisites for understanding this are basic knowledge of Core Java and &lt;a href="http://java.sun.com/docs/books/tutorial/jdbc/index.html"&gt;JDBC&lt;/a&gt; knowledge.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Introduction to Hibernate&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Hibernate is basically an &lt;strong&gt;Object-Relational Mapping&lt;/strong&gt; tool that is used to query against the database in an effective manner and can convert the results into (Java) objects pretty easily. &lt;/p&gt;  &lt;p&gt;In simple terms, one can say that you can &lt;em&gt;Map one database table to a Java class&lt;/em&gt; and objects of that class will correspond to rows in that database table. (Obviously, complex mappings also exists and we can specify all kind of relationships but to begin with, we will talk about the easy ones). &lt;/p&gt;  &lt;p&gt;You can think of &lt;strong&gt;Hibernate as a Wrapper over JDBC&lt;/strong&gt;. The reason I choose the word ‘&lt;strong&gt;Wrapper’&lt;/strong&gt; is to emphasize on the fact that &lt;em&gt;Hibernate itself uses JDBC under the hood&lt;/em&gt;. So, its not really a radically different technology that we are going to learn.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Some Advantages of Hibernate&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Database Independence&lt;/strong&gt; – The code that you write is not dependent on the underlying database. It is also known as &lt;strong&gt;transparent persistence&lt;/strong&gt; as you can always change the underlying database without changing any of your Java code, the only change required is in a Hibernate configuration file. This also means independence from the queries that you will otherwise write specific to a database. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Open Source&lt;/strong&gt; – Hibernate is &lt;em&gt;Free&lt;/em&gt; and &lt;em&gt;Open Source&lt;/em&gt; and licensed under LGPL (Less GNU Public License) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Performance&lt;/strong&gt; – It claims to have improved performance when compared to JDBC API. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Enhanced API&lt;/strong&gt; - It also provides an enhanced Criteria API to query against the database in an effective fashion. Apart from Criteria API, one can also use Native SQL or HQL (Hibernate Query Language) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Lets Get Started&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;So lets start with setting up a basic Hibernate Project in Eclipse. Software and Other stuff that you require are&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.eclipse.org/downloads/"&gt;Eclipse IDE&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="https://www.hibernate.org/6.html"&gt;Hibernate Core Framework&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Any Relational Database (I’m using MySQL) &lt;/li&gt;    &lt;li&gt;JDBC Database Driver for your database &lt;/li&gt;    &lt;li&gt;JARs for Logging (&lt;a href="http://logging.apache.org/log4j/1.2/index.html"&gt;log4j&lt;/a&gt; and &lt;a href="http://www.slf4j.org/"&gt;slf4j&lt;/a&gt;) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Open Eclipse IDE. Create a new Java Project. Name it as you like and press finish. Before we start coding, first let us add the required libraries:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Hibernate JARs&lt;/strong&gt; (from Hibernate Core Framework). Consists of classes like &lt;strong&gt;Session&lt;/strong&gt;, &lt;strong&gt;Transaction&lt;/strong&gt;, &lt;strong&gt;Criteria&lt;/strong&gt; etc. which are used to write Hibernate code. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;hibernate3.jar &lt;/li&gt;    &lt;li&gt;common-collections-3.1.jar &lt;/li&gt;    &lt;li&gt;javassist-3.4.GA.jar &lt;/li&gt;    &lt;li&gt;jta-1.1.jar &lt;/li&gt;    &lt;li&gt;dom4j-1.6.1.jar &lt;/li&gt;    &lt;li&gt;antlr-2.7.6.jar &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Logging JARs&lt;/strong&gt; (used to log the queries fired and for other purposes)&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;log4j-1.2.13.jar &lt;/li&gt;    &lt;li&gt;slf4j-api-1.5.8.jar &lt;/li&gt;    &lt;li&gt;slf4j-log4j12-1.5.8.jar &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Data-Base specific Driver JAR&lt;/strong&gt; (which contains the implementation of JDBC specification and is used to connect to underlying DB )&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;mysql-connector-java-5.1.7-bin.jar &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;After adding the required libraries, let us create a table in our database, which we will use in our example. Following syntax is used for MySQL to create a student table in ‘&lt;strong&gt;hibernate’&lt;/strong&gt; schema :&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/SqKi3ldClxI/AAAAAAAAANo/Ay9buABof_Q/s1600-h/studentsql%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="studentsql" border="0" alt="studentsql" src="http://lh4.ggpht.com/_QosbmeyDVxU/SqKi4RogfNI/AAAAAAAAANs/8MlkUyrDZuM/studentsql_thumb%5B2%5D.jpg?imgmax=800" width="344" height="155" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As already discussed, Hibernate Provides us with the Capability of Mapping a Table in a Relational Database to a Java Class. So Any Operations that you perform the objects of that class are actually performed on the rows of that database table when you save these objects using Hibernate API.&lt;/p&gt;  &lt;p&gt;So, lets create a Student Class in Eclipse (&lt;strong&gt;Student.java&lt;/strong&gt;). This class will have (in this simple case) the instance variables with one-to-one correspondence with the columns of the Student Table that we created above. These classes generally follow &lt;strong&gt;JavaBeans Pattern&lt;/strong&gt; by having Getters and Setters for every property and an empty constructor. So, this is how our Student.java looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/SqKi5V-aXmI/AAAAAAAAANw/08bK_9rt80s/s1600-h/Studentpic%5B7%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Studentpic" border="0" alt="Studentpic" src="http://lh6.ggpht.com/_QosbmeyDVxU/SqKi6mo96BI/AAAAAAAAAN0/VEFVKkv1pm4/Studentpic_thumb%5B5%5D.jpg?imgmax=800" width="421" height="407" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Mapping Java Class to Database Table&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now, we must provide the &lt;strong&gt;mapping&lt;/strong&gt; so that Hibernate can actually map this java file against our table. This mapping is provided in a &lt;strong&gt;XML file&lt;/strong&gt;, which we name as &lt;strong&gt;Student.hbm.xml&lt;/strong&gt;. Although you can provide any name, but keeping the same name as that of the class helps in identification if required later on. The &lt;strong&gt;*.hbm.xml&lt;/strong&gt; extension is mandatory for all mapping files. The Mapping File looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/SqKi7rMfeeI/AAAAAAAAAN4/KC9he61ZXy8/s1600-h/Studenthbm%5B7%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Studenthbm" border="0" alt="Studenthbm" src="http://lh5.ggpht.com/_QosbmeyDVxU/SqKi8TQ5M0I/AAAAAAAAAN8/2bqzv8gIUvE/Studenthbm_thumb%5B5%5D.jpg?imgmax=800" width="416" height="316" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Let us spend a few minutes pondering over this mapping file. Although, most of it is pretty straight-forward. It says that we are mapping our &lt;strong&gt;class&lt;/strong&gt; Student (Student.java) to &lt;strong&gt;table &lt;/strong&gt;Student with primary key as id which maps to id column of table. The &lt;strong&gt;generator &lt;/strong&gt;tag is used to generate primary keys. Different types of generator classes can be used for this purpose. Most commonly used are &lt;strong&gt;native&lt;/strong&gt; (which means use database dependent scheme for generating primary keys), &lt;strong&gt;increment&lt;/strong&gt; (which can be used to increment normal data types like Long, Integer etc. &lt;/p&gt;  &lt;p&gt;Other tags in the file simply maps &lt;strong&gt;properties &lt;/strong&gt;of Java Class to Database columns. You can also specify data types of these properties. Hibernate supports almost all data types that Java offers. This is perhaps the simplest Mapping file that you will see and use. More complex mappings are normally used but we stick to basics since there is much more to learn.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Hibernate Configuration&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Are we missing something ? So far we have created a Database table, a Java file and a XML file that specifies the mapping used by Hibernate, but so far we have not told Hibernate anything about the &lt;strong&gt;location of database&lt;/strong&gt;, the &lt;strong&gt;username-password&lt;/strong&gt; to access it, the &lt;strong&gt;name of schema&lt;/strong&gt; in which all these database objects lie. So to specify these, we use Hibernate Configuration File &lt;strong&gt;hibernate.cfg.xml &lt;/strong&gt;which looks like the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/SqKi9tcVqhI/AAAAAAAAAOA/n5UtCi8nbIA/s1600-h/hibernatecfgxml%5B18%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="hibernatecfgxml" border="0" alt="hibernatecfgxml" src="http://lh3.ggpht.com/_QosbmeyDVxU/SqKi-szOtrI/AAAAAAAAAOE/gDAgdNXiC2Q/hibernatecfgxml_thumb%5B14%5D.jpg?imgmax=800" width="420" height="394" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;There are many ways to provide this configuration to Hibernate. One of them is to use this file. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The property &lt;strong&gt;connection.url&lt;/strong&gt; tells the location of the schema (named as hibernate) to which we connect using Hibernate. It contains all the database objects that we use in our Hibernate application. &lt;/li&gt;    &lt;li&gt;The &lt;strong&gt;connection.username &lt;/strong&gt;and &lt;strong&gt;connection.password &lt;/strong&gt;properties specifies the username and password to connect to our schema. &lt;/li&gt;    &lt;li&gt;This file also specifies all the &lt;strong&gt;hbm.xml&lt;/strong&gt; mapping files that we created (only one in this case) as &lt;strong&gt;mapping resources&lt;/strong&gt;. Yes, this is not a best practice and better techniques do exist. &lt;/li&gt;    &lt;li&gt;The property &lt;strong&gt;hibernate.show_sql&lt;/strong&gt; property is used to generate all SQL statements that Hibernate internally uses. &lt;/li&gt;    &lt;li&gt;There are many other properties as well which we have skipped &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Logging APIs&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Hibernate uses &lt;strong&gt;log4j&lt;/strong&gt; and &lt;strong&gt;slf4j&lt;/strong&gt; for logging purposes. We use &lt;strong&gt;log4j.properties&lt;/strong&gt; to specify logging properties.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/SqKi_i5TIMI/AAAAAAAAAOI/jmUTAw8rdJA/s1600-h/log4properties%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="log4properties" border="0" alt="log4properties" src="http://lh3.ggpht.com/_QosbmeyDVxU/SqKjAwhRM7I/AAAAAAAAAOM/UQtkF2fi7Ts/log4properties_thumb%5B4%5D.jpg?imgmax=800" width="420" height="334" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Finally, lets write some Code&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I have always experienced that setting up environment for any web application is far more difficult than actually writing the code. ;) So, perhaps the difficult part of setting up the environment for our Hibernate Application ( a one-time activity ) is over, and we can write Hibernate code to insert and retrieve data. &lt;/p&gt;  &lt;p&gt;Again, there are many best practices to code like using &lt;strong&gt;Connection Pooling&lt;/strong&gt; for connections, &lt;strong&gt;Transaction Management&lt;/strong&gt; in an effective manner. But for demo purposes, we have ignored all of it as the purpose here to get started with Hibernate.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Hibernate Session Factory&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We create a Session Factory which is then used to get new Hibernate sessions, close sessions and retrieve existing sessions. &lt;/p&gt;  &lt;p&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://lh4.ggpht.com/_QosbmeyDVxU/SqKjB1o6EfI/AAAAAAAAAOQ/vkPsv9sORSM/s1600-h/hibernatesessionfactory%5B8%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="hibernatesessionfactory" border="0" alt="hibernatesessionfactory" src="http://lh4.ggpht.com/_QosbmeyDVxU/SqKjCxk-nLI/AAAAAAAAAOU/4KXNbMrEWsE/hibernatesessionfactory_thumb%5B6%5D.jpg?imgmax=800" width="426" height="479" /&gt;&lt;/a&gt; &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;Now we create a Test Java file (&lt;strong&gt;TestHibernate.java&lt;/strong&gt;) that creates a new Student, retrieves all existing Students, delete a particular student.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Creating a New Student&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/SqKjD90ge3I/AAAAAAAAAOY/X-qVPzLuqdQ/s1600-h/createStudent%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="createStudent" border="0" alt="createStudent" src="http://lh4.ggpht.com/_QosbmeyDVxU/SqKjE3wSlUI/AAAAAAAAAOc/NJOX9-WDZ1g/createStudent_thumb%5B4%5D.jpg?imgmax=800" width="423" height="371" /&gt;&lt;/a&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Retrieving all Students&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/SqKjF9azwII/AAAAAAAAAOg/EPVu_3vHrps/s1600-h/getAllStudents%5B7%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="getAllStudents" border="0" alt="getAllStudents" src="http://lh6.ggpht.com/_QosbmeyDVxU/SqKjGyQsZ9I/AAAAAAAAAOk/cUCtJeknWbU/getAllStudents_thumb%5B5%5D.jpg?imgmax=800" width="428" height="415" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Delete A Student&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://lh5.ggpht.com/_QosbmeyDVxU/SqKjHg4wSJI/AAAAAAAAAOo/CPoyNQ-BHE4/s1600-h/deleteStudent%5B4%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="deleteStudent" border="0" alt="deleteStudent" src="http://lh6.ggpht.com/_QosbmeyDVxU/SqKjIu5tbUI/AAAAAAAAAOs/gPMssrPE084/deleteStudent_thumb%5B2%5D.jpg?imgmax=800" width="428" height="318" /&gt;&lt;/a&gt; &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Project Structure&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now that we have created all the files that we need, we really missed the big part. How to &lt;strong&gt;organize&lt;/strong&gt; them ?? So lets see how we should organize all these files we have created so far.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/SqKjJo0Sy8I/AAAAAAAAAOw/wF8XnWSXa18/s1600-h/projectStructure%5B6%5D.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="projectStructure" border="0" alt="projectStructure" src="http://lh5.ggpht.com/_QosbmeyDVxU/SqKjKvpEKrI/AAAAAAAAAO0/4OdQGkc8zhk/projectStructure_thumb%5B4%5D.jpg?imgmax=800" width="331" height="415" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;TestHibernate.java &lt;/strong&gt;class contains all the hibernate methods shown above plus a main method (for demo) to  test these methods. When you run this Java file, check out the messages printed on the console. &lt;/p&gt;  &lt;p&gt;I hope I have told all the implementation details in the best possible ways I can. I hope you will have a good time building applications with Hibernate. :-)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Notes&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I faced a few problems in setting up project. Mostly were related to missing JARs or usage of old JARs, but few minutes of &lt;strong&gt;googling&lt;/strong&gt; really helped. That’s why I have clearly stated the JARs that I used. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Connection Pooling&lt;/strong&gt; with Hibernate is done using &lt;a href="http://sourceforge.net/projects/c3p0/"&gt;C3P0&lt;/a&gt; connection pooling mechanism.&lt;/p&gt;  &lt;p&gt;Last but not the least, this is just a small yet decent beginning in the huge world of Hibernate. In case you find any mistake, please leave it as a comment. Any suggestions and comments are welcome. :-)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-5233036347164559739?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/09/hibernate-basic-tutorial.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_QosbmeyDVxU/SqKi4RogfNI/AAAAAAAAANs/8MlkUyrDZuM/s72-c/studentsql_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-7683036674588721257</guid><pubDate>Sun, 12 Jul 2009 17:30:00 +0000</pubDate><atom:updated>2009-07-12T10:56:56.819-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Singleton</category><title>Singleton Class in Java</title><description>&lt;p&gt;&lt;strong&gt;What is Singleton ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Many times we face situations in which we need to model an entity which can have &lt;em&gt;only one Live instance at a time&lt;/em&gt;. Speaking in technical terms, we need to create a class which can be &lt;strong&gt;instantiated only once&lt;/strong&gt; i.e. only one object can be created of that class during the scope of the entire application. Examples can be an instance of a Printer which can be used by many clients to print any document. But since we have only printer, so we must not allow the user/client to create more than one instance of the Printer class. Rather all of them must use the same instance in an synchronized manner.&lt;/p&gt;  &lt;p&gt;A more common example could be a handle to the &lt;strong&gt;Database Connection Pool&lt;/strong&gt;. We must have only 1 instance of the Pool Manager, which in turn can allocate database connections from the common pool of connections and can put them back after they are used. Having more than 1 instance of the Pool Manager can result in problems, which can be really difficult to debug.&lt;/p&gt;  &lt;p&gt;Lets see how to make a &lt;strong&gt;Java Class&lt;/strong&gt; that can be used as a &lt;strong&gt;Singleton&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Method 1&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Inside your class, &lt;strong&gt;keep a count&lt;/strong&gt; of how many instances have been created so far in a static variable. Now, you can throw an &lt;strong&gt;Exception&lt;/strong&gt; in your constructor, the moment a client tries to create more than one instance. Example code given below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/Sloi8CTPzvI/AAAAAAAAAMY/2fq4r0skJ7E/s1600-h/pic2%5B4%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="pic2" border="0" alt="pic2" src="http://lh3.ggpht.com/_QosbmeyDVxU/Sloi9OXcprI/AAAAAAAAAMc/NWgKQCDwatg/pic2_thumb%5B2%5D.jpg?imgmax=800" width="448" height="205" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Method 2&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Make your class’ &lt;strong&gt;constructor Private&lt;/strong&gt; and keep another private instance variable which can call the constructor. Create a public getter function for the retrieving the singleton object as shown below:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/Sloi-eZXEMI/AAAAAAAAAMg/HeqiE5ZC6BY/s1600-h/pic1%5B4%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="pic1" border="0" alt="pic1" src="http://lh6.ggpht.com/_QosbmeyDVxU/Sloi_deuVXI/AAAAAAAAAMk/7f7VP7cBf1Y/pic1_thumb%5B2%5D.jpg?imgmax=800" width="441" height="300" /&gt;&lt;/a&gt; &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;One problem with this approach is that one can use &lt;a href="http://java.sun.com/docs/books/tutorial/reflect/index.html"&gt;Reflection API&lt;/a&gt; to invoke the private constructor and thus the whole concept of Singleton class is violated as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_QosbmeyDVxU/SlojAPgjoaI/AAAAAAAAAMo/YouxS-KLkpY/s1600-h/pic3%5B3%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="pic3" border="0" alt="pic3" src="http://lh6.ggpht.com/_QosbmeyDVxU/SlojBDWLt_I/AAAAAAAAAMs/j1njnQ76MBk/pic3_thumb%5B1%5D.jpg?imgmax=800" width="439" height="274" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This can be overcome by throwing an exception in the MySingleton constructor when it is called more than once (as we have done in first Method)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Another Caveat&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Can you think of any more way the above code can be broken? By breaking, I mean can you create two instances of the above class.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Hint&lt;/strong&gt;: &lt;strong&gt;Serialization !!&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;: If your class implements &lt;em&gt;Serializable&lt;/em&gt; interface or inherits some class which implements this interface, then this code can be broken by first &lt;strong&gt;serializing&lt;/strong&gt; the object (saving the state of the object to some file) and then &lt;strong&gt;de-serializing&lt;/strong&gt; it (retrieving back the original state). If we do not override &lt;strong&gt;readResolve() &lt;/strong&gt;method as shown below:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_QosbmeyDVxU/SlojCCBs6FI/AAAAAAAAAMw/GKFzTbrsa28/s1600-h/pic4%5B9%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="pic4" border="0" alt="pic4" src="http://lh4.ggpht.com/_QosbmeyDVxU/SlojDbPAGQI/AAAAAAAAAM0/pfjSbMItVVk/pic4_thumb%5B5%5D.jpg?imgmax=800" width="355" height="83" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;To the best of my knowledge, now I think there is no way you can create another object with all these checks implemented. Please feel free to comment &amp;amp; discuss this and point out any problems with above mentioned techniques.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-7683036674588721257?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/07/singleton-class-in-java.html</link><author>noreply@blogger.com (Agraj)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_QosbmeyDVxU/Sloi9OXcprI/AAAAAAAAAMc/NWgKQCDwatg/s72-c/pic2_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-7967377929522925233</guid><pubDate>Sat, 06 Jun 2009 16:08:00 +0000</pubDate><atom:updated>2009-06-12T12:23:45.405-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SCJP</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">uCertify</category><title>uCertify PrepKit for SCJP 5.0</title><description>&lt;div&gt;Recently, I got an exciting opportunity to review the &lt;a href="http://www.ucertify.com/download/scjp-5-0.html"&gt;&lt;b&gt;uCertify Prepkit&lt;/b&gt;&lt;/a&gt; for &lt;b&gt;SCJP 5.0&lt;/b&gt; certification.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;SCJP stands for &lt;b&gt;Sun Certified Java Professional&lt;/b&gt; and is one of the many certifications offered by &lt;a href="http://www.sun.com/"&gt;Sun Microsystems&lt;/a&gt; that makes you stand out in the crowd. In the times like these, who would not want that. :-)&lt;/div&gt;&lt;div&gt;If you want to know more about SCJP, you can look at the Sun's site &lt;a href="http://www.sun.com/training/certification/java/scjp.xml"&gt;here&lt;/a&gt; or you check &lt;a href="http://blogs.sun.com/agraj/entry/scjp_get_certified_now"&gt;this&lt;/a&gt; to see some related &lt;b&gt;FAQ's&lt;/b&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Coming back to &lt;b&gt;uCertify Preparation Kit&lt;/b&gt;, I actually found it &lt;b&gt;pretty useful &lt;/b&gt;overall, for it gives you a broad spectrum of features for a relatively smaller price. The preparation engine provides you with&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Study Notes &amp;amp; Helpful Articles&lt;/li&gt;&lt;li&gt;7 Mock Tests, each with 75 questions&lt;/li&gt;&lt;li&gt;Study &amp;amp; Learn Module&lt;/li&gt;&lt;li&gt;Ability to Bookmark and Tag Questions&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;The &lt;b&gt;Study &amp;amp; Learn Module&lt;/b&gt; provides a presentation (thus not really boring) based on each &lt;i&gt;Examination Objective &lt;/i&gt;of SCJP 5.0. Going through them before the exam could be the fastest way to &lt;b&gt;revise&lt;/b&gt; the entire &lt;b&gt;syllabus&lt;/b&gt;. This is so because SCJP has well-defined objectives (rules &amp;amp; syntax of the language) that it tests. There is no need to go beyond these objectives if you want to clear the exam.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can also &lt;b&gt;attach&lt;/b&gt; your &lt;b&gt;own notes&lt;/b&gt; with Study Notes and Articles, thus making a note of some exceptional condition or some related fact. These makes life really easy when you go through them again.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The practice exams give you a &lt;b&gt;real feel&lt;/b&gt; of the actual certification exam as it contains both&lt;i&gt; multiple choice questions &lt;/i&gt;(MCQ's) and few &lt;i&gt;drag-and-drop questions&lt;/i&gt;. Also the level of the questions are neither too high nor too low. On a personal level, when I took the first practice exam without really preparing for it, I faltered on questions related to in-built classes like Locale, Date, NumberFormat and use of native modifier and Enumerations.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;According to me, the &lt;b&gt;best feature&lt;/b&gt; is certainly the way the answers are explained once your test is over and you see the results. You can review all the answers whether correct or not, and find out the reason for the same.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;The &lt;b&gt;answers are explained&lt;/b&gt; in length, touching the &lt;b&gt;concept involved&lt;/b&gt; and focussing on why other options are wrong. This gives you a proper insight of what's going on.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;On the downside, I would have preferred a &lt;b&gt;highlighting &lt;/b&gt;feature that enables me to highlight important facts in the study notes and articles, so that next time I read only that part, which is important to me. Also, I would have loved had they allowed me to copy the code and paste it to test it but I understand that they probably can't do it for piracy issues and misuse.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Although it is an &lt;b&gt;excellent kit&lt;/b&gt; to give you a proper feel of the actual exam; it is equipped with some easy ones and some tough(read tricky) questions as well just as the actual paper would. But, do not think that you need just this PrepKit to clear the paper!!&lt;/div&gt;&lt;div&gt;What you also need is a javac(Java compiler) to &lt;b&gt;practice &lt;/b&gt;and code. I strongly recommend understanding the involved concepts and coding by hand (&lt;b&gt;not &lt;/b&gt;by IDE), then take these useful tests to gain confidence as the actual exam looks almost the same.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For more information about the other preparation kits that uCertify provides you, please visit there site &lt;a href="http://www.ucertify.com/download/"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-7967377929522925233?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/06/ucertify-prepkit-for-scjp-50.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-6821668908894724772</guid><pubDate>Mon, 01 Jun 2009 18:34:00 +0000</pubDate><atom:updated>2009-06-12T12:32:55.791-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Decompiler</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>ByteCode Back to Java Source Code !!</title><description>&lt;div&gt;I have always thought about the title of this blog entry, ever since I started learning Java. But, until recently, I had no idea about the same. I didn't knew that something &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;magical &lt;/span&gt;existed that would perform this &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;conversion&lt;/span&gt;. But now since I know a bit, I thought I better share it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Java Decompiler&lt;/span&gt;&lt;/div&gt;&lt;div&gt;As you would have guessed by now, a &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Java Decompiler&lt;/span&gt; is a computer program capable of reversing the work done by a Compiler. In essence, it can convert back the Bytecode (the .class file) into the source code (the .java file). &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;       &lt;/span&gt;There are many decompilers that exist today, but we will talk about the most widely used &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;a href="http://java.decompiler.free.fr/"&gt;JD - Java Decompiler&lt;/a&gt;&lt;/span&gt;, which is available both as a &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;a href="http://java.decompiler.free.fr/?q=jdgui"&gt;stand-alone GUI&lt;/a&gt;&lt;/span&gt; program and as an &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;a href="http://java.decompiler.free.fr/?q=jdeclipse"&gt;Eclipse-plugin&lt;/a&gt;&lt;/span&gt;.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To install and use these tools are a breeze and would not take you more than a few minutes to get accustomed to it. Hence, I would not repeat the process that's already mentioned on their site.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;One thing that we must note here is that the process of conversion might &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;NOT &lt;/span&gt;result into &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;100% exact code&lt;/span&gt;, i.e. the generated Java file might not match the actual Java code character by character. However, most of the code would be replicated but things like variable &amp;amp; function names or some other minor details may differ. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lets have a look at the JD-GUI, stand-alone tool, &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;written in C++&lt;/span&gt;;making it pretty fast to execute(decompile) and display the result. Also, it is &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;independent of the the Java Runtime&lt;/span&gt; Environment and thus no setup is required to install it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lets test the tool now. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Example Java Code:&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;public class test{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;public static void main(String[] args){&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;System.out.println("Hello world");&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span"  style="font-family:arial;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Compile it:  &lt;span class="Apple-style-span" style="font-style: italic;"&gt;javac test.java &lt;/span&gt;&lt;/div&gt;&lt;div&gt;so that we have ByteCode (&lt;span class="Apple-style-span" style="font-style: italic;"&gt;test.class&lt;/span&gt; file) with us now.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Decompile it using any one of the following ways:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;Execute the following on the command line: &lt;span class="Apple-style-span" style="font-style: italic;"&gt;jdi-gui.exe test.class&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Select 'Open File' from the menu, browse to get to the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;test.class&lt;/span&gt; file and open it&lt;/li&gt;&lt;li&gt;Drag and Drop the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;test.class&lt;/span&gt; file into the JD-GUI tool&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;All of the above situations result in generating the following Java Code, have a look:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;import java.io.PrintStream;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;public class test&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;  public static void main(String[] paramArrayOfString)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;  {&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;    System.out.println("Hello world");&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;  }&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Well just by seeing JD perform really well on this simple example, we cannot decide how efficient is this tool. But, still it is a tool that every Java Developer must be aware of. Because, in case you have &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;accidently deleted&lt;/span&gt; your &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Java &lt;/span&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;files &lt;/span&gt;and are left with only the .class files, this is the tool that can save your neck. :-)&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-6821668908894724772?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/01/bytecode-back-to-java-source-code.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-5529295135869111223</guid><pubDate>Sun, 31 May 2009 15:25:00 +0000</pubDate><atom:updated>2009-05-31T11:29:54.981-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SCJP</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Strings</category><title>Strings &amp; Java</title><description>Perhaps one of the most frequently used Class in Java is the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;String &lt;/span&gt;class. The major reason for Strings to be really poweful is the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;flexibility &lt;/span&gt;of Strings to interact with other objects with so much ease.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Strings in Java posses many &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;unique characteristics&lt;/span&gt; that differentiates it from the rest. For example, it is a well-known fact that Strings are &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Immutable&lt;/span&gt;. For those who do not know what that means is, &lt;span class="Apple-style-span" style="font-style: italic;"&gt;'String objects one created, can &lt;/span&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;never &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;be changed.'&lt;/span&gt; An example would make it more clear.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s1 = "Hello World";&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Here, s1 holds the reference to the String object on the heap containing the value "Hello World". Now, if we type&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s2 = s1.concat(", here I come");&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Now, s2 holds another object on the heap with the value "Hello World, here I come". But this doesn't change the object s1. What it rather does, it creates a &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;new object&lt;/span&gt; concatenating the new value to it and storing its reference in s2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If we would have typed,&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;s1.concat(", here I come");&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;then also a new object with value "Hello World, here I come" is created but is lost and cannot be referenced since we have not stored it somewhere. Here also, s1 remains the same and is not changed. It cannot be changed.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It would also be good to remember that to make &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Java Memory Model&lt;/span&gt; &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;more efficient&lt;/span&gt;,  Java Runtime allocates memory for Strings from "&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;String constant pool&lt;/span&gt;" rather than allocating the memory from the heap. &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;Now, when the compiler encounters any &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;string literal&lt;/span&gt;, it first searches the String constant pool for an identical string. &lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;If a match is found, the reference to the already existing identical string is returned for the new request. &lt;/li&gt;&lt;li&gt;If a match is not found, then a new string literal is created in the pool of strings. This approach works without failure since Strings are immutable in nature.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;An example can again smooth things out here:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s1 = "Agraj";&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s2 = "Agraj";&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;System.out.println(s1 == s2);              // prints &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;true &lt;/span&gt;!!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;System.out.println(s1.equals(s2));     // prints true as expected&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s1 = "Agraj";&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s2 = new String("Agraj");&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;System.out.println(s1 == s2);              // prints &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;false &lt;/span&gt;because of 'new'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;System.out.println(s1.equals(s2));     // prints true as expected&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The first example prints according to String constant pool approach. While, the second example allocates memory again to s2 because the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;new operator&lt;/span&gt; is used, which forces the compiler to allocate memory again.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;It would not hurt to remember that the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;'=='&lt;/span&gt; operator is used to compare references while the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;'equals'&lt;/span&gt; function actually compares the string value(character by character) stored inside the objects.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;On some pondering, one might think that if identical strings are not allocated same memory, then if one might change one reference to the string, then all other references would also reflect the change. For example,&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s1 = "Agraj";&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic; "&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;String s2 = "Agraj";&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;s1 = s1.toUpperCase();       // s1 = "AGRAJ" but s2="Agraj"&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;One might expect s2 also to be equal to "AGRAJ", which is not desired. But, since Strings are immutable in nature, so the toUpperCase() function creates a new string, rather than modifying the original string (it cannot modify the original string, nothing can) and assigns it to s1 while s2 still reference to the original string "Agraj".&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;StringBuffer and StringBuilder &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A person can argue that in a module that does heavy &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;String manipulation&lt;/span&gt;, there would be excess of new Strings that would be created now and then and would not be used at the end, leading to wastage of memory. And you see, this person is not entirely wrong. Strings if used in a careless manner can cause huge performance bottleneck. &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;       &lt;/span&gt;So mostly, when we have to do String manipulation a lot in our application, it is advisable to use classes such as StringBuffer &amp;amp; StringBuilder. Both of these provide the same API and behave in the same manner as Strings do, the difference being the fact that they are &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;mutable&lt;/span&gt;. Thus, you can change a StringBuffer / StringBuilder object once created. &lt;br /&gt;&lt;/div&gt;&lt;div&gt;Example:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;StringBuilder str1 = new StringBuilder("Hello");&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: arial;"&gt;str1.append(" World");&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;would change the existing object "Hello" and will not create a new object as normal String Object would have done. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Difference b/w StringBuilder &amp;amp; StringBuffer&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Both of them provide us with the same API and thus are similar except the fact that &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;StringBuffer is thread-safe&lt;/span&gt; while StringBuilder is not. &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;     &lt;/span&gt;Thus it is always advisable to use StringBuilder class, where-ever thread-safety is not a issue (which is commonly the case always). It is advisable to use StringBuilder than StringBuffer because &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;StringBuilder &lt;/span&gt;is much more &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;faster &lt;/span&gt;than its &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;thread-safe twin&lt;/span&gt;. This is so because it ignores the complications that comes along while deailing with stuff like synchronization and threads. &lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;There is much more to Strings usage in Java than what can be covered in a blog entry, but I guess I have covered some of the important points which are normally ignored by novices like me. &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-5529295135869111223?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/05/strings-java.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5223788876950011016.post-3836544341840207110</guid><pubDate>Sat, 25 Apr 2009 12:18:00 +0000</pubDate><atom:updated>2009-04-25T10:27:27.787-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Garbage Collection</category><title>Ergonomics - Garbage Collection in Java</title><description>What is &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Ergonomics &lt;/span&gt;?&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;With &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;J2SE 5.0&lt;/span&gt; onwards, Ergonomics is defined as the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;capability &lt;/span&gt;of the JVM to&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Automatically select &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Heap Size&lt;/span&gt;, &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Garbage collector&lt;/span&gt; &amp;amp; type of &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Virtual Machine&lt;/span&gt; (server or client) based on the platform on which your application is running.&lt;/li&gt;&lt;li&gt;Allowing us to &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Tune the behaviour&lt;/span&gt; of the Garbage Collector using various command line options to achieve the desired effect. &lt;/li&gt;&lt;/ul&gt;&lt;div&gt;But, the aim of Ergonomics is to &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;improve performance&lt;/span&gt; &amp;amp; &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;minimize command line tuning&lt;/span&gt; as it believes in selecting the optimum configuration for the application which is running. The values are chosen based on the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;type of machine&lt;/span&gt; and the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;operating system&lt;/span&gt; (the platform) on which the application is running.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Machines are divided into 2 categories:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Server class&lt;/span&gt; machines&lt;/li&gt;&lt;li&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Non-server class&lt;/span&gt; machines&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;According to J2SE 5.0, a &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;server class&lt;/span&gt; machine is the one which has&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;At least 2 CPU's (processors)&lt;/li&gt;&lt;li&gt;At least 2 GB of physical memory&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;All other machines with less no of processors and memory are treated as non-server class machines. As visible from the configuration, the server class machines are mostly used to run large applications (typically web applications using some server framework) &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="text-decoration: underline;"&gt;Automatic Selections&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For a &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;server class&lt;/span&gt; machine, the default values chosen are&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Type of JVM &lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;: Server JVM&lt;/div&gt;&lt;div&gt;Type of Garbage Collector  : Parallel Garbage Collector&lt;/div&gt;&lt;div&gt;Heap Size :&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;1/64th of Physical Mem Avail  &lt;=  Min Heap Size  &lt;= 1 GB &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;1/4th of Physical Mem Avail    &lt;=  Max Heap Size &lt;= 1 GB &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For a &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;non-server class&lt;/span&gt; machine, the default values chosen are&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Type of JVM &lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;   : Client JVM&lt;/div&gt;&lt;div&gt;Type of Garbage Collector     : Serial Collector&lt;/div&gt;&lt;div&gt;Heap Size :&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;Min Heap Size :    4 MB&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;Max Heap Size : 64 MB&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To explicitly make JVM treat your machine as server machine, one can do it using any of the 2 ways:&lt;/div&gt;&lt;div&gt;1. Use the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;-server&lt;/span&gt; command line option with &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;java&lt;/span&gt; command&lt;/div&gt;&lt;div&gt;2. Change the first two lines of &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;jvm.cfg&lt;/span&gt; file in your JRE_installation_directory/lib/&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;Change &lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;-client KNOWN&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;-server KNOWN&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;to&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;-server KNOWN&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;-client KNOWN&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A server JVM differs from client JVM in the sense that it might take &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;more time&lt;/span&gt; (approx. 10% more time) &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;in start-up&lt;/span&gt;, but eventually it &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;increases&lt;/span&gt; the &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;performance&lt;/span&gt; &amp;amp; the throughput of the application in the long run. &lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;While client JVM is sufficient for normal standalone applications, server JVM is best suited for large-scale applications like web applications using a server framework running on some application server.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;span class="Apple-style-span" style="text-decoration: underline;"&gt;Behavioral Tuning&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold; text-decoration: underline;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;If you really know that these automatic settings are not suitable for your application, then JVM gives you the flexibility of changing them as well.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Changing the type of VM&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Use -client for using Client VM&lt;/div&gt;&lt;div&gt;Use -server for using Server VM&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Changing the type of Garbage Collector&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Use&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;-XX:UseSerialGC&lt;span class="Apple-tab-span" style="white-space:pre"&gt;    &lt;/span&gt;Serial Collector&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;-XX:UseParallelGC&lt;span class="Apple-tab-span" style="white-space:pre"&gt;   &lt;/span&gt;Parallel Collector&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;-XX:UseParallelOldGC&lt;span class="Apple-tab-span" style="white-space:pre"&gt;  &lt;/span&gt;Parallel Compacting Collector&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;-XX:UseConcMarkSweepGC&lt;span class="Apple-tab-span" style="white-space:pre"&gt; &lt;/span&gt;CMS Collector&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Changing the Heap Size&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Use -Xms to set Minimum heap size. Ex: -Xms512 will set 512MB to be minimum heap size&lt;/div&gt;&lt;div&gt;Use -Xmx to set Maximum heap size. Ex: -Xmx724 will set 724MB to be maximum heap size.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Setting maximum &amp;amp; minimum heap size to be same takes away the flexibility away from JVM of changing the heap size according to the requirements as they arise.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Certain Goals&lt;/span&gt;&lt;/div&gt;&lt;div&gt;With these command line options, you can set certain goals for the Garbage Collector to achieve and their relative priorities. The Garbage Collector works to achieve these goals and can change the heap size and other related parameters&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Maximum Pause Time Goal&lt;/span&gt;&lt;/div&gt;&lt;div&gt;This goal can be used to set Maximum pause time that a Garbage Collector is allowed i.e. we can specify -XX:MaxGCPauseMillis=n meaning that Garbage Collector is allowed a maximum pause time of n milliseconds for each generation. And if it is not able to limit itself to n milliseconds then it must take some measures to achieve this goal. For example, it can reduce the size of the heap in order to reduce the maximum pause time.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Throughput Goal&lt;/span&gt;&lt;/div&gt;&lt;div&gt;It can be used to specify the time spend in doing Garbage Collection using -XX:GCTimeRatio=n &lt;/div&gt;&lt;div&gt;The ratio of garbage collection time to application time is 1/(1+n)&lt;/div&gt;&lt;div&gt;So, that means if we set -XX:GCTimeRatio=19, it means that a maximum of 5% of total time can be spend in Garbage Collection.&lt;/div&gt;&lt;div&gt;Default value is n=99 which means that 1% of  total time is allowed for garbage collection&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;If this goal is not met, then the size of heap is increased so that it takes more time to fill the heap and thus garbage collection occurs less frequently.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Footprint Goal&lt;/span&gt;&lt;/div&gt;&lt;div&gt;This is an interesting one. It specifies that if Maximum Pause time goal and Throughput goal have been met, then keep on decreasing the size of the heap until one of the above mentioned Goals cannot be met. Then try to meet that goal. :-)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Goal Priorities&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;This goal specifies that priority of above mentioned goals. The order of importance is &lt;/span&gt;&lt;/div&gt;&lt;div&gt;1. Max Pause Time goal&lt;/div&gt;&lt;div&gt;2. Throughout goal&lt;/div&gt;&lt;div&gt;3. Footprint goal&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Tools to evaluate GC performace&lt;/span&gt;&lt;/div&gt;&lt;div&gt;In an ideal condition, you need not worry about Garbage Collection. This is precisely the goal of Ergonomics; selecting the best possible configuration according to the machine on which the application is running. But if your application runs into memory troubles like memory leaks and OutOfMemoryError, then there exists a plethora of tools to choose from.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style=""&gt;Writing about each of them is out of scope of this document. So, I'm just giving out the names and the required links that would help you analyze the application and take proper steps&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Some Tools To Look Out for&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html"&gt;jConsole&lt;/a&gt; (Monitor you App)&lt;/div&gt;&lt;div&gt;&lt;a href="http://java.sun.com/developer/technicalArticles/Programming/HPROF.html"&gt;HPROF&lt;/a&gt; (A Heap/CPU Profiling Tool)&lt;/div&gt;&lt;div&gt;&lt;a href="http://java.sun.com/javase/6/docs/technotes/tools/share/jhat.html"&gt;jHat&lt;/a&gt; (Java Heap Analysis Tool)&lt;/div&gt;&lt;div&gt;&lt;a href="http://java.sun.com/javase/6/docs/technotes/guides/visualvm/index.html"&gt;VisualVM&lt;/a&gt; (Latest Tool and a Very Good One)&lt;/div&gt;&lt;div&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jstack.html"&gt;jStack&lt;/a&gt; (Stack Trace)&lt;/div&gt;&lt;div&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jinfo.html"&gt;jinfo&lt;/a&gt; (Configuration Info)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-weight: bold;"&gt;Disclaimer&lt;/span&gt;: The contents of this blog and of all others are totally my personal opinion about the stuff, I have written and has nothing to do with my employer :-)&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5223788876950011016-3836544341840207110?l=techno-cratic.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://techno-cratic.blogspot.com/2009/04/ergonomics-garbage-collection-in-java.html</link><author>noreply@blogger.com (Agraj)</author><thr:total>0</thr:total></item></channel></rss>

