Friday, July 24, 2009

My experiments with web

With my sudden new found interest to become a better developer, and to learn advanced techniques, I am thinking to try out some code samples which I want to use in my pet project. I would post those examples under 'My experiments' tag...

Wednesday, June 24, 2009

Random Password in Java

Recently, I implemented creating a random password for my pet application using Coldfusion. I implemented based on the example from Ben Nadel Web Page. Since, I have started exploring deep into Java technology, I thought I would try the example in Java. Found it a little difficult in the beginning, but kind of completed the code. took extensive help from google. The code is as below: I copy pasted the shuffle code. Finding no time to write one. Suggestions are welcome.

/*
* The purpose of the class is to generate a random password whenever a method inside it is run
* The password should be 10 characters in length, must contain one Uppercase letter, one lower case letter, one
* numeric digit, and the remaining 7 characters can be a combination of the three types, and special character.
* /

/**
*
* @fun and learning
*/

import java.util.Random;

public class RandomPassword {

//function to return a randome value from an array.
public static char get(char[] array) {
Random ranpwd = new Random();
int ret = ranpwd.nextInt(array.length);
return array[ret];
}

//once the 10digit password is created according to the rules, this method is executed to shuffle the characters
public static String shuffle1(String passwd) {
if (passwd.length() <= 1) {
return passwd;
}

int split = passwd.length() / 2;
String temp1 = shuffle1(passwd.substring(0, split));
String temp2 = shuffle1(passwd.substring(split));

if (Math.random() > 0.5) {
return temp1 + temp2;
} else {
return temp2 + temp1;
}
}

public static void main(String[] args) {

//make an array of lower case, upper case aplhabets, and arrays of numbers and special characters
char[] lowerCaseChar = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char[] upperCaseChar = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] specialChars = {'~', '!', '@', '#', '$', '%', '^', '&', '*'};

char lower1 = RandomPassword.get(lowerCaseChar);
System.out.print(lower1);
char upper1 = RandomPassword.get(upperCaseChar);
System.out.print(upper1);
char numbers1 = RandomPassword.get(numbers);
System.out.println(numbers1);
int lowLength = lowerCaseChar.length;
int upLength = upperCaseChar.length;
int numLength = numbers.length;
int spLength = specialChars.length;

char[] combineAll = new char[lowLength + upLength + numLength + spLength];
System.arraycopy(lowerCaseChar, 0, combineAll, 0, lowLength);
System.arraycopy(upperCaseChar, 0, combineAll, lowLength, upLength);
System.arraycopy(numbers, 0, combineAll, upLength + lowLength, numLength);
System.arraycopy(specialChars, 0, combineAll, upLength + lowLength + numLength, spLength);

char sevenChar = 0;
String finalPwd = "";
for (int count = 1; count < 8; count++) {
sevenChar = RandomPassword.get(combineAll);
finalPwd = finalPwd + sevenChar;

if (count == 7) {
finalPwd = finalPwd + lower1 + upper1 + numbers1;
}
}

System.out.println(finalPwd);
String passwd = shuffle1(finalPwd);
System.out.println("Shuffled String = " + passwd);

Sunday, June 14, 2009

Started a New Project

I have always wanted to start my own project as a beginner and have it reviewed by the pros. So to satisfy my desire to do so, I have started to do my Master's project in Coldfusion( which I did in J2EE, Hibernate, and Spring). I have learned that Spring corresponds to Coldspring and Hibernate corresponds to Transfer in Coldfusion world. As a Masters student, I was in a hurry to finish my project, but never really understood the purposes of Spring and Hibernate. Right now, I am developing the project totally using Coldfusion (CFCs model). Once I finish the project to an extent, would have a pro review it. I am making the application to be used by three users. I developed a prototype for each user. Its kind of getting interesting, as I am trying to touch different aspects of Web Technologies, viz, Coldfusion, JavaScript, CSS, HTML until now. Would like to learn lot of stuff, and make this project as user friendly as possible. Also planning to step into the world of Java. I would like to begin by exploring the Object Oriented Principles, and exploring examples in the journey. I am one excited human being....

Saturday, March 21, 2009

HTML drop down

A small tip for future reference for myself...Beginners might find it useful too...this is a way to use this.options in select onchange.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Understanding This</title>
<script type="text/javascript" language="javascript">
function onSelectdd(value)
{
if(value == 'one')
{
alert("Calling One");
}
if(value == 'two')
{
alert("Calling second one");
}
if(value == 'three')
{
alert("Calling three");
}
if(value == 'four')
{
alert("Calling four");
}
}

</script>
</head>

<body>
<table>
<tr>
<td>Select an option:
<select name="dropdowns" onchange="onSelectdd(this.options[this.options.selectedIndex].value)">
<option value="select">Select an option</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
</td>
</tr>
</table>
</body>
</html>

Sunday, March 8, 2009

Check box and javascript

The following example shows various validations for a checkbox using Javascript. A brief explanation of what each function does is as follows:

a) select_checkall(): Is a function for selecting or deselecting checkboxes at one go
b) updateSelect(): Is a function if you manually check all the checkboxes, the select all checkbox is automatically selected, or if all the checkboxes are selectted and the user deselect atleast one checkbox, the "select/deselect" check box is unchecked automatically
c)checkone(): returns error if the user submits form without selecting atleast one check box.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Check Box</title>
</head>
<script language="javascript" type="text/javascript">
function select_checkall()
{
for(i=0;i<document.all.example_chkbox.length;i++)
{
document.all.example_chkbox[i].checked = document.all.chk_example.checked;
}
}

function updateSelect()
{
var isSelected = true;
for(i=0;i<document.all.example_chkbox.length;i++)
{
if(document.all.example_chkbox[i].checked == false)
{
isSelected = false;
}
}
document.all.chk_example.checked = isSelected;

}

function checkOne()
{
var count = 0;
for(i=0;i<document.all.example_chkbox.length;i++)
{
if(document.all.example_chkbox[i].checked == true)
{
count += 1;
}
}
if(count < 1)
{
alert("Please check one box");
return false;
}
}
</script>
<body>
<form name="myform" action="something.cfm" method="post">
<table>
<tr>
<td>
Please choose or check the boxes....
<input type="checkbox" name="chk_example" value="checkall" onclick="select_checkall()" />Select/Deselect All

<input type="checkbox" name="example_chkbox" value="checkbox1" onclick="updateSelect()" />Checkbox1

<input type="checkbox" name="example_chkbox" value="checkbox2" onclick="updateSelect()" />Checkbox2

<input type="checkbox" name="example_chkbox" value="checkbox3" onclick="updateSelect()" />Checkbox3
</td>
<td><input type="submit" name="submit" value="Click Me!!!" onclick="checkOne();" /></td>
</tr>
</table>
</form>
</body>
</html>

Friday, March 6, 2009

drop down select and onchange...

This post deals with how to create select drop downs and how to change the display of the page when the user selects a particular entry using javascript and onchange attribute of the select tag. The example is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Drop Downs</title>
</head>
<script language="javascript" type="text/javascript">
function showdata_onchange(choice_value)
{
if(choice_value == 'choice2')
{
document.getElementById('show_data').style.display = 'block';
}
else
{
document.getElementById('show_data').style.display = 'none';
}
}
</script>
<body>
<form name="myform" action="getvalues.cfm" method="post">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>

<td>Please select a choice:
<select name="selectChoice" onchange="showdata_onchange(this.options[this.selectedIndex].value)">
<option value="choice">Select a choice</option>
<option value="choice1">choice1</option>
<option value="choice2">choice2</option>
<option value="choice3">choice3</option>
<option value="choice4">choice4</option>
</select>
</td>
</tr>
</table>
<span id="show_data" style="display:none">
<p>Hey am shown for this choice</p>
</span>

</form>
</body>
</html>

Sunday, February 22, 2009

<cftooltip> example

Has anyone encountered a situation, where you had to pull some data from the database to output it onto the web page and then when the user hovers on any of the row, they should see something like a small summary about what it means. If no one has, I did encounter such a situation for my project. I tried using the 'title' attribute, and when I posted my problem onto a forum, I got some suggestions about JavaScript tool tips, which requires downloading the files into the web root and calling them using <script src=""></script>. Then after sometime, I simply typed coldfusion tool tips and I came across this wonderful coldfusion tag called <cftooltip> which requires just one line of code without the need for going o Javascript, AJAX. Atleast for a beginner like me, learning so many things for doing one task was overwhelming. The following is a small example I ran.

The database I am using is the sample database from the book "Getting Started with Coldfusion Volume 1 by Ben Forta and Raymond Camden". The following is the code example:

cftooltip.cfm:

<!--- Query the database table FILMS --->
<cfquery datasource="ows" name="showToolTip">
SELECT MOVIETITLE, PITCHTEXT, SUMMARY FROM FILMS
</cfquery>

<!--- Output the results you just got from querying the database --->
<cfoutput>
<!--- Output them in table format--->
<table>
<tr>
<cfloop query="showToolTip">
<!--- Use cftooltip for showing PITCHTEXT AND SUMMARY when the user hovers over the Movie name --->

<cftooltip tooltip="">#PITCHTEXT#</cfoutput><br />MOVIE SUMMARY IS:<cfoutput>#SUMMARY#</cfoutput>">
<td>#MOVIETITLE#</td><br />
</tr>
</table>
</cftooltip>
</cfloop>
</cfoutput>