Wednesday 15 May 2013

Java - The Set Interface

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.

The methods declared by Set are summarized in the following table:


1)    add( )  //Adds an object to the collection
   
2)    clear( ) //Removes all objects from the collection
   
3)    contains( ) //Returns true if a specified object is an element within the collection
   
4)    isEmpty( ) //Returns true if the collection has no elements
   
5)    iterator( ) //Returns an Iterator object for the collection which may be used to retrieve an object
   
6)    remove( ) //Removes a specified object from the collection
   
7)    size( ) //Returns the number of elements in the collection
   


Set have its implementation in various classes like HashSet, TreeSet, HashSet, LinkedHashSet, Following is the example to explain Set functionlaity:

/* -------------------------------------------------------------------------------------------- */

import java.util.*;

public class SetEx {

    public static void main(String[] args) {
        AddSet as = new AddSet();
        as.addset();

    }

}
class AddSet{
   
    public void addset(){
       
         int data [] = {80,5000,50,20,90,100,102,100,1000,2000,1000,50};
         Set<Integer> set = new HashSet<Integer>();
         Set<Integer> sortedset = new TreeSet <Integer>();
         Set<Integer> linkedhashset = new LinkedHashSet <Integer>();
         for(int i=0;i<data.length;i++){
         set.add(data[i]);
         sortedset.add(data[i]);
         linkedhashset.add(data[i]);
         }
         System.out.print("Set Example"+set);
         System.out.print("\nSorted Set Example"+sortedset);
         System.out.print("\nLinked Hash Set Example"+linkedhashset);
    }
   
}

Monday 29 April 2013

File Upload Validation on Size or file Extension



Upload only less then 50 kb image size and Allow only jpeg, jpg, gif ,png 
Extensions type.

 <html>
<head>
<script>
function imgdym()
  {
var img = document.getElementById('filename');

var sz =img.files[0].size;
var kbsize = sz/1024
  if(kbsize > 50){ 
alert("Image is Too large! please upload less then 50 kb")
alert("your image size is: "+kbsize+" kb" );

return false;
}
else{
 var img =img.value;
 var arraydata =img.split('.');
 var Extension =arraydata[1];
if ((Extension === 'JPG') || (Extension === 'jpg')|| (Extension === 'jpeg') || (Extension === 'JPEG') || (Extension === 'GIF') || (Extension === 'gif') || (Extension === 'png') || (Extension === 'PNG'))
{
return true;
}
{
alert('Allow only jpeg, jpg, gif ,png');
return false;
}
  } 
}
</script>
</head>
<form action="upload_action.jsp" method="post" enctype="multipart/form-data" name="frmUpload" id="frmUpload">
<table align="center" width="30%" border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<td class="textn"><input type="file" name="filename" id="filename"  accept="image/gif,image/jpeg,image/tiff" class="textbox" ></td>
</tr>
<tr>
<td class="textn">  <input type="submit" name="AddFile" class="textbox1" id="AddFile" onClick="javascript:return imgdym();" value="Update Image"/> <input type="button" name="Cancel" id="Cancel" value="Cancel" class="textbox"/>
</td>
</tr>
</table>
<center><p style="color:#ff3dff;font-size:15px; font-weight:bold;">Upload User Image</p></center>
</form>


Monday 22 April 2013

Session Tracking Example in jsp



<%@ page import="java.io.*,java.util.*" %>

<%
   // Get session creation time.
   Date createTime = new Date(session.getCreationTime());
   // Get last access time of this web page.
   Date lastAccessTime = new Date(session.getLastAccessedTime());

   String title = "Welcome Back to my website";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = new String("userID");
   String userID = new String("ABCD");

   // Check if this is new comer on your web page.
   if (session.isNew()){
      title = "Welcome to my website";
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   } 
   visitCount = (Integer)session.getAttribute(visitCountKey);
 //  out.print(visitCount);
   visitCount = visitCount + 1;
   userID = (String)session.getAttribute(userIDKey);
   session.setAttribute(visitCountKey,  visitCount);
%>
<html>
<head>
<title>Session Tracking</title>
</head>
<body>
<center>
<h2>Session Tracking</h2>
</center>
<table border="1" align="center"> 
<tr bgcolor="#949494">
   <th>Session info</th>
   <th>Value</th>
</tr> 
<tr>
   <td>id</td>
   <td><% out.print( session.getId()); %></td>
</tr> 
<tr>
   <td>Creation Time</td>
   <td><% out.print(createTime); %></td>
</tr> 
<tr>
   <td>Time of Last Access</td>
   <td><% out.print(lastAccessTime); %></td>
</tr> 
<tr>
   <td>User ID</td>
   <td><% out.print(userID); %></td>
</tr> 
<tr>
   <td>Number of visits</td>
   <td><% out.print(visitCount); %></td>
</tr> 
</table> 
</body>
</html>

Friday 19 April 2013

Add Animated marks to Google Maps in the web page



<head>
<title>Add Animated marker to Google Maps in asp.net website</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
</script>
<script type="text/javascript">
function initialize() {
var myLatlng = new google.maps.LatLng(28.635308000000000000,77.224960000000010000)
var mapOptions = {
center: myLatlng,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
marker: true
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
animation:google.maps.Animation.BOUNCE,
title: "New Delhi"
});
marker.setMap(map);
}
</script>
</head>
<body onload="initialize()">
<form id="form1" runat="server">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
</form>
</body>
</html>