Tuesday, March 20, 2012
Connecting to Database
a) Loading the driver : To load the driver, Class. forName() method is used. Class. forName(”sun. jdbc. odbc. JdbcOdbcDriver”); When the driver is loaded, it registers itself with the java. sql. DriverManager class as an available database driver.
b) Making a connection with database: To open a connection to a given database, DriverManager. getConnection() method is used. Connection con = DriverManager. getConnection (”jdbc:odbc:somedb”, “user”, “password”);
c) Executing SQL statements : To execute a SQL query, java. sql. statements class is used. createStatement() method of Connection to obtain a new Statement object. Statement stmt = con. createStatement(); A query that returns data can be executed using the executeQuery() method of Statement. This method executes the statement and returns a java. sql. ResultSet that encapsulates the retrieved data: ResultSet rs = stmt. executeQuery(”SELECT * FROM some table”);
d) Process the results : ResultSet returns one row at a time. Next() method of ResultSet object can be called to move to the next row. The getString() and getObject() methods are used for retrieving column values: while(rs. next()) { String event = rs. getString(”event”); Object count = (Integer) rs. getObject(”count”);
Interface
No ( there is no point in defining a private variable/methods in the interface)
Can an interface have an abstract method defined ?
YES
Can a method in an interface be static or final
NO
Can a variable in an interface be static
YES
Setting up TOMCAT on Unix
How To Install Apache Server on your Unix Account
Following are steps to install Apachee Server in your area and you get the chance to become your own server administrator
Step 1: visit http://www.apache.org/
Step 2: Click on Apache Server (seventh item from the left).
Step 3: Click on Download.
Step 4: Click on apache_1.3.28.tar.z this the 12th item from the left and it is for Unix. Remember the place where you saved it on your PC. For the sake of explanation assume that you saved it on the desk_top. Thus on your desk_top now you have apache_1.3.28.tar.z . Notice z tells that the file is zipped and tar tells that the file is an archieve file contains more than one file.
Step 5: log in into your Unix account.
Step 6: type the following two commands to create apache directory and to make it to be the current working directory
mkdir apache
cd apache
Step 7: Use an FTP facility such as CuteFTP 32 or lite Ftp to transfer apache_1.3.28.tar.z from the desk_top to your directory in Unix. To achieve that do the following items:
a. click on the FTP facility
b. Login into your Unix account on FTP,.
c. Click on apache directory in your home directory (in FTp).
d. Click on C: then click on WINDOWS, click on desk_top, click on apache_1.3.28.tar.z on some FTP
facilities you also have to click on -->> to transfer it from desk top to your account. Now you have
apache_1.3.28.tar.z in apache directory on your home directory. Close the FTP facility.
Step 8: Make sure you are at apache directory by typing pwd command. If you are not at apache directory type
command cd apache.
Step 9: To unzip the file issue the command
gunzip apache_1.3.28.tar.z
Now the file is unzipped and if you type ls command you see you have only apache_1.3.28.tar file in apache
directory.
Step 10: To extract files from apache_1.3.28.tar type the following command:
tar xvf apache_1.3.28.tar
Notice xvf are options to tar command.
Step 11: Since files are extracted from apache_1.3.28.tar, now delete apache_1.3.28.tar file by typing:
rm apache_1.3.28.tar
Step 12: type the following command
Change directory to Apache_1.3.28 by executing
cd Apache_1.3.28
Now execute the command
./configure --prefix=/usr/users/USERDIR/apache
Notice replace USERDIR by your home director. Thus this command for user ks6241 will be:
./configure --prefix=/usr/users/students/ks6241/apache
Step 13: Type the command
make
Step 14: Type the command
make install
Step 14a: Type the command - this will save you a significant amount of disk space!
make clean
Step 15: Edit apache/conf/httpd.conf by using an editor such as pico or vi. Assume that we use pico editor, then type
pico apache/conf/httpd.conf
make the following changes while you are at pico:
Listen 3000 Notice any port number above 1000 will be fine.
Now make the following changes:
MinSpareServers 1
MaxSpareServers 2
StartServers 1
If you done every right the following message. Notice the installation takes approximately 40 minutes.
--------------------------------------------------------------------
| You now have successfully built and installed the
| Apache 1.3 HTTP server. To verify that Apache actually
| works correctly you now should first check the
| (initially created or preserved) configuration files
|
| /usr/users/cis/khailany/apache/conf/httpd.conf
|
| and then you should be able to immediately fire up
| Apache the first time by running:
|
| /usr/users/cis/khailany/apache/bin/apachectl start
|
| Thanks for using Apache. The Apache Group
| http://www.apache.org/
----------------------------------------------------------------------
After installing apache server you have to make the following changes
1. Edit 'httpd.conf' file. Assuming that you are at your home directory.
type
pico apache/conf/httpd.conf
use other path if you 'httpd.conf' is in another directory
Notice in pico control c tells in which line the editor is.
2. Comment "Listen" by using '#', since "Listen" is not being used
3. Find value "Port" (line 236) and make sure it is uncommented. The default value is '8080' which is the EMU unix web server, and therefore can not be used. Change that value to a UNIQUE value that is specific to you server.
Notice to find all port numbers which are used type the command:
netstat -a
4. Find values "User" and "Group" (line 250). Change them to your own username and group.
5. Make sure that "world" can access your apache/htdocs directory which is root directory for html web pages. In other words change the protections of apache/htdocs by typing chmod 755 apache/htdocs
6. The default cgi-bin directory is apache/cgi-bin. To change it go to line# 535 and change the value of the directory here and couple of line below to the one that you want to be your cgi-bin.
7. Close pico
8. If your apache server is running stop it by typing
apache/bin/apachectl stop
9. Restart apache by typing
apache/bin/apachectl start
Notice: If someone using your server you may not want to stop it immediately. In such cases use gracefull
command by typing
apache/bin/apachectl gracefull
Monday, November 7, 2011
Overriding Equals method and hashcode
Friday, August 19, 2011
Cookies In and out
http://www.quirksmode.org/js/cookies.html
Cookies
Loading site navigation; please wait...
See section 6G of the book.
This script was originally written by Scott Andrew. Copied and edited by permission.
This article has been translated into French
On this page I give three functions to save, read and erase cookies. Using these functions you can manage cookies on your site.
First an introduction to cookies, and a summary of document.cookie, followed by an example. Then come the three functions and their explanation.
Cookies
Cookies were originally invented by Netscape to give 'memory' to web servers and browsers. The HTTP protocol, which arranges for the transfer of web pages to your browser and browser requests for pages to servers, is state-less, which means that once the server has sent a page to a browser requesting it, it doesn't remember a thing about it. So if you come to the same web page a second, third, hundredth or millionth time, the server once again considers it the very first time you ever came there.
This can be annoying in a number of ways. The server cannot remember if you identified yourself when you want to access protected pages, it cannot remember your user preferences, it cannot remember anything. As soon as personalization was invented, this became a major problem.
Cookies were invented to solve this problem. There are other ways to solve it, but cookies are easy to maintain and very versatile.
How cookies work
A cookie is nothing but a small text file that's stored in your browser. It contains some data:
- A name-value pair containing the actual data
- An expiry date after which it is no longer valid
- The domain and path of the server it should be sent to
As soon as you request a page from a server to which a cookie should be sent, the cookie is added to the HTTP header. Server side programs can then read out the information and decide that you have the right to view the page you requested or that you want your links to be yellow on a green background.
So every time you visit the site the cookie comes from, information about you is available. This is very nice sometimes, at other times it may somewhat endanger your privacy. Fortunately more and more browsers give you the opportunity to manage your cookies (deleting the one from the big ad site, for example).
Cookies can be read by JavaScript too. They're mostly used for storing user preferences.
name-value
Each cookie has a name-value pair that contains the actual information. The name of the cookie is for your benefit, you will search for this name when reading out the cookie information.
If you want to read out the cookie you search for the name and see what value is attached to it. Read out this value. Of course you yourself have to decide which value(s) the cookie can have and to write the scripts to deal with these value(s).
Expiry date
Each cookie has an expiry date after which it is trashed. If you don't specify the expiry date the cookie is trashed when you close the browser. This expiry date should be in UTC (Greenwich) time.
Domain and path
Each cookie also has a domain and a path. The domain tells the browser to which domain the cookie should be sent. If you don't specify it, it becomes the domain of the page that sets the cookie, in the case of this page www.quirksmode.org.
Please note that the purpose of the domain is to allow cookies to cross sub-domains. My cookie will not be read by search.quirksmode.org because its domain is www.quirksmode.org . When I set the domain to quirksmode.org, the search sub-domain may also read the cookie.
I cannot set the cookie domain to a domain I'm not in, I cannot make the domain www.microsoft.com . Only quirksmode.org is allowed, in this case.
The path gives you the chance to specify a directory where the cookie is active. So if you want the cookie to be only sent to pages in the directory cgi-bin, set the path to /cgi-bin. Usually the path is set to /, which means the cookie is valid throughout the entire domain.
This script does so, so the cookies you can set on this page will be sent to any page in the www.quirksmode.org domain (though only this page has a script that searches for the cookies and does something with them).
document.cookie
Cookies can be created, read and erased by JavaScript. They are accessible through the property document.cookie. Though you can treat document.cookie as if it's a string, it isn't really, and you have only access to the name-value pairs.
If I want to set a cookie for this domain with a name-value pair 'ppkcookie1=testcookie' that expires in seven days from the moment I write this sentence, I do
document.cookie =
'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'
- First the name-value pair ('
ppkcookie1=testcookie') - then a semicolon and a space
- then the expiry date in the correct format ('
expires=Thu, 2 Aug 2001 20:47:11 UTC') - again a semicolon and a space
- then the path (
path=/)
This is a very strict syntax, don't change it! (Of course the script manages these dirty bits for you)
Also, even though it looks like I'm writing this whole string to the string document.cookie, as soon as I read it out again I only see the name-value pair:
ppkcookie1=testcookie
If I want to set another cookie, I again do
document.cookie =
'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
The first cookie is not overwritten, as it would when document.cookie would be a real string. Instead the second one is added to document.cookie, so if we read it out we get
ppkcookie1=testcookie; ppkcookie2=another test
If I reset a cookie
document.cookie =
'ppkcookie2=yet another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
the old cookie is overwritten and document.cookie reads
ppkcookie1=testcookie; ppkcookie2=yet another test
To read out a cookie you have to treat document.cookie as a string and search for certain characters (semicolons, for instance) and for the cookie name. I'll explain how to do it below.
Finally, to remove a cookie, set it with an expiry date before today. The browser sees that the cookie has expired and removes it.
document.cookie =
'ppkcookie2=yet another test; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/'
Example
If you're thoroughly confused by all this strange syntax, try the example below. You can set two cookies, ppkcookie1 and ppkcookie2. Fill in the desired value in the text box.
Create cookie 1
Read cookie 1
Erase cookie 1.
Create cookie 2
Read cookie 2
Erase cookie 2.
For comparision, read out document.cookie.
I set the cookies to remain active for seven days. If you return to this page within that time, you'll get an alert that the cookie(s) is/are still active. Try it by setting a cookie, then reloading this page.
The scripts
These are the three scripts you need.
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Explanation
The functions are not very difficult, the hardest part is creating the correct syntax for setting a cookie.
createCookie
When calling createCookie() you have to give it three bits of information: the name and value of the cookie and the number of days it is to remain active. In this case the name-value pair should become ppkcookie=testcookie and it should be active for 7 days.
createCookie('ppkcookie','testcookie',7)
If you set the number of days to 0 the cookie is trashed when the user closes the browser. If you set the days to a negative number the cookie is trashed immediately.
The function receives the arguments and starts doing its job.
function createCookie(name,value,days) {
First of all see if there is a days value. If there isn't we don't need to do the time calculation.
if (days) {
If there is, create a new Date object containing the current date.
var date = new Date();
Now get the current Time (in milliseconds) and add the required number of days (in milliseconds). Set the Time of the date to this new value, so that it now contains the date in milliseconds that the cookie should expire.
date.setTime(date.getTime()+(days*24*60*60*1000));
Set the variable expires to this date in the UTC/GMT format required by cookies.
var expires = "; expires="+date.toGMTString();
}
If 0 is passed to the function, expires is not set and the cookie expires when the user closes his browser..
else var expires = "";
Finally write the new cookie into document.cookie in the correct syntax.
document.cookie = name+"="+value+expires+"; path=/";
}
Cookie created.
readCookie
To read out a cookie, call this function and pass the name of the cookie. Put the name in a variable. First check if this variable has a value (if the cookie does not exist the variable becomes null, which might upset the rest of your function), then do whatever is necessary.
var x = readCookie('ppkcookie1')
if (x) {
[do something with x]
}
The function receives the argument and starts.
function readCookie(name) {
We're going to search for the name of the cookie, followed by an =. So create this new string and put it in nameEQ:
var nameEQ = name + "=";
Then split document.cookie on semicolons. ca becomes an array containing all cookies that are set for this domain and path.
var ca = document.cookie.split(';');
Then we go through the array (so through all cookies):
for(var i=0;i < ca.length;i++) {
Set c to the cookie to be checked.
var c = ca[i];
If the first character is a space, remove it by using the substring() method. Continue doing this until the first character is not a space.
while (c.charAt(0)==' ') c = c.substring(1,c.length);
Now string c begins with the name of the current cookie. If this is the name of the desired cookie
if (c.indexOf(nameEQ) == 0)
we've found what we were looking for. We now only need to return the value of the cookie, which is the part of c that comes after nameEQ. By returning this value we also end the function: mission accomplished.
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
If, after having gone through all cookies, we haven't found the name we're looking for, the cookie is not present. We return null.
return null;
}
Cookie read.
eraseCookie
Erasing is extremely simple.
eraseCookie('ppkcookie')
Pass the name of the cookie to be erased
function eraseCookie(name) {
and call createCookie() to set the cookie with an expiry date of one day ago.
createCookie(name,"",-1);
}
The browser, seeing that the expiry date has passed, immediately removes the cookie.
Sunday, August 14, 2011
Tuesday, July 12, 2011
Collections


Collection— the root of the collection hierarchy. A collection represents a group of objects known as its elements. TheCollectioninterface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform doesn't provide any direct implementations of this interface but provides implementations of more specific subinterfaces, such asSetandList. Also see The Collection Interface section.Set— a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine. See also The Set Interface section.List— an ordered collection (sometimes called a sequence).Lists can contain duplicate elements. The user of aListgenerally has precise control over where in the list each element is inserted and can access elements by their integer index (position). If you've usedVector, you're familiar with the general flavor ofList. Also see The List Interface section.Queue— a collection used to hold multiple elements prior to processing. Besides basicCollectionoperations, aQueueprovides additional insertion, extraction, and inspection operations.Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements' natural ordering. Whatever the ordering used, the head of the queue is the element that would be removed by a call to
removeorpoll. In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. EveryQueueimplementation must specify its ordering properties. Also see The Queue Interface section.Map— an object that maps keys to values. AMapcannot contain duplicate keys; each key can map to at most one value. If you've usedHashtable, you're already familiar with the basics ofMap. Also see The Map Interface section.
Set and Map: SortedSet— aSetthat maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. Also see The SortedSet Interface section.SortedMap— aMapthat maintains its mappings in ascending key order. This is theMapanalog ofSortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. Also see The SortedMap Interface section.