TechnoBase DBMS | MySQL | Oracle | Microsoft .NET | Mainframes
Unix/Linux
The Unix kernel ---- Process management ---- Memory management The file system ---- File types System management ---- Hardware configuration ---- Software configuration ---- Viewing system information ---- systemd Processes ---- Daemon processes The Shell language ---- Shell commands -------- File system commands -------- Process control commands -------- Search commands -------- System information commands -------- User management commands -------- Miscellaneous commands ---- Language constructs ---- Special built-in utilities ---- Shell scripts Text editors ---- The vi editor Remote connectivity ---- TCP/UDP port numbers ---- Telnet Troubleshooting/Recovery ---- Error codes Tools/Utilities ---- htop C/C++ programming ---- The C++ language -------- Datatypes -------- Variables -------- Operators -------- Arrays -------- Statements -------- Functions -------- Pointers -------- Preprocessor directives -------- Classes -------- Structures ---- C++ Programming -------- Exception handling -------- File handling -------- Process control Java programming ---- Datatypes ---- Classes -------- Class modifiers ---- Packages -------- The java.io package -------- The java.lang package -------- The java.util package ---- Exception handling ---- File handling ---- Networking ---- Serialization ---- Remote Method Invocation (RMI) ---- Thread Management ---- JDBC ---- JavaBeans ---- Enterprise JavaBeans (EJB) -------- Session Beans -------- Entity Beans -------- Message-driven Beans ---- Servlets -------- The lifecycle of servlets -------- Database access ---- Java Server Pages (JSP) -------- JSP Elements -------- JSP Actions -------- JSP and Databases ---- JFC/Swing -------- Swing Components -------- Event handling -------- Programming in Swing


The Unix operating system was developed in the AT&T Bell Laboratories over the years 1969-74 in an effort to provide a multiuser, multitasking system for programmers. AT&T licensed Unix to outside parties in the late 1970s, leading to a variety of both academic and commercial Unix variants from vendors including University of California, Berkeley (BSD), Microsoft (Xenix), Sun Microsystems (SunOS/Solaris), HP/HPE (HP-UX) and IBM (AIX).

The Unix OS comprises three parts - the kernel, the standard utility programs and the system configuration files.

Linux is a family of open-source Unix-like operating systems based on the Linux kernel, an OS kernel first released in 1991. Linux is typically packaged as a Linux distribution (distro), which includes the kernel and supporting system software and libraries - most of which are provided by third parties - to create a complete OS, designed as a clone of Unix.

Thousands of Linux distributions exist and popular Linux distributions include Debian, Fedora Linux, Linux Mint, Arch Linux and Ubuntu, while commercial distributions include Red Hat Enterprise Linux, SUSE Linux Enterprise and ChromeOS.

THE UNIX KERNEL

The kernel is the core of the Unix OS. It is a large program that is loaded into memory when the machine is turned on and it controls the allocation of hardware resources from that point forward. The kernel knows what hardware resources are available (like processor(s), on-board memory, disk drives, network interfaces etc.) and it has the necessary programs to talk to all the devices connected to it.

The kernel provides an execution environment in which applications may run by implementing a set of services and corresponding interfaces. Applications use those interfaces and do not usually interact directly with hardware resources.

The kernel handles the following operations.
1. schedule the running of user and other processes
2. allocating memory
3. managing the swapping between memory and disk
4. moving data to and from the peripherals
5. receive service requests from the processes and honor them

Process management

A CPU can run in either user mode or kernel mode. When a program is executed in user mode, it cannot directly access the kernel data structures or the kernel programs. When an application executes in Kernel Mode, these restrictions no longer apply. Each CPU model provides special instructions to switch from user to kernel mode and vice versa. A program usually executes in user mode and switches to kernel mode only when requesting a service provided by the kernel. When the kernel has satisfied the program’s request, it puts the program back in user mode.

The kernel is reentrant i.e several processes may be executing in kernel mode at the same time. Each process runs in its private address space. A process running in user mode refers to private stack, data and code areas. When running in kernel mode, the process addresses the kernel data and code area and uses another stack.

Semaphores

Semaphores are a widely used mechanism, effective in both uniprocessor and multiprocessor systems, for kernel reentrancy and process synchronization. A semaphore is a counter associated with a data structure that is checked by all kernel threads before accessing the data structure. Each semaphore may be viewed as an object composed of - an integer variable, a list of waiting processes and two atomic methods - down() and up().

Each data structure to be protected has its own semaphore, which is initialized to 1. When a kernel control path wishes to access the data structure, it executes the down() method on the semaphore and decrements its value. If the value of the semaphore is not negative, access to the data structure is granted, else the process that is executing the kernel control path is added to the semaphore list and blocked. When another process executes the up() method on that semaphore, one of the processes in the semaphore list is allowed to proceed.

Spin locks

In multiprocessor systems, semaphores are not always the best solution to the synchronization problems. Some kernel data structures should be protected from being concurrently accessed by kernel control paths that run on different CPUs. In this case, if the time required to update the data structure is short, a semaphore could be very inefficient. To check a semaphore, the kernel must insert a process in the semaphore list and then suspend it. Since both operations are relatively expensive, in the time it takes to complete them, the other kernel control path could have already released the semaphore.

In these cases, multiprocessor operating systems use spin locks. A spin lock is very similar to a semaphore, but it has no process list. When a process finds the lock closed by another process, it 'spins' around repeatedly, executing a tight instruction loop until the lock becomes open.

Memory Management

Memory management is by far the most complex activity in a kernel.

Kernel Memory Allocator

The Kernel Memory Allocator (KMA) is a subsystem that tries to satisfy the requests for memory areas from all parts of the system. Some of these requests come from other kernel subsystems needing memory for kernel use and some come via system calls from user programs to increase address spaces of their processes. A good KMA should have the following features
  • it must be fast, since it is invoked by all kernel subsystems (including the interrupt handlers)
  • it should minimize the amount of wasted memory
  • it should try to reduce the memory fragmentation problem
  • it should be able to cooperate with the other memory management subsystems to borrow and release page frames from them

    RAM usage

    The Unix OS distinguishes between two portions of the RAM. A few MB is dedicated to storing the kernel image i.e the kernel code and the kernel static data structures. The remaining portion of RAM is usually handled by the virtual memory system and is used in three possible ways
  • to satisfy kernel requests for buffers, descriptors and other dynamic kernel data structures
  • to satisfy process requests for generic memory areas and for memory mapping of files
  • to get better performance from disks and other buffered devices by means of caches One problem that must be solved by the virtual memory system is memory fragmentation. Ideally, a memory request should fail only when the number of free page frames is too small. However, the kernel is often forced to use physically contiguous memory areas and the memory request could fail even if there is enough memory available but it is not available as one contiguous chunk.

    Virtual memory

    Virtual memory acts as a logical layer between the application memory requests and the hardware Memory Management Unit (MMU). Virtual memory has many purposes and advantages
  • several processes can be executed concurrently
  • it is possible to run applications whose memory needs are larger than the available physical memory
  • processes can execute a program whose code is only partially loaded in memory
  • each process is allowed to access a subset of the available physical memory
  • processes can share a single memory image of a library or program
  • programs can be relocatable i.e they can be placed anywhere in physical memory
  • programmers can write machine-independent code, since they do not need to be concerned about physical memory organization

    The main ingredient of a virtual memory subsystem is the notion of virtual address space. The set of memory references that a process can use is different from physical memory addresses. When a process uses a virtual address, the kernel and the MMU cooperate to locate the actual physical location of the requested memory item.

    Out of memory (OOM) management

    An OOM killer is a function of the Linux kernel that is intended to kill processes that are requesting more memory that the OS can allocate, so that the system can survive. The function applies some heuristics to decide which process to kill when the system is in such a state. The process monopolizing the most amount of memory and not releasing enough of it is generally the most likely to be killed.

    The OOM killer when invoked, writes details about the process that was killed and the memory usage to the system log.
    	total-vm	total virtual memory used by the process (in 4kB pages)
    	rss		resident memory used (RAM)
    	anon-rss	resident anonymous memory
    	file-rss	resident file mappings
    	shmem-rss	resident shmem memory

    THE FILE SYSTEM

    Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at the base of the file system that contains other files and directories.

    The Unix filesystem has the following properties
  • it has a root directory (/) that contains other files and directories
  • each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier, typically called an inode. By convention, the root directory has an inode number of 2 and the lost+found directory has an inode number of 3. Inode numbers 0 and 1 are not used. File inode numbers can be seen by specifying the -i option to ls command
  • it is self-contained. There are no dependencies between one file system and another

    File types

    Every item in the file system can be defined as belonging to one of four possible types:

  • Ordinary files

    Ordinary files can contain text, data or program information. An ordinary file cannot contain another file or directory. An ordinary file can be thought of as a one-dimensional array of bytes. It can be either an ASCII file or a binary file.

  • Directories

    Directories can be described as containers that can hold files, and other directories. A directory is actually implemented as a file that has one line for each item contained within the directory. Each line in a directory file contains only the name of the item, and a numerical reference to the location of the item. The reference is called an i-number, and is an index to a table known as the i-list. The i-list is a complete list of all the storage space available to the file system.

  • Special files

    Special files represent I/O devices, like a tty (terminal), a disk drive, or a printer. Because UNIX treats such devices as files, a degree of compatibility can be achieved between device i/o, and ordinary file i/o, allowing for the more efficient use of software. Special files can be either character special files, that deal with streams of characters, or block special files, that operate on larger blocks of data. Typical block sizes are 512, 1024 and 2048 bytes.

  • Links

    A link is a pointer to another file. A directory is a list of the names and i-numbers of files. A directory entry can be a hard link, in which the i-number points directly to another file. A hard link to a file is indistinguishable from the file itself. When a hard link is made, then the i-numbers of two different directory file entries point to the same inode. For that reason, hard links cannot span across file systems. A soft link (or symbolic link) provides an indirect pointer to a file. A soft link is implemented as a directory file entry containing a pathname. Soft links are distinguishable from files, and can span across file systems. Not all versions of UNIX support soft links.


    Basic directories (filesystems)

  • The root (/) file system contains basic operating system and maintenance tools. The content of this file system should be sufficient to start up the system and perform emergency maintenance and repairs if necessary

  • /usr file system contains all commands, libraries, documentation and other files that do not change during normal operation. It also contains applications that come with the Linux distribution e.g telnet, ftp
    	/usr/bin/		additional user commands
    	/usr/include/		standard system header files
    	/usr/lib/		more programming and system call libraries
    	/usr/local/		typically a place where local utilities are placed
    	/usr/man		the manual pages are kept here
  • /var file system contains files that change - spool directories, log files, lock files, temporary files and formatted (on use) manual pages

  • /home file system contains user files (users' own settings, customization files, documents, data, mail, caches, etc). The contents of this directory should be preserved on an OS upgrade

  • /proc file system contains entirely illusionary files. They do not really exist on the disk and do not take up any space there (although ls -l will show their size). Viewing them actually accesses information stored in the memory about the system
    	/dev/			where special files are kept 
    	/bin/			executable system utilities, like sh, cp, rm
    	/lib/			operating system and programming libraries
    	/tmp/			system scratch files (all users can write here)
    	/lost+found/		where the file system checker puts detached files

    The file system table

    Each file system that is mounted on a UNIX machine is accessed through its own block special file. The information on each of the block special files is kept in a system database called the file system table, and is usually located in /etc/fstab. It includes information about the name of the device, the directory name under which it will be mounted, and the read and write privileges for the device. It is possible to mount a file system as "read-only" to prevent users from changing anything.

    Back



    SYSTEM MANAGEMENT

    Configuration files

    Configuration files contain information about the structure or arrangement of specific parts of the system and are kept in the /etc directory.
    	/etc/	- system configuration files and databases
    		/etc/hosts	- IP to hostname mappings
    		/etc/httpd.conf	- has information about the Apache HTTP server (httpd)
    		/etc/group	- contains the names of valid groups and the usernames of their members. This file is owned by root and only root may modify it
    		/etc/passwd	- is the password file. Each line in the file holds all the information on a single user
    		/etc/security/limits.conf	- allows setting resource limits for users
    		/etc/syslogd.conf	- has information about the syslog daemon (syslogd), which helps to manage system and application logs
    Hardware configuration

    Partitions

    The hard disk is subdivided into partitions for security and robustness, for e.g if an application starts filling up the disk and if the disk contains a single partition, the entire system may stop functioning if the disk is full.

    There are two kinds of major partitions
  • data partition - normal system data, including the root partition containing all the data to start up and run the system
  • swap partition - expansion of the computer's physical memory, extra memory on hard disk

    All partitions are attached to the system via a mount point. The mount point defines the place of a particular data set in the file system. Usually, all partitions are connected through the root (/) partition. On this partition, directories are created and these empty directories will be the starting point of the partitions that are attached to them.

    For e.g given a partition that holds the following directories - videos/, cd-images/ and pictures/ that needs to be attached in the file system in a directory called /opt/media, the directory /opt/media should exist on the system and preferably be empty. Then, using the mount command, the partition can be attached to the system. The formerly empty directory /opt/media, now contains the files and directories that are on the mounted medium (hard disk or partition of a hard disk, CD, DVD, flash card, USB or other storage device).

    Mounting drives/partitions

    The mount command instructs the OS to make a file system available for use at a specified location (the mount point).
    	mount	- display all currently attached file systems
    	mount device directory	- attach a file system
    	
    	umount device or directory	- detach a mounted file system

    File descriptors

    A file descriptor is the Unix abstraction for an open input/output stream: a file, a network connection, a pipe (a communication channel between processes), a terminal, etc.

    A file descriptor thus fills a similar niche as a stdio FILE*. However, whereas a FILE* (like stdin or stdout) is a pointer to some object structure, a file descriptor is just an integer. For e.g, 0, 1, and 2 are the file descriptor versions of stdin, stdout, and stderr, respectively.

    To increase the number of file descriptors for all users, in the /etc/security/limits.conf file, use
    	*	hard	nofile	65535
    	*	soft	nofile	8192
    To increase the number of file descriptors for a specific user, use
    	mysql	hard	nofile	100000
    	mysql	soft	nofile	10000
    If systemd is used, then the following line needs to be appended in the [service] section of /usr/lib/systemd/system/mysqld.service
    	LimitNOFILE=100000


    Accessing a CD ROM or a floppy drive
    	mount -t auto /dev/cdrom /mnt/cdrom		- mounting a CDROM drive
    The contents of the CD appears in the directory /mnt/cdrom.
    	umount /media/cdrom		- unmount
    lsblk

    lsblk lists information about all or the specified block devices by reading the sysfs file system to gather information.
    	lsblk
    Configuring a sound card in Linux

    The soundcard can be configured by typing in sndconfig - the sound card configuring utility. In RedHat Linux, setup command can be used.
    	cdplay		- play CD
    	cdplay stop	- stop CD
    	cdplay play 4	- play 4th track
    	eject		- stop and eject the CD
    Software Configuration

    Environment variables

    The settings of environment variables tell Unix/Linux where to look for certain defaults. These variables are exported to all processes spawned by the shell. Some of the key environment variables are

    BASH - path of the BASH executable file BASH_ENV - path of the BASH environment file, which specifies BASH options BASH_VERSION - version of BASH COLUMNS - width, in characters, of console window EUID - effective user ID HISTFILE - path of the BASH command history file HISTSIZE/HISTFILESIZE - maximum number of commands/lines recorded in history file HOME - path of user's home directory HOSTNAME - name of the host IFS - field separator (white space) characters LINES - length, in lines, of console window LOGNAME - user's login name LS_COLORS - color options for ls command MAIL - specifies where mail is put OSTYPE - operating system name ('Linux') PATH - specifies directories where commands are to be located PPID - process ID of the shell's parent process PRINTER - tells Unix what to use as a default printer PS1 - command prompt string PS2 - continuation prompt string PS4 - execution trace string PWD - current working directory SHELL - path of the shell executable SHLVL - number of nested shell invocations TERM - terminal type UID - user ID USER - user name


    Getting/setting environment variables in a program

    Getting the value of an environment variable is done by using getenv().
    	#include <stdlib.h>
    	char *getenv(const char *name);
    Setting the value of an environment variable is done by using putenv().
    	#include <stdlib.h>
    	int putenv(char *string);
    Obtaining the whole list of environment variables

    A global variable, 'environ', holds a pointer to an array of pointers to environment strings, each string in the form '"NAME=value"'. A NULL pointer is used to mark the end of the array.
    	#include <stdio.h>
    	extern char **environ;
    
    	int main()
    	{
    		char **ep = environ;
    		char *p;
    		while ((p = *ep++))
    			printf("%s\n", p);
    		return 0;
    	}
    Viewing system information

    /proc is a virtual file system that is sometimes referred to as a process information pseudo-file system. It doesn't contain 'real' files but runtime system information (e.g. system memory, devices mounted, hardware configuration etc) and it can be regarded as a control and information center for the kernel.
    	cat /proc/cpuinfo	show CPU info
    	cat /proc/meminfo	show memory info
    	cat /proc/version	show version info
    systemd

    Systemd is a system and service manager for Linux OSs that provides a number of features such as parallel startup of system services at boot time, on-demand activation of daemons or dependency-based service control logic. Systemd introduces the concept of systemd units which are represented by unit configuration files located in one of the directories listed below and encapsulate information about system services, listening sockets, and other objects that are relevant to the init system.

    Available systemd Unit Types
    	Unit Type	File Extension	Description
    	---------	--------------	-----------
    	Service unit	.service	A system service
    	Target unit	.target		A group of systemd units
    	Automount unit	.automount	A file system automount point
    	Device unit	.device		A device file recognized by the kernel
    	Mount unit	.mount		A file system mount point
    	Path unit	.path		A file or directory in a file system
    	Scope unit	.scope		An externally created process
    	Slice unit	.slice		A group of hierarchically organized units that manage system processes
    	Snapshot unit	.snapshot	A saved state of the systemd manager
    	Socket unit	.socket		An inter-process communication socket
    	Swap unit	.swap		A swap device or a swap file
    	Timer unit	.timer		A systemd timer
    Systemd Unit Files Locations
    	Directory			Description
    	---------			-----------
    	/usr/lib/systemd/system/	Systemd unit files distributed with installed RPM packages.
    	/run/systemd/system/		Systemd unit files created at run time. This directory takes precedence over the directory with 
    								installed service unit files.
    	/etc/systemd/system/		Systemd unit files created by systemctl enable as well as unit files added for extending a service. 
    								This directory takes precedence over the directory with runtime unit files.

    Back



    PROCESSES

  • A process can be defined as an instance of a program running with which a particular set of data is associated so that the process can be kept track of
  • A process is started when a program is initiated (either by a user entering a shell command or by another program)
  • A process can initiate a subprocess, called a child process (and the initiating process is referred to as its parent). A child process is a replica of the parent process and shares some of its resources, but cannot exist if the parent is terminated
  • Processes can exchange information or synchronize their operation through several methods of interprocess communication (IPC)

    The INIT process

    INIT is the parent of all processes. Its primary role is to create processes from a script stored on the file /etc/inittab. This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system.

    Runlevel

    A runlevel is a software configuration of the system which allows only a selected group of processes to exist. The processes spawned by init for each of these runlevels are defined in the /etc/inittab file. Init can be in one of the 8 runlevels 0-6 or S or s. The runlevel is changed by having a privileged user run telinit, which sends appropriate signals to init, telling it which runlevel to change to.

    Runlevels 0,1 and 6 are reserved. 0 is used to halt the system, 6 is used to reboot the system and 1 is used to get the system down to single-user mode. S or s are not meant to be used directly, but more for the scripts that are executed when entering runlevel 1.

    Cron

    Cron is a linux process that performs scheduled tasks in the background. The functioning of cron is controlled by the contents of the /etc/crontab file. Among other things cron may
    - rebuild the database of files which is used when searching for files with the locate command
    - clean the /tmp directory
    - rebuild the manual pages
    - "rotate" the log files i.e. discard the oldest log files, rename the intermediate logs and create new logs
    - perform some other checkups e.g. adding fonts that were recently copied to the system

    Named pipe

    A named pipe is a special file that is used to transfer data between unrelated processes. One (or more) processes write to it, while another reads from it. Named pipes are visible in the file system and may be viewed with 'ls' like any other file. (Named pipes are also called "fifo"s - First In, First Out)
  • Named pipes may be used to pass data between unrelated processes, while normal (unnamed) pipes can only connect parent/child processes
  • Named pipes are strictly unidirectional, even on systems where anonymous pipes are bidirectional (full-duplex)

    Daemon processes

    A daemon process is a background process that does not belong to a terminal session. Most system services like network services, printing etc. are performed by daemons.

    Common daemon processes

    anacron - checks 'cron' jobs that were left out due to down time and executes them, on machines that don't run all the time - anacron will detect that during bootup
    amd - automount daemon (automatically mounts removable media)
    apmd - Advanced Power Management BIOS daemon. For use on machines, especially laptops, that support apm
    arpwatch - keeps watch for ethernet/ip address pairings
    atd - runs jobs queued by the 'at' command
    autofs - control the operation of automount daemons (competition to amd)
    bootparamd - server process that provides information to diskless clients necessary for booting
    crond - automatic task scheduler. Manages the execution of tasks that are executed at regular but infrequent intervals, such as rotating log files, cleaning up /tmp directories etc.
    cupsd - the Common Unix Printing System (CUPS) daemon. CUPS is an advanced printer spooling system which allows setting of printer options and automatic availability of a printer configured on one server in the whole network. The default printing system of Linux Mandrake.
    dhcpd - implements the Dynamic Host Configuration Protocol (DHCP) and the Internet Bootstrap Protocol (BOOTP)
    gated - routing daemon that handles multiple routing protocols and replaces routed and egpup
    gpm - useful mouse server for applications running on the Linux text console
    httpd - daemon for the Apache webserver
    inetd - listens for service requests on network connections, particularly dial-in services. This daemon can automatically load and unload other daemons (ftpd, telnetd, etc.), thereby economizing on system resources. Newer systems use xinetd instead
    isdn4linux - for users of ISDN cards
    kerneld - automatically loads and unloads kernel modules
    klogd - the daemon that intercepts and displays/logs the kernel messages depending on the priority level of the messages. The priority is (copied from /usr/include/linux/kernel.h)
    	KERN_EMERG	0	system is unusable
    	KERN_ALERT	1	action must be taken immediately
    	KERN_CRIT	2	critical conditions
    	KERN_ERR	3	error conditions
    	KERN_WARNING	4	warning condition
    	KERN_NOTICE	5	normal but significant condition
    	KERN_INFO	6	informational
    	KERN_DEBUG	7	debug-level messages
    The messages typically go to the appropriately named files in the directory /var/log/kernel
    kudzu - detects and configures new or changed hardware during boot
    keytable - loads selected keyboard map
    linuxconf - a configuration tool for Linux. Can perform various tasks at boot time to maintain the system configuration
    lpd - printing daemon.
    mcserv - server program for the Midnight Commander networking file system. It provides access to the host file system to clients running the Midnight file system. If the program is run as root it will try to get a reserved port else it will use 9876 as the port. If the system has a portmapper running, then the port will be registered with the portmapper and clients will automatically connect to the right port. If the system does not have a portmapper, then a port should be manually specified with the -p option
    named - the Internet Domain Name Server (DNS) daemon
    netfs - network filesystem mounter, used for mounting nfs, smb and ncp shares on boot
    network -activates all network interfaces at boot time by calling scripts in /etc/sysconfig/network-scripts
    nfsd - used for exporting nfs shares when requested by remote systems
    nfslock - starts and stops nfs file locking service
    numlock - locks numlock key at init runlevel change
    pcmcia - generic services for pcmcia cards in laptops
    portmap - needed for Remote Procedure Calls (RPC) to route requests between clients and servers
    postfix - mail transport agent which is a replacement for sendmail. Now the default on desktop installations of Mandrake (RedHat uses sendmail instead)
    random - saves and restores the "entropy" pool for higher quality random number generation
    routed - daemon that manages routing tables
    rstatd - kernel statistics server
    rusersd, rwalld - identification of users and "wall" messaging services for remote users
    rwhod - server which maintains the database used by the rwho(1) and ruptime(1) programs. Its operation depends on the ability to broadcast messages on a network
    sendmail - mail transfer agent, that comes with Red Hat
    smbd - the SAMBA (or smb) daemon, a network connectivity service to Windows computers on the network (hard drive sharing, printers etc.)
    squid - A http proxy with caching. Proxies relay requests from clients to the outside world, and return the results and can enable a linux computer to be used as a gateway to the internet for other computers on the network. Another (and probably safer at home) way to do it, is to set up masquerading
    syslogd - manages system activity logging. The configuration file is /etc/syslog.conf
    smtpd - Several daemons that support SMTP are available including sendmail, smtpd, rsmtpd, qmail, zmail, etc. for the exchange of email messages
    usb - daemon for devices on USB(Universal Serial Bus)
    xfs - X font server
    xntpd - finds the server for a NIS domain and stores the information about it in a binding file
    ypbind - NIS binder. Needed if computer is part of Network Information Service domain.

    Back



    THE SHELL LANGUAGE

    The Shell is both a command language and a programming language that provides an interface to the Unix operating system. There are different versions of the shell interface as the Bourne Shell, Korn shell, C Shell, TC Shell and the Z shell, each developed by different companies.

    General operating rules of the shell language

  • The shell reads its input from a file, from the -c option or from the system() and popen() functions in the XSH specification
  • If the first line of a file of shell commands starts with the characters #!, the results are unspecified
  • The construct #! is reserved for implementations wishing to provide that extension. A portable application cannot use #! as the first line of a shell script; it might not be interpreted as a comment
  • The shell breaks the input into tokens: words and operators
  • The shell parses the input into simple commands and compound commands
  • The shell performs various expansions (separately) on different parts of each command, resulting in a list of pathnames and fields to be treated as a command and arguments
  • The shell performs redirection and removes redirection operators and their operands from the parameter list
  • The shell executes a function, built-in, executable file or script, giving the names of the arguments as positional parameters numbered 1 to n, and the name of the command (or in the case of a function within a script, the name of the script) as the positional parameter numbered 0
  • The shell optionally waits for the command to complete and collects the exit status

    Reserved words in the shell language

    	!  elif  fi  in  while  case  else  for  then  {*  do  esac  if  until  }  done
    In some systems, function, select, [[ and ]] are also considered as reserved words.

    Lines starting with # are comments.

    Shell commands

    File system commands

    cd - changes the current working directory
    cp - copy a directory/file to a new location
    dircolors - color setup for ls by outputting commands to set the LS_COLORS environment variable
    	dircolors -p			output defaults
    
    	Default colors
    	bright red - compressed/archive files
    	green	- executables
    	blue	- directory
    	cyan	- symbolic link
    ls - lists all files in a directory
    	ls -a		- includes files beginning with a period in the list
    	ls -R		- produces a recursive listing of all files and subdirectories under the current directory
    	ls -l		- includes additional information (mode, number of links, owner, group, size and last modified time) for each file
    	ls |more	- lists files page by page (use Space key to move to next page)
    mkdir - creates a new directory
    mv - rename a directory/file or move it to a new location
    	mv currentfile newfile
    pwd - show present working directory
    rm - removes a file/directory
    rmdir - removes an empty directory
    scp - copy files across servers
    	scp [-P port] /path/to/file username@destserver:/path/to/destination
    Viewing and editing files

    cat - read the contents of a file and also copy or create new files
    	cat filename | more			- view a textfile, one page at a time
    	cat filename | less or less filename	- scroll through text file one line at a time, press q to exit
    head - print first 10 lines of the text file
    	head filename
    tail - print last 10 lines of the text file
    	tail filename
    pico - edit a text file using the pico editor
    	pico filename
    more - view file, page by page (use space key to move)
    	more filename
    touch - create a new empty file
    	touch filename
    Process control commands

    bg - resumes a suspended process in the background
    	bg 2
    <Control Z> - suspends a foreground process

    fg - resumes a suspended process in the foreground
    	fg 2
    jobs - list background or stopped processes with their job numbers
    	jobs
    kill - used to kill a job or a process
    	kill -l		- list all available signals
    	kill -9 <pid>	- use SIGKILL (cannot be handled by the receiving process and forcefully terminates it)
    lsof - list all opened files
    	lsof -c mysql	- list all files opened by mysql process
    	lsof -p <pid>
    nice - run program name adjusting its priority
    	nice program_name
    pidof - find the process id of a running program
    	pidof mysqld
    pmap - report the memory map of a process
    	pmap <pid>
    	pmap -x <pid>	- show the extended format
    ps - lists the process ids of the processes being run, along with the terminal controlling the process, current process status and time spent
    	ps			displays two processes, the shell and the ps command
    	ps -efl			view all processes
    	ps -efl | grep mysql	show processes of specific user
    	ps -p <pidlist>		show processes given in pidlist
    	ps ax			list all processes (using BSD syntax)
    	ps -u <user>		list processes associated with a user
    	
    	PROCESS STATE CODES
    	 D uninterruptible sleep (usually IO)
    	 R running or runnable (on run queue)
    	 S interruptible sleep (waiting for an event to complete)
    	 T stopped, either by a job control signal or because it is being traced
    	 W paging (not valid since the 2.6.xx kernel)
    	 X dead (should never be seen)
    	 Z defunct ("zombie") process, terminated but not reaped by its parent
    stop - suspends a background process

    Search commands

    find - find a file named filename starting from the root, wildcards (*.?) may be used
    	find / - name  'filename'
    grep - searches the named input files (or standard input if no files are named, or the file name - is given) for lines containing a match to the given pattern. By default, grep prints the matching lines
    	grep [options] PATTERN [FILE...]
    	grep [options] [-e PATTERN | -f FILE] [FILE...]
    In addition, two variant programs egrep (same as grep -E) and fgrep (grep -F) are available.

    locate - find the filename that contains the string filename
    	locate filename
    which - shows the full path of an executable that can be run from the command file
    	which  executablefilename
    whereis - prints the location of the source, binary and manual pages for the command
    	whereis  command
    System information commands

    df - reports the amount of free disk space available on each partition
    	df -h		show disk free space in GB
    	df -m		show disk free space in MB
    free - displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel, gathered by parsing /proc/meminfo
    	free
    	free -m		display in MB
    	free -g		display in GB
    	free -h		show all output fields scaled to shortest three digit unit and display the units
    	free -m -s 5	display system memory usage every 5 seconds (Ctl+c to exit)
    hostnamectl - used to query and change the system hostname and related settings
    	hostnamectl or hostnamectl status	show system hostname and related info
    	hostnamectl hostname 			if no argument is given, prints the hostname. If argument is given, changes the hostname
    iostat - used for monitoring system I/O statistics for devices and partitions
    	iostat -c	display the CPU utilization report
    	iostat -d	display the device utilization report
    	iostat -m	display statistics in MB
    	iostat -x	display extended statistics
    		%iowait - percentage of time the CPUs were idle during which the system had an outstanding disk I/O request
    		tps - I/O requests issued to the device per second
    		await - average time (in milliseconds) for I/O requests issued to the device to be served
    		r_await/w_await - average time (in milliseconds) for read/write requests to be served
    locale - displays information about the current locale
    	locale
    	locale -a		list all available locales
    	locale -k LC_CTYPE	display values of all keywords in a locale category
    top - displays system information and list of processes in real time
    	top		press q to quit
    	top -n 10	quit after 10 refreshes
    	top -u root	display specific user processes
    uname - displays the system information such as hardware platform, system name and processor, OS type
    	uname -a	show Linux version details
    	uname -r	display kernel version
    uptime - gives a one line display of the current time, how long the system has been running, how many users are currently logged on and the system load averages for the past 1, 5, and 15 minutes
    	uptime -s	system up since
    User management commands

    chmod - used to change file access permissions (d rwx rwx rwx, d=directory, rwx=permissions for file owner,group and others)
    	chmod [reference][operator][mode] file
    	chmod [OPTION]... OCTAL-MODE file
    	chmod [OPTION]... --reference=RFILE file
    reference: u - file's owner, g - members of the file's group, o - other, a - all
    operator: + (add), - (remove), = (assign)
    mode: r (read), w (write/delete), x (execute/search directory)
    	chmod o=rw file1.c	- assign read/write access to others
    passwd - change password
    	passwd
    sudo - execute a command as a superuser or another user, as specified by the security policy. The security policy determines what privileges, if any, a user has to run sudo. The policy may require that users authenticate themselves with a password or another authentication mechanism.

    To grant access to a specific user, an existing superuser needs to first add an entry in the /etc/sudoers file, which contains the entire list of users who have sudo access, along with what level of access is granted.
    	sudo <command>
    Miscellaneous commands

    echo - sends its argument to standard output
    	echo Hello		- writes Hello on the screen
    	echo $var		- displays the value of variable var
    	echo $PATH		- show user's path environment variable
    	echo "create database tempdb" | mysql		- creates a db in mysql server
    read - read a line from the standard input and split it into fields

    Read takes input from the user console and causes the shell to accept an input string to be read in until a newline character is encountered. The shell removes all white space (with the obvious exception of newline characters) but does not interpret filename or character substitutions. The shell takes the input string and breaks it up into substrings which are surrounded by white space.
    	$ read Variable1 Variable2 Variable3 ... VariableN
    env - list environment variables
    	env		- list all environment variables
    ssh - start the ssh client program that enables secure connections to remote machines
    	ssh remote.host.com
    	ssh alternateuser@remote.host.com	- use a different username
    sync - writes any data buffered in memory out to disk by exercising the sync(2) system call. This can include modified superblocks, modified inodes and delayed reads and writes. This is implemented by the kernel

    sysctl - configures kernel parameters at runtime. The parameters available are those listed under the virtual directory /proc/sys

    ulimit - provides control over the resources available to the shell and to processes started by it
    	-a	all current limits are reported
    	-n	the maximum number of open file descriptors
    	-t	the maximum amount of cpu time in seconds
    	-u	the maximum number of processes available to a single user
    	-v	the maximum amount of virtual memory available to the shell and to its children
    	-T	the maximum number of threads
    	
    	-H	set the hard limit
    	-S	set the soft limit

    Back


    Using Shell commands

    Entering multiple commands in a single line

    Multiple commands can be entered on a single line by separating them with semi-colons or the ampersand &. Shell processes commands separated by ; in the order they appear. Commands separated by & are executed at the same time.
    	$ cd docs; mkdir old_docs; mv *.* old_docs	- executed in order
    	$ cp big_file new_dir& rm *.o& who		- executed concurrently
    Aborting a shell command

    Most UNIX systems allow users to abort the current command by typing Ctrl-C

    Running a command in the background

    Any command can be run in the background by using an & at the end of the command.
    	command &
    The job number is printed on the screen so the command can be brought to the foreground, if desired.

    Back



    Language Constructs

    Variables

    Variables can be classified into two main categories - environment variables and shell variables.
  • Environment variables are variables that are available system-wide and are inherited by all spawned child processes and shells
  • Shell variables are variables that apply only to the current shell instance

    The shell provides string-valued variables. Variable names begin with a letter and consist of letters, digits and underscores. Variables may be given values by writing
    	user=fred box=m000 acct=mh0000
    The value of a variable is substituted by preceding its name with $
    	echo $user		- echos fred
    Creating environment variables

    An environment variable can be created by using the SET command.
    	set steps=39
    	echo $steps			- see value

    Control statements in the shell

    The shell script language, like other programming languages, has constructs for conditional execution (if-then-else, while), iterative execution (for loop), a switch statement and a goto statement.

    foreach

    Syntax
         foreach var ( worddlist )
             commandlist
         end
    The commandlist is executed once for each word in the wordlist, and each time the variable var will contain the value of that word. For e.g, the following script can search all immediate subdirectories of the current directory for a given file (and then quit if it finds one)
    	#! /bin/csh -f
    		 set f = $1
    		 foreach d (*)
    			if (-e $d/$f) then
    				echo FOUND: $d/$f
    				exit(0)
    			endif
    		 end
    		 echo $f not found in subdirectories

    goto

    The goto command provides a way to branch unconditionally to a line identified by a label.
    	goto label1
    where label1 is a label on a line (by itself) somewhere in the script in the form
    	label1:

    if-then-else

    Syntax
    	if ( expr ) simple-command
    
    	OR 
    
    	if ( expr ) then
    		commandlist-1
    	[else
    		commandlist-2]
    	endif
    Example
    	if ($#argv <> 2) then
    		echo "you must give exactly two parameters"
    	else
    		set name1 = $argv[1]
    		set name2 = $argv[2]
    	endif
    switch

    The switch command provides a multiple branch similar to the switch statement in C. The general form of switch is
    	switch (str)
    		case string1:
    			commandlist1
    			breaksw
    		case string2:
    			commandlist2
    			breaksw
    		default
    			commandlist
    	endsw
    Control flow is switched to where the first match occurs. As in file name expansion, a case label may be a literal string, or contain variable substitution, or contain wild-card character such as *,? etc.

    while

    The syntax of while loop construct is
    	while (expr)
    		commandlist
    	end
    The commandlist will be executed until the expr evaluates to false.


    Special built-in utilities

    The following special built-in utilities are supported in the shell language. The output of each command, if any, will be written to standard output, subject to the normal redirection and piping possible with all commands.

    The term built-in implies that the shell can execute the utility directly and does not need to search for it. An implementation can choose to make any utility a built-in.

    The special built-in utilities differ from regular built-in utilities in two respects
  • A syntax error in a special built-in utility may cause a shell executing that utility to abort, while a syntax error in a regular built-in utility will not cause an abort. If a special built-in utility encountering a syntax error does not abort the shell, its exit value will be non-zero
  • Variable assignments specified with special built-in utilities will remain in effect after the built-in completes but not with a regular built-in or other utility

    break - exit from for, while or until loop
    	break [n]
    Exits from the smallest enclosing for, while or until loop, if any, or from the nth enclosing loop if n is specified. The value of n is an unsigned integer >= 1 and the default is equivalent to n=1. If n is greater than the number of enclosing loops, the last enclosing loop is exited from. Execution will continue with the command immediately following the loop.
    	for i in *
    	do
    		if test -d "$i"
    		then break
    		fi
    	done

    colon - null utility
    	: [argument ...]
    This utility will only expand command arguments. It is used when a command is needed, as in the then condition of an if command, but nothing is to be done by the command.
    	: ${X=abc}
    	if     false
    	then   :
    	else   echo $X
    	fi
    	abc
    As with any of the special built-ins, the null utility can also have variable assignments and redirections associated with it, such as
    	x=y : > z
    which sets variable x to the value y (so that it persists after the null utility completes) and creates or truncates file z.

    continue - continue for, while or until loop
    	continue [n]
    The continue utility will return to the top of the smallest enclosing for, while or until loop, or to the top of the nth enclosing loop, if n is specified. This involves repeating the condition list of a while or until loop or performing the next assignment of a for loop, and reexecuting the loop if appropriate. n is an integer >= 1 and the default is n=1. If n is greater than the number of enclosing loops, the last enclosing loop is used.
    	for i in *
    	do
    	    if test -d "$i"
    	    then continue
    	    fi
    	done

    dot - execute commands in current environment
    	. file
    The shell will execute commands from the file in the current environment. If file does not contain a slash, the shell will use the search path specified by to find the directory containing file. Unlike normal command search, however, the file searched for by the dot utility need not be executable. If no readable file is found, a non-interactive shell will abort; an interactive shell will write a diagnostic message to standard error, but this condition will not be considered a syntax error.
    	cat foobar
    	foo=hello bar=world
    	. foobar
    	echo $foo $bar
    	hello world
    eval - construct command by concatenating arguments
    	eval [argument ...]
    The eval utility will construct a command by concatenating arguments together, separating each with a space character. The constructed command will be read and executed by the shell.
    	foo=10 x=foo
    	y='$'$x
    	echo $y
    	$foo
    	eval y='$'$x
    	echo $y
    	10
    exec - execute commands and open, close or copy file descriptors
    	exec [command [argument ...]]
    The exec utility will open, close or copy file descriptors as specified by any redirections as part of the command. If exec is specified without command or arguments, and any file descriptors with numbers > 2 are opened with associated redirection statements, it is unspecified whether those file descriptors remain open when the shell invokes another utility. Scripts concerned that child shells could misuse open file descriptors can always close them explicitly. If exec is specified with command, it will replace the shell with command without creating a new process. If arguments are specified, they are arguments to command. Redirection will affect the current shell execution environment.
    	exec 3< readfile	-- Open readfile as file descriptor 3 for reading 
    	exec 4> writefile	-- Open writefile as file descriptor 4 for writing
    	exec 5<&0		-- Make unit 5 a copy of unit 0 
    	exec 3<&-		-- Close file unit 3 
    	exec cat file01		-- Cat the file file01 by replacing the current shell with the cat utility
    exit - cause the shell to exit
    	exit [n]
    The exit utility causes the shell to exit with the exit status specified by the unsigned integer n. If n is specified, but its value is not between 0 and 255 inclusively, the exit status is undefined.

    Certain exit status values have been reserved for special uses and should be used by applications only for those purposes
    	126 - a command to be executed was found, but the file was not an executable utility
    	127 - a command to be executed was not found
    	> 128 - a command was interrupted by a signal
    A trap on EXIT will be executed before the shell terminates, except when the exit utility is invoked in that trap itself, in which case the shell will exit immediately.
    	exit 0	-- Exit with a true value
    	exit 1	-- Exit with a false value
    export - set export attribute for variables
    	export name[=word]... 
    	export -p
    The shell will give the export attribute to the variables corresponding to the specified names, which will cause them to be in the environment of subsequently executed commands.

    When -p is specified, export will write to the standard output the names and values of all exported variables, in the following format
    	"export %s=%s\n", name,value
    The -p option allows portable access to the values that can be saved and then later restored using, for instance, a dot script. The shell will format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as commands that achieve the same exporting results. When no arguments are given, the results are unspecified.
    	export PWD HOME      --> Export variables 
    	export PATH=/local/bin:$PATH    --> Set and export the variable
    	export -p > temp-file        ---> Save and restore all exported variables 
    	unset a lot of variables
    	... processing
    	. temp-file
    readonly - set read-only attribute for variables
    	readonly name[=word]...
    	readonly -p
    The variables whose names are specified will be given the readonly attribute. The values of these variables cannot be changed by subsequent assignment, nor can those variables be unset by the unset utility. Some versions of the shell exist that preserve the read-only attribute across separate invocations.

    The readonly special built-in supports the XBD specification, Utility Syntax Guidelines.

    When -p is specified, readonly will write to the standard output the names and values of all read-only variables, in the following format
    	"readonly %s=%s\n", name,value
    The shell will format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as commands that achieve the same attribute-setting results. The -p option allows portable access to the values that can be saved and then later restored using, for instance, a dot script.
    	readonly HOME PWD
    return - return from a function
    	return [n]
    The return utility will cause the shell to stop executing the current function or dot script. If the shell is not currently executing a function or dot script, the results are unspecified. The results of returning a number greater than 255 are undefined because of differing practices in the various implementations. Some shells AND out all but the low-order 8 bits; others allow larger values, but not of unlimited size.

    set - set or unset options and positional parameters
    	set [-abCefmnuvx][-h][-o option][argument...]
    	set [+abCefmnuvx][+h][-o option][argument...]
    	set --[argument...]
    	set -[argument...]
    If no options or arguments are specified, set will write the names and values of all shell variables in the collation sequence of the current locale. Each name will start on a separate line, using the format
    	"%s=%s\n", name, value
    The value string will be written with appropriate quoting so that it is suitable for reinput to the shell, setting or resetting, as far as possible, the variables that are currently set. Read-only variables cannot be reset.

    When options are specified, they will set or unset attributes of the shell. When arguments are specified, they will cause positional parameters to be set or unset. Setting or unsetting attributes and positional parameters are not necessarily related actions, but they can be combined in a single invocation of set.

    The set special built-in supports the XBD specification, Utility Syntax Guidelines except that options can be specified with either a leading hyphen (meaning enable the option) or plus sign (disable it).

    Options -a - From now on, automatically mark variables for export after defining or changing them
    -b - Cause the shell to notify the user asynchronously of background job completions. The following message will be written to standard error
    	"[%d]%c %s%s\n", job-number, current, status, job-name
    -e - Exit if a command yields a nonzero exit status
    -v - show each shell command line when read
    -x - show commands and arguments when executed. This provides step-by-step debugging of shell scripts
    	set		- Write out all variables and their values
    	set c a b	- Set $1, $2 and $3 and set $# to 3
    	set -xv		- Turn on the -x and -v options
    	set - -		- Unset all positional parameters
    	set - - "$x"	- Set $1 to the value of x, even if x begins with - or +
    	set - - $x	- Set the positional parameters to the expansion of x, even if x expands with a leading - or +

    shift - shift positional parameters
    	shift [n]
    The positional parameters will be shifted. Positional parameter 1 will be assigned the value of parameter (1+n), parameter 2 will be assigned the value of parameter (2+n), and so on. The parameters represented by the numbers $# down to $#-n+1 will be unset, and the parameter "#" will be updated to reflect the new number of positional parameters. The value n will be an unsigned integer less than or equal to the value of the special parameter "#". If n is not given, it will be assumed to be 1. If n is 0, the positional and special parameters will not be changed.
    	$ set a b c d e
    	$ shift 2
    	$ echo $*
    	c d e
    times - write process times
    	times
    Write the accumulated user and system times for the shell and for all of its child processes, in the following POSIX locale format
    	"%dm%fs %dm%fs\n%dm%fs %dm%fs\n", shell user minutes, shell user seconds, shell system minutes, seconds, 
    			children user minutes, seconds, children system minutes, seconds
    The four pairs of times correspond to the members of the XSH specification tms structure as returned by times() - tms_utime, tms_stime, tms_cutime and tms_cstime respectively.
    	$ times
    	0m0.43s 0m1.11s
    	8m44.18s 1m43.23s
    trap - trap signals
    	trap [action  condition ...]
    If action is '-', the shell will reset each condition to the default value. If action is null ("), the shell will ignore each specified condition if it arises. Otherwise, the argument action will be read and executed by the shell when one of the corresponding conditions arises. The action of the trap will override a previous action (either default action or one explicitly set). The value of '$?' after the trap action completes will be the value it had before the trap was invoked.

    The condition can be EXIT, 0 (equivalent to EXIT) or a signal specified using a symbolic name, without the SIG prefix, as listed in the tables of signal names in the XSH specification under <signal.h>; e.g HUP,INT,QUIT,TERM. Implementations may permit lower-case signal names or names with the SIG prefix as an extension. Setting a trap for SIGKILL or SIGSTOP produces undefined results. XSI-conformant systems also allow numeric signal numbers for the conditions, corresponding to the following signal names
    	Signal#		Name  
    	1  		SIGHUP  
    	2  		SIGINT  
    	3  		SIGQUIT  
    	6  		SIGABRT  
    	9  		SIGKILL  
    	14  		SIGALRM  
    	15  		SIGTERM
    The environment in which the shell executes a trap on EXIT will be identical to the environment immediately after the last command executed before the trap on EXIT was taken.

    The trap command with no arguments will write to standard output a list of commands associated with each condition in the format
    	"trap - - %s %s ...\n", <action>, <condition>
    The shell will format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as commands that achieve the same trapping results. For e.g
    	save_traps=$(trap)
    	...
    	eval "$save_traps"
    Each time the trap is invoked, the action argument will be processed in a manner equivalent to
    	eval '$action'
    Signals that were ignored on entry to a non-interactive shell cannot be trapped or reset, although no error need be reported when attempting to do so. An interactive shell may reset or catch signals ignored on entry. Traps will remain in place for a given shell until explicitly changed with another trap command. When a subshell is entered, traps that are not being ignored are set to the default actions.
    	trap				- write out a list of all traps and actions
    	trap '$HOME/logout' EXIT
    		or
    	trap '$HOME/logout' 0		- set a trap so the logout utility in the directory will execute when the shell terminates
    	trap - INT QUIT TERM EXIT	- unset traps on INT, QUIT, TERM and EXIT
    unset - Unset values and attributes of variables and functions
    	unset [-fv] name ...
    Each variable or function specified by name will be unset.
    If -v is specified, name refers to a variable name and the shell will unset it and remove it from the environment. Read-only variables cannot be unset.
    If -f is specified, name refers to a function and the shell will unset the function definition.
    If neither -f nor -v is specified, name refers to a variable; if a variable by that name does not exist, it is unspecified whether a function by that name, if any, will be unset.
    Unsetting a variable or function that was not previously set is not considered an error and will not cause the shell to abort.
    	unset -v VISUAL		- unset variable
    	unset -f foo bar	- unset the functions foo and bar

    Back



    Shell scripts

    A shell script is a file containing a list of commands to be executed by the Unix shell. Shell scripts provide users the ability to create their own customized Unix commands.

    Shell scripts can be created by using any text editor like the vi editor.

    The below sample shell script will delete files, prompting for the user's confirmation for each file to be deleted, including directory files.
    	#! /bin/csh -f
    
    	foreach name ($argv)
    	  if ( -f $name ) then
    	     echo -n "delete the file '${name}' (y/n/q)?"
    	  else
    		 echo -n "delete the entire directory '${name}' (y/n/q)? "
    	  endif
    	  set ans = $<
    	  switch ($ans)
                case n: 
                   continue
                case q: 
                   exit
                case y: 
                   rm -r $name
                   continue
    	  endsw
    	end
    Invoking Shell scripts

    There are two ways to invoke a shell script file.

    a. Direct Interpretation
    In direct interpretation, any of the shell programs (sh, csh, bash etc.) can be invoked to interpret the script contained in the file
    	sh filename [arg ...]

    b. Indirect Interpretation
    In indirect interpretation, the following must be inserted as the first line of the file
    	#! /bin/csh
    
    	or
    
    	#! /bin/csh -f
    The file must be made executable using chmod ($ chmod u+x filename). Then it can be invoked in the same way as any other command, i.e., by typing the script file name on the command line.

    Back



    TEXT EDITORS

    The vi editor

    The vi editor (short for visual editor) is available by default on all unix systems. vim (vi improved) is an enhanced version of vi.
    	vi		- start the vi editor
    	vi file1	- open an existing file or create a new one
    vi has three modes of operation - command, insert and command line(escape). In the command mode, letters or sequence of letters interactively command vi and in the insert mode text is inserted. The escape mode is invoked by typing ':' while vi is in command mode and the cursor jumps to the last line and vi will wait for a command.

    Commands for switching modes
    	a	append, enters insert mode and moves one position to the right
    	i	enter insert mode
    	o	enter insert mode, insert a blank line under the current cursor position and move the cursor to that line
    	Esc	switch back to command mode
    	:	enter command line mode
    Moving through the text is possible with the arrow keys or
    	h	move the cursor to the left
    	l	move it to the right
    	k	move up
    	j	move down
    	SHIFT-G	puts the prompt at the end of the document
    Editing commands
    	dd	delete line
    	n dd	delete n lines starting from the current cursor position
    	dw	delete word
    	n dw	delete n words to the right side of the cursor
    	x	delete the character on which the cursor is positioned
    	yy	copy current line into storage buffer
    	p	paste after current line
    	n p	paste it n times
    
    	:n	moves to line n of the file
    	:w	save (write) the file
    	:q	exit the editor
    	:q!	forces the exit when quitting a file containing unsaved changes
    	:wq	save and exit
    	:w newfile	save the text to newfile
    	:wq!	overrides read-only permission (if user has the permission to override permissions, for instance when using the root account)
    	:recover	recover a file after an unexpected interruption
    	:1, $s/word/anotherword/g	replace word with anotherword throughout the file
    Search commands
    	/astring	search the string in the file and position the cursor on the first match below its position
    	/		perform the same search again, moving the cursor to the next match

    Back



    REMOTE CONNECTIVITY

    TCP and UDP port numbers

    List of TCP and UDP port numbers used by protocols of the Internet protocol suite for operation of network applications, maintained by the Internet Assigned Numbers Authority (IANA).
    	22	SSH
    	25	SMTP
    	80	HTTP
    	1433	SQL Server DBMS
    	3306	MySQL DBMS
    	3389	Microsoft RDP
    	5432	PostgreSQL DBMS
    	8080	Alternative port for HTTP
    Telnet

    Telnet is a client-server protocol that can be used to open a command line on a remote computer, typically a server. Telnet works with what is called a virtual terminal connection emulator or an abstract instance of a connection to a computer, using standard protocols to act like a physical terminal connected to a machine.

    Users can utilize telnet to ping a port and find out whether it is open. An open port will show a blank screen, while an error message that says the port is connecting means that it is closed. Telnet is not a secure protocol and is unencrypted.
    	telnet 203.117.234.45 3389	- connect to a host and port
    	Ctl + ]	- to go to telnet prompt
    	quit	- exit telnet
    PuTTY

    PuTTY is a free and open-source terminal emulator, serial console and network file transfer application. It supports several network protocols including SCP, SSH, Telnet, rlogin and raw socket connection. It can also connect to a serial port. PuTTY was developed and is maintained by Simon Tatham, a British programmer.

    RDP

    Remote Desktop Protocol (RDP) is a proprietary protocol developed by Microsoft which provides a user with a graphical interface to connect to another computer over a network connection. The user employs RDP client software for this purpose, while the other computer must run RDP server software.

    Clients exist for most versions of Microsoft Windows, Linux, Unix, macOS, iOS, Android and other operating systems. RDP servers are built into Windows operating systems and an RDP server for Unix and OS X also exists. By default, the server listens on TCP port 3389 and UDP port 3389.


    TROUBLESHOOTING/RECOVERY

    Messages

    Buffer I/O error on device <device>, logical block xxxxxxx - HDD needs to be checked
    INFO: task <process>:<pid> blocked for more than 120 seconds - Task is hung due to being starved of I/O or other resources

    Error codes

    The perror utility can be used to explain error codes
    	perror [options] errcode
    Error#	Error code
    110	ETIMEDOUT	Connection timed out
    111	ECONNREFUSED	Connection refused
    112	EHOSTDOWN	Host is down
    113	EHOSTUNREACH	No route to host

    Back



    TOOLS/UTILITIES

    htop

    htop is a command line utility that allows the user to interactively monitor the server's processes and system resources in real time. It is an enhanced version of the top command.

    CPU usage:
    The numbers on the top represent the number of cpu cores in the system with the progress bar next to them representing the load.
    	Blue - low priority processes (nice > 0)
    	Green - normal (user) processes
    	Red - kernel processes
    	Yellow - IRQ time
    	Magenta - soft IRQ time
    	Grey - IO Wait time
    Memory usage:
    	Green - used memory pages
    	Blue - buffer pages
    	Yellow - cache pages
    Processes: Tasks - processes opened
    Thr - threads opened
    Running - threads running
    Load average:
    The load average represents the average system load over a period of time - 1.0 on a single core cpu represents 100% utilization. The first number is a 1 minute load average, second is 5 minutes load average and the third is 15 minutes load average.

    The list of processes displays data about each process. The Home/End/PageUp/PageDown and arrow keys can be used to scroll through the process list pid - process ID
    user - owner of the process
    PRI - the process's priority. The lower the number, the higher the priority
    NI - the nice value of the process, which affects its priority
    state (S) - state of the process
    	S for sleeping (idle)
    	R for running
    	D for disk sleep (uninterruptible)
    	Z for zombie (waiting for parent to read its exit status)
    	T for traced or suspended (e.g by SIGTSTP)
    	W for paging
    VIRT - virtual memory used by the process
    RES - physical RAM used by the process
    SHR - shared memory used
    %CPU - percentage of the processor time used by the process
    %MEM - percentage of physical RAM used
    TIME+ - processor time used
    command - full command line of the process (program name and arguments)

    Back



    C/C++ PROGRAMMING

    THE C++ LANGUAGE

    Datatypes in C++

    	char or signed char	Character or integer 8-bits long Range -128 to 127	1 byte
    	unsigned char		Range 0 to 255						1 byte						
    	int			unsigned or signed					length depends on OS, 2 bytes or 4 bytes
    	long int or long								4 bytes long
    	short int or short								2 bytes long
    	float			floating point number					4 bytes
    	double			double precision floating point number			8 bytes
    	long double		long double precision floating point number		10 bytes
    	bool			boolean value 	true or false				1 byte
    	wchar_t			2-bytes-wide character designed to store international characters

    Variables in C++

    Variables of the same type can be declared on the same line.
    	int a,b,c;
    Variables can be initialized in two ways
    	float a = 2.5;         int a = 0;
    	float a (2.5);          int a (0);
    Integer data types (char, short, long and int) can be signed or unsigned. By default, they are signed. signed and unsigned may also be used as a simple types, meaning the same as signed int and unsigned int respectively.
    	signed x;   //signed int x;
    	unsigned y;  //unsigned int y;
    Declaring and using constants

    Literal constants can be integers, floating point numbers, characters or strings
    	707	// unsigned integer constant
    	-273	// signed integer
    	75	// decimal
    	0113	// octal(preceded by 0)
    	0x4b	// hexadecimal(preceded by 0x)
    	3.14159	// floating point
    	6.02e23	// exponential constant
    	1.6e-19	// negative exponential constant
    	'z'		// character
    	"Hello world"	// string
    Constants can be declared by using a const prefix in front
    	const int width = 100;
    	const char tab = '\t';
    	const zip = 12440;
    If type is not specified int is assumed.

    Escape codes used in C++

    	\n newline
    	\r carriage return
    	\t tabulation
    	\v vertical tabulation
    	\b backspace
    	\f page feed
    	\a alert (beep)
    	\' single quotes (')
    	\" double quotes (")
    	\? question (?)
    	\\ inverted slash (\)

    Operators

    Assignment =
    Arithmetic + , - , * , / , %(module)
    Compound assignation +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=
    Increment and decrement ++, --
    Relational ==, !=, >, <, >=, <=
    Logical ! (NOT), && (AND), || (OR)
    Conditional operator ? Usage : condition ? result1 : result2 result1 is returned if condition is true, if false result2 is returned


    Bitwise operators

    & - Bitwise AND
    | - Bitwise OR
    ^ - Bitwise Exclusive OR
    ~ - Bitwise Compliement
    << - Bitwise Shift Left
    >> - Bitwise Shift Right

    The SIZEOF operator

    The sizeof operator is used to return the size (number of bytes) of a datatype or expression. It returns a value of type size_t which is an unsigned int.
    	int i=12;
    	size_t var = sizeof (i);
    	size_t var = sizeof (int);

    Arrays

    Declaring arrays
    	type name [elements];
    	int sockets [5];	//declares an integer array of five elements

    Initialization of arrays

    When declaring an array of local scope (within a function), unless specified otherwise, it will not be initialized, so its content is undetermined until we store some values in it. If a global array is declared (outside any function) its content will be initialized with all its elements filled with zeros. Additionally, an array can be initialized as below:
    	int ports [5] = { 16, 2, 77, 40, 12071 };
    	char fname [5] = {'a','c','m','o','p'};


    Statements

    if-else

    An if-else statement controls conditional branching.
    	bool is_true() { return true; }
    	int x = 10;
    
    	int main()
    	{
    	if (is_true())
    	{	cout << "b is true!\n";		}
    	else
    	{	cout << "b is false!\n";	}
    
    	if (x == 10)
    	{	x = 0;	}
    	}
    for statement

    The for statement format is
    	for (initialization; condition; increase) statement;
    The for statement works as follows
    1. initialization is executed only once. Generally it is a initial value setting for a counter variable
    2. condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is skipped.
    3. statement is executed. It can be either a single instruction or a block of instructions enclosed within curly brackets { }
    4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
    	// countdown using a for loop
    	#include <iostream.h>
    	int main ()
    	{
    	  for (int n=10; n>0; n--) {
    		cout << n << ", ";    // outputs 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
    	  }
    	  cout << "FIRE!";
    	  return 0;
    	}

    switch statement
    	switch (expression) {
    	  case constant1:
    		block of instructions 1
    		break;
    	  case constant2:
    		block of instructions 2
    		break;
    	  .
    	  .
    	  .
    	  default:
    		default block of instructions
    	  }

    while and do-while
    	while (condition) statement
    	do statement while (condition);
    The functionality of both loops is exactly the same except that condition in the do-while is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled.


    Functions

    Overloaded functions

    Overloaded functions are two different functions that have the same name with different argument prototypes. i.e different number of arguments or different types in arguments.
    	int divide (int a, int b)
    	{
    	  return (a/b);
    	}
    
    	float divide (float a, float b)
    	{
    	  return (a/b);
    	}

    Pointers

    Operators used in conjunction with pointers

    The reference operator (*) is used to refer to the value stored in the memory address contained by a pointer variable. The * operator is also used to declare a pointer.
    	char  *ptr1;
    	var1 = *ptr1;
    The address dereference operator (&) is used to assign a memory address to a pointer variable.
    	ptr1 = &var1;	-- assign to pointer variable ptr1 the address of variable var1

    Void pointers

    The void pointer is a special type of pointer that can point to any data type, from an integer value or a float to a string of characters. Its sole limitation is that the pointed data cannot be referenced directly (reference * operator cannot be used on them), since its length is always undetermined, and type casting or assignations have to be resorted to to turn a void pointer to a pointer of a concrete datatype that can be referred to. One of its utilities may be for passing generic parameters to a function
    	// integer increaser
    	void increase (void* data, int type)
    	{
    	  switch (type)
    	  {
    		case sizeof(char) : (*((char*)data))++; break;
    		case sizeof(short): (*((short*)data))++; break;
    		case sizeof(long) : (*((long*)data))++; break;
    	  }
    	}
    
    	int main ()
    	{
    		char a = 5;
    		short b = 9;
    		long c = 12;
    		increase (&a,sizeof(a));
    		increase (&b,sizeof(b));
    		increase (&c,sizeof(c));
    		cout << (int) a << ", " << b << ", " << c;
    		return 0;
    	}		--> outputs 6, 10, 13

    Preprocessor directives

    #define - serves to generate defined constants or macros
    #undef - the inverse functionality of #define
    #ifdef, #ifndef, #if, #endif, #else and #elif - These directives allow to discard part of the code of a program if a certain condition is not fulfilled
    #line - Assigns line numbers to code
    #error - This directive aborts the compilation process when it is found returning the error that is specified as parameter
    	#ifndef __cplusplus
    	#error A C++ compiler is required
    	#endif
    	/*abort the compilation process if the defined constant __cplusplus is not defined*/
    #include - When the preprocessor finds an #include directive it replaces it by the whole content of the specified file. There are two ways to specify a file to be included
    	#include "file"
    	#include <file>
    #pragma - used to specify diverse options to the compiler. These options are specific for the platform and the compiler used


    Classes

    Permissions allotted to members of classes

    private members of a class are accessible only from other members of its same class or from its "friend" classes
    protected members are accessible, in addition to from members of the same class and friend classes, also from members of its derived classes
    public members are accessible from anywhere where the class is visible


    Structures

    A data structure is a set of diverse types of data that may have different lengths grouped together under a unique declaration.
    	struct products {
    	  char name [30];
    	  float price;
    	} apple;
    
    	products orange, melon;

    Using pointers in conjunction with structures

    Like any other type, structures can be pointed by pointers. The rules are the same as for any fundamental data type - the pointer must be declared as a pointer to the structure
    	struct movies_t {
    	  char title [50];
    	  int year;
    	};
    
    	movies_t amovie;
    	movies_t * pmovie;
    Here amovie is an object of struct type movies_t and pmovie is a pointer to point to objects of struct type movies_t.
    	#include <iostream.h>
    	#include <stdlib.h>
    
    	int main ()
    	{
    	char buffer[50];
    
    	movies_t amovie;
    	movies_t * pmovie;
    	pmovie = & amovie;
    
    	cout << "Enter title: ";
    	cin.getline (pmovie->title,50);
    	cout << "Enter year: ";
    	cin.getline (buffer,50);
    	pmovie->year = atoi (buffer);
    
    	cout << "\nYou have entered:\n";
    	cout << pmovie->title;
    	cout << " (" << pmovie->year << ")\n";
    
    	return 0;
    	}

    Back



    C++ PROGRAMMING

    Exception handling

    The try-catch block
    	#include <iostream.h>
    
    	int main () {
    		char myarray[10];
    		try
    		{
    		for (int n=0; n<=10; n++)
    		{
    			if (n>9) throw "Out of range";
    			myarray[n]='z';
    		}
    		}
    		catch (char * str)
    		{
    		cout << "Exception: " << str << endl;
    		}
    		return 0;
    	}

    Standard exceptions thrown by C++ functions

    Some functions of the standard C++ language library send exceptions that can be captured if included within a try block. These exceptions are sent with a class derived from std::exception as type. This class (std::exception) is defined in the C++ standard header file <exception> and serves as pattern for the standard hierarchy of exceptions
    	exception
    		bad_alloc (thrown by new)
    		bad_cast (thrown by dynamic_cast when fails with a referenced type)
    		bad_exception (thrown when an exception doesn't match any catch)
    		bad_typeid (thrown by typeid)
    		logic_error
    			domain_error
    			invalid_argument
    			length_error
    			out_of_range
    		runtime_error
    			overflow_error
    			range_error
    			underflow_error
    		ios_base::failure (thrown by ios::clear)

    Back



    File handling

    C++ classes that support file input/output

    ofstream - file class for writing operations (derived from ostream)
    ifstream - file class for reading operations (derived from istream)
    fstream - for both reading and writing operations (derived from iostream)

    File open modes used in C++ file streams

    ios::app Append output to the end of the file
    ios::ate Open the file and move to the end. Data may be written anywhere in the file
    ios::in Open the file for input
    ios::out Open the file for output. If the file exists, it will be overwritten
    ios::trunc Truncates the files contents if it exists
    ios::nocreate Only open the file if it exists
    ios::noreplace Only open the file if it doesn't exist
    Note: The ! operator is overloaded to test for errors opening the file.


    Write/read data from a sequential/text file

    The ofstream constructor may be used to create a sequential file. The constructor can take two arguments, the filename and the open mode.
    	#include <iostream.h> 
    	#include <fstream.h>
    	int main()
    	{
    		char name[80];
    		ofstream outFile("junk.txt", ios::out);
    		// Check if there was an error opening the file
    		if (!outFile)
    		{
    			cout << "Unable to open the file\n";
    			return 1;
    		}
    		cout  << "Enter your name: ";
    		cin >> name;
    		outFile  << "This file was created by "  << name  << endl;
    		outFile.close();
    		return 0;
    	}
    Reading a sequential file is a simple case of opening the file and using the extraction operator to read the contents. The following example opens the file for reading, reads each word from the file, and writes each word read on a new line.
    	#include <iostream.h> 
    	#include <fstream.h>
    	int main()
    { char buffer[80];
    ifstream inFile("junk.txt", ios::in); // Check if there was an error opening the file if (!inFile) { cout << "Unable to open the file\n"; return 1; } while (inFile >> buffer) cout << buffer << endl; inFile.close(); return 0; }

    Process Control

    The fork() function

    The fork() function (present in the unistd.h library) is used to create a new process (child) from an existing process (parent). The return value from fork() can distinguish the parent and child. The parent gets the child's pid returned to it, but the child gets 0 returned to it.
    	pid_t pid;
    
    	switch (pid = fork())
    	{
    	case -1:	/*the fork failed, some possible reasons are lack of process slots or virtual memory */
    		perror("The fork failed!");
    		break;
    
    	case 0:		/* pid of zero is the child */ 
    		/* child process functions go here */
    		/* ... */
    		_exit(0);
    
    	default:	/* pid greater than zero is parent getting the child's pid */
    		printf("Child's pid is %d\n",pid);
    	}

    Back



    JAVA PROGRAMMING

    Java is a software platform released by Sun Microsystems. The Java platform has two components - the JVM and the Java API. Java can be run over various hardware platforms. The Java language is fully object-oriented.

    Java Virtual Machine (JVM)

    The JVM is responsible for interpreting Java bytecode, and translating this into actions or OS calls. Java bytecodes are instructions for the JVM. The java compiler javac converts a java source code file into a bytecode file. Any OS or browser which has the JVM installed can understand the bytecodes.

    The JVM is a part of a large system, the Java Runtime Environment (JRE). Each OS and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM.

    Compiling Java programs

    Java source code is enclosed in a file with the same name as the class and a .java extension i.e classname.java. The source code can be compiled by the javac command-line compiler:
    	javac -classpath "." filename.java
    Compilation produces the output bytecode file with a .class extension. To run the class, use
    	java -classpath "." classname
    Disassembling java classes - The "javap" program included in the JDK disassembles .class files into reasonably legible, assembly like code.
    	javap -c HelloWorld
    Classpath

    Classpath is an environment variable. The Java compiler and the JVM use the classpath or class search path to locate classes when they are referenced by other Java code.


    Java Archive files (JAR)

    JAR files are a format that enables multiple files to be bundled into a single archive file. Typically a JAR file will contain the class files and auxiliary resources associated with applets and applications. The JAR file format provides many benefits like security, decreased download time, compression, packaging, versioning and portability.

    Commands used to handle JAR files -
    	jar cf jar-file input-file(s)	- creates a jar file
    	jar tf jar-file		- view contents of a jar file
    	jar xf jar-file		- extract contents of a jar file
    	jar xf jar-file archived-file(s)	- extracts specific files from a jar file
    	java -jar app.jar		- runs an application packaged as a jar file
    	<applet code=AppletClassName.class
    			archive="JarFileName.jar"
    			width=width height=height>
    	</applet>			- invokes an applet packaged as a jar file
    Comparison of Java and C++

  • Java does not support multiple inheritance.
  • Superclasses of a class are indicated with the extends keyword rather than with a :.
  • Methods must be defined inside the class to which they belong. They may not be declared inside the class and defined outside the class as is common in C++
  • Features removed that make Java easier to read and understand than C++ include #define, typedef, operator overloading, enum, unions and structs
  • Pointer arithmetic is removed in Java to make it safer and more robust than C++
  • Other features removed include global variables, standalone functions (everything is a method), friend functions (Everything in a package is a friend of everything else in the package.) and non-virtual functions
  • A number of features have been added to Java to make it safer including true arrays with bounds checking, garbage collection, concurrency, interfaces (from Objective C) and packages. There is no need to explicitly allocate or free memory in Java

    Java language keywords

    (datatypes) - boolean, byte, char, double, float, int, long, short
    (modifiers) - abstract, const, final, native, package, private, protected, public, static, synchronized, transient, void, volatile
    (statements) - break, case, catch, continue, do, else, finally, for, goto, if, return, switch, throw, throws, try, while
    (others) - class, default, extends, implements, import, instanceof, interface, new, strictfp, super, this


    DATATYPES

    Primitive datatypes in Java

    boolean
    byte
    short
    int
    long
    float
    double
    char
    Strings
    Arrays


    Casting and Conversion

  • Conversions can be of three types - assignment conversion, method call conversion and arithmetic promotion
  • Widening conversions are acceptable but narrowing conversions are rejected. The below figure shows allowable primitive conversions
    			char
    			 |
    			 v	
    	byte -> short -> int -> long -> float -> double
    	
  • Casting is used to convert wider datatypes to narrower datatypes. Also optionally used in the other way around to improve code clarity. All datatypes other than boolean can be cast between each other
  • boolean types cannot be cast from or to

    Converting Strings to numbers

    Strings can be converted into numbers using the Integer, Float, Double and Long type wrapper classes.
    	class StringConvert {
    
    	  public static void main (String args[]) {
    
    		String str = "25";
    
    		int i = Integer.valueOf(str).intValue();
    		System.out.println(i);
    		long l = Long.valueOf(str).longValue();
    		System.out.println(l);
    
    		str = "25.6";
    
    		float f = Float.valueOf(str).floatValue();
    		System.out.println(f);
    		double d = Double.valueOf(str).doubleValue();
    		System.out.println(d);
    	   }
    	 }

    OPERATORS

    Arithmetic : +, -, *, /, %

    Unary:
  • Increment and Decrement operators ++, -- In post-fix notation value of the variable/expression is modified after the value is taken for the execution of statement. In prefix notation, value of the variable/expression is modified before the value is taken for the execution of statement.
    	x = 5; y = 0; y = x++;  //Result will be x = 6, y = 5
    	x = 5; y = 0; y = ++x;  //Result will be x = 6, y = 6
    	
    Implicit narrowing conversion is done, when applied to byte, short or char.
  • Unary minus and unary plus +, -
    + has no effect than to stress positivity. - negates an expression's value. (2's complement for integral expressions)
  • Negation !
    Inverts the value of a boolean expression.
  • Complement ~
    Inverts the bit pattern of an integral expression. (1's complement - 0 to 1 and 1 to 0). Cannot be applied to non-integral types.
  • Cast()
    Persuades compiler to allow certain assignments. Extensive checking is done at compile and runtime to ensure type-safety.

    Shift operators:
  • >> - signed right shift
  • << - signed left shift
  • >>> - unsigned logical right shift

    Assignment: =, +=, -=, *=, /=, %=

    Comparison: <, <=, >, >=, instanceof (object comparison)

    Bitwise operators: &, |, ^ (AND, OR, XOR)

    Short-circuit logical operators: &&, || (AND, OR)

    Ternary operator: Format: a= x?b:c


    VARIABLES

    Method and Automatic variables:

    The two kinds of variables are method variables and automatic (method local) variables.
  • Method variables are accessible anywhere within the class. Local variables are accessible only within the occurring method.
  • Method variables are automatically initialized by the compiler before invoking any constructor. Automatic varibles have to be initialized explicitly or the compiler will generate an error.
  • Member variables can have the same name as the class.
  • Local variables can have the same name as a member variable. Resolution is based on scope.



    STATEMENTS

    Control statements

    looping - for, do-while, while
    decision-making - if-else, switch-case
    exception handling - try-catch-finally, throw
    branching - break, continue, label:, return

    break and continue

    Both statements are used to exit a loop. Continue terminates the current iteration of a loop and starts the next one, behaving as a goto jumping to the top of the loop body.

    Break exits the loop, terminating the current iteration and executing no more iterations.

    In the case of nested loops, to jump out of a particular loop, one can label the loop, and then affix the label to the break or continue statement.
    	 outer: while (true) {
    	     inner: while (true) {
    			break;             // Exits the innermost loop
    			break inner;       // Also exits the innermost loop	
    			break outer;       // Exits the outermost loop
         }
     }
    for

    	for (initialization; termination; increment) {
    	    statement	// termination expression is evaluated before each iteration
    		}	// loop terminates when termination expression returns false
    return

    Used to end execution of a method, returning to where the method was invoked. For methods that are declared to return values (all methods other than of type void), a value of the proper type must follow the keyword return and precede the semicolon which terminates the statement.

    switch

    The switch statement tests a single expression for various values and performs appropriate processing.
    	import java.util.Date;
    	Date today = new Date();
    	switch (today.getDay) {
    		case 0:		//Sunday
    		case 6:		//Saturday
    			System.out.println("It's Soccer Day!");
    			break;
    		case 2:		//Tuesday
    			System.out.println("Tennis at 8:00 am");
    		case 1:		//Monday
    		case 5:		//Friday
    			System.out.println("Office Hours: 10:00 - 5:00");
    			break;
    	}
    	System.out.println("All done!");
    while and do-while

    In the do-while statement, the expression is checked at the end of the block. Thus the statements in the block are executed at least once.
    	while (expression) {
    	    statement
    	}
    	do {
    	    statement(s)
    	} while (expression);	// expression returns a boolean value
    Comments

    Java supports both the /* This is a comment */ comment from C and the // This is a C++ comment comment from C++. However comments that begin with a /** are special. These comments should only be used before a method or class declaration. They indicate that the comment should be included in automatically generated documentation for that declaration.


    ARRAYS

    Java arrays should be declared, allocated and initialized.

    Declaration

    Size need not be specified. Square brackets can come after datatype or before/after variable name. White spaces are fine. Compiler just ignores them.
    	int[] a;	String b[];	Object []c;
    Allocation
    	a = new int[10];         
    	c = new String[arraysize];
    Initialization
    	for (int i = 0; i < a.length; a[i++] = 0);
    All the above three can be done in a single step.
    	int a[] = { 1, 2, 3 };
    	int a[] = new int[] { 1, 2, 3 };  // size should not be specified with the new statement in this case
    	char [] charArray = {'a', 'b', 'c'};
    	String [] stringArray = {"A", "Four", "Element", "Array"};
    Array elements can be accessed with indexes. Array indexes start with 0 and are of the int datatype.

    Multi-dimensional arrays

  • For a rectangular array, space can be allocated all at once.
    	int arr[][] = new int[4][5];	- creates a 4x5 array
  • If each row is desired to have a different number of columns, the fact that a two-dimensional array is actually an array of arrays can be used. The following code allocates a triangular array:
    	int arr[][] = new int[4][];    // allocate the four row arrays
    	for (int i = 0; i < 4; i++) // initialize each of the four rows
    	arr[i] = new int[i + 1];       // row i has i + 1 columns

    CLASSES

    Inheritance

    Inheritance is the ability of a class to invoke the members of a parent class by deriving or inheriting from it. Inheritance provides a natural classification for kinds of objects and allows for the commonality of objects to be explicitly taken advantage of in modeling and constructing object systems.

    Polymorphism

    Polymorphism means 'many forms'. In OOP, it refers to the capability of objects to react differently to the same method. Polymorphism can be implemented in Java in the form of multiple methods having the same name. Java code uses a late-binding technique to support polymorphism; the method to be invoked is decided at runtime.

    Interfaces

    An interface is a contract. It describes the public methods that a class implements and their calling conventions without saying anything about how those methods are implemented. It is the responsibility of each class that implements an interface to provide code to handle the cases where the methods of the interface are called. For e.g while modeling an inventory database where every object has to return a price, the following interface is declared:
    	public interface Price {
    		  public float price();
    	}
    Different classes can then implement the Price interface
    	public class Monopoly extends BoardGame implements Price {
    		// other methods
    		public float price() {
    				return 14.95;
    		}
    	}
    When other code is passed an object, it can test whether the object implements Price with the instanceof operator. For e.g
    	if (o instanceof Price) System.out.println("Subtotal is " + o.price());
    Access levels for class members

    The Java language supports four distinct access levels for member variables and methods:
  • private - A private member is accessible only to the class in which it is defined.
  • protected - It allows the class itself, subclasses and all classes in the same package to access the members.
  • public - Any class, in any package, has access to a class's public members.
  • package - This is the default access. This access level allows classes in the same package to access the members. This level of access assumes that classes in the same package are trusted friends.

    Instance and class members

    Class members are declared using the static modifier. The runtime system allocates class variables once per class regardless of the number of instances created of that class. The system allocates memory for class variables the first time it encounters the class. All instances share the same copy of the class's class variables. Class variables are accessed through an instance or through the class itself.

    Instance members do not have the static modifier. Every time an instance of a class is created, the runtime system creates one copy of each of the class's instance member. Instance members can only be accessed only through a reference to an instance.
    	class sampleClass {
    		int x;	//instance variable
    		static int y;	// class variable
    		public int x() {	//instance method
    			return x;
    			}
    		static public int y() {	//class method
    			return y;
    			}
    		public void setX(int newX) {	//instance method
    			x = newX;
    			}
    		static public void setY(int newY) {     //class method
    			y = newY;
    			}
    		}

    Overloading and Overriding

    Overloading occurs if several methods have the same name but different arguments. The java interpreter sorts out which method to call by matching the argument types in the headers of the methds with the types of the values used in the specific method call. This is called overloading resolution.

    Differences between Overloading and Overriding

  • Overloaded methods have different signatures and can have different return types. Overridden methods have the same signature and return type.
  • There is no limit on the number of overloaded methods in a class. Each parent class method may be overridden at most once in any sub-class. (i.e There cannot be two identical methods in the same class)
  • In overloading, accessibility can vary freely. But overriding methods cannot be more private than the overridden methods.

    this keyword

    The this keyword is used in the following cases:
  • To bypass local variables or parameters that hide member variables having the same name, in order to access the member variable.
  • To make it possible for one overloaded constructor to invoke another overloaded constructor in the same class.
  • To pass a reference to the current object to a method belonging to a different object (as in implementing callbacks, for example).

    super keyword

  • To bypass the overridden version of a method in a subclass and execute the version in the superclass.
  • To bypass a member variable in a subclass in order to access a member variable having the same name in a superclass.
  • To cause a constructor in a subclass to invoke a parameterized constructor in the immediate superclass.

    The finalize method

    All objects have a finalize method. It is inherited from the Object class. The finalize method is used to release system resources other than memory, such as file handles and network connections. The order in which finalize methods are called may not reflect the order in which objects are created.
    This is the signature of the finalize method:
    	protected void finalize() throws Throwable { }
    Nested classes

    There are four categories of classes defined inside of other classes.
  • Top-level nested classes / interfaces - declared as a class member with a static modifier. Can be accessed/instantiated without an instance of the outer class. Can access only static members of the outer class.
  • Non-static inner classes - declared as a class member without the static modifier. Can exist only with an instance of the outer class.
  • Local classes - defined inside a block. Do not have the static or any access modifiers.
  • Anonymous classes - defined when they are constructed. Typically used for creating objects on the fly.

    Garbage collection

    Garbage collection is a mechanism for reclaiming memory from objects that are no longer in use, and making it available for new objects. It runs in a low priority thread and may kick in when memory is too low. While it is not possible to force garbage collection, invoking System.gc may start the garbage collection process.

    Class modifiers

    abstract

    An abstract class cannot be instantiated. Only its subclasses can be instantiated. Abstract classes may contain abstract methods. A method declared abstract is not actually implemented in the current class. It exists only to be overridden in subclasses. It has no body. For e.g.
    	abstract class item {
    		public abstract float price();
    		}
    Abstract methods may only be included in abstract classes. However, an abstract class is not required to have any abstract methods, though most of them do. Each subclass of an abstract class must override the abstract methods of its superclasses or itself be declared abstract.


    Interfaces vs abstract classes

  • Interfaces and abstract classes are almost similar. As a rule, interfaces are used to define behavior over abstract classes.
  • Abstract classes are useful when there are common implementations. i.e Interface defines behavior while abstract class defines a pattern for implementation and a behavioral hierarchy.
  • Interface inheritance groups objects with common behavior while abstract Class inheritance groups objects by implementation pattern.
  • A class can implement many interfaces, but inherit from only one class.

    final

    A final class cannot be extended (subclassed). Mostly this is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve.

    Methods may be declared final as well. This means they may not be overridden in a subclass.

    Fields can be declared final too. However, this has a completely different meaning. A final field cannot be changed after it's initialized, and it must include an initializer statement where it's declared.

    volatile

  • The volatile modifier is used only against variables. It is mostly used in multi-processor environments and indicates that the variable may be modified asynchronously by concurrently running threads, so that all the threads will get the correct value of the variable.
  • Static variables can be volatile but final variables cannot be.

    native

  • The native modifier can be applied only to methods. It indicates that the method is written in a non-Java language and compiled for a single-machine target type
  • Java classes use lot of native methods for performance and for accessing hardware Java is not aware of
  • Java objects can be passed or returned from native methods
  • The native modifier does not affect other modifiers. Native methods can be private
  • System.loadLibrary is used in static initializer code to load native libraries. If the library is not loaded when the static method is called, an UnsatisfiedLinkError is thrown

    transient

  • The transient modifier is used only against class variables (cannot be used for local variables)
  • Transient variables may not be final or static.(But compiler allows the declaration, since it doesn't do any harm)
  • Transient variables are not stored as part of object's persistent state, i.e. not written out during serialization
  • The modifier can be used for security

    Back



    PACKAGES

    All related classes and interfaces are bundled into packages for ease of identification and avoiding conflicts. The following are some of the in-built packages

    The java.io package

    Often programs need to bring in data from an external source or send out data. The information can be in a file, disk, network, memory or another program. Also, it can be of any type - objects, characters, images or sounds. To bring in data, a program opens a stream on an information source (a file, memory or a socket) and reads the data serially. Similarly, a program can send data by opening a stream to an external destination and writing the data out serially.

    The classes in the java.io package can be divided into two class hierarchies based on the data type (either characters or bytes) on which they operate. Streams operate on bytes while Readers/Writers operate on chars.

    The classes can also be grouped on the basis of functionality

  • Low-level Data-sink streams read from or write to specialized data sinks such as strings, files, or pipes. These classes classified on the basis of the data-sink are
    Memory - CharArrayReader, CharArrayWriter, ByteArrayInputStream, ByteArrayOutputStream, StringReader, StringWriter, StringBufferInputStream
    Pipe - PipedReader, PipedWriter, PipedInputStream, PipedOutputStream
    File - FileReader, FileWriter, FileInputStream, FileOutputStream

  • High-level Processing streams perform some sort of operation, such as buffering or character encoding, as they read and write. These classes classified on the basis of usage are
    Buffering - BufferedReader, BufferedWriter, BufferedInputStream, BufferedOutputStream
    Filtering - FilterReader, FilterWriter, FilterInputStream, FilterOutputStream
    Byte/Char conversion - InputStreamReader, OutputStreamWriter
    Concatenation - SequenceInputStream
    Object Serialization - ObjectInputStream, ObjectOutputStream
    Data Conversion - DataInputStream, DataOutputStream
    Counting - LineNumberReader, LineNumberInputStream
    Peeking ahead - PushbackReader, PushbackInputStream
    Printing - PrintWriter, PrintStream

    Accepting user input from the console

    To grab a whole line of input - if the user is just typing in a single value, like a username, for instance - then simply a java.io.Reader can be used, such as java.io.BufferedReader.
    	InputStreamReader isr = new InputStreamReader ( System.in );
    	BufferedReader br = new BufferedReader ( isr );
    	String s = null;
    	try {
    	   while ( (s = br.readLine ()) != null ) {
    		  // do something
    	   }
    	}
    	catch ( IOException ioe ) {
    	   // exception handler
    	}
    If it is needed to do some parsing of a line of input, Java provides a powerful and general facility through the java.io.StreamTokenizer.
    	InputStreamReader isr = new InputStreamReader ( System.in );
    	StreamTokenizer st = new StreamTokenizer ( isr );
    	int c;
    	try {
    		while ( true ) {
    			switch ( c = st.nextToken () ) {
    			case StreamTokenizer.TT_NUMBER:
    				System.out.println ( "Read a number: " + st.nval );
    				break;
    			case StreamTokenizer.TT_WORD:
    				System.out.println ( "Read a word: " + st.sval );
    				break;
    			case default:
    				// do something in other cases (EOL, EOF, whitespace, quotes...)
    				break;
    			}
    		}
    	catch ( IOException ioe ) {
    	   // exception handler
    	}

    The java.lang package

    The package java.lang contains classes and interfaces that are essential to the Java language. These include
  • Object, the ultimate superclass of all classes in Java
  • Thread, the class that controls each thread in a multithreaded program
  • Throwable, the superclass of all error and exception classes in Java
  • Classes that encapsulate the primitive data types in Java
  • Classes for accessing system resources and other low-level entities
  • Math, a class that provides standard mathematical methods
  • String, the class that is used to represent strings

    Because the classes in the java.lang package are so essential, the java.lang package is implicitly imported by every Java source file and hence all of the classes and interfaces in java.lang can be referred using their simple names.

    The java.util package

    The java.util package contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization and miscellaneous utility classes like a string tokenizer, a random-number generator and a bit array.

    The Collections framework

    Interfaces

  • Collection - A basic interface that defines the operations that all the classes that maintain collections of objects typically implement.
  • Set - Extends Collection, sets that maintain unique elements. Set interface is defined in terms of the equals operation
  • SortedSet - Extends Set, maintain the elements in a sorted order
  • List - Extends Collection, maintain elements in a sequential order, duplicates allowed.
  • Map - A basic interface that defines operations that classes that represent mappings of keys to values typically implement
  • SortedMap - Extends Map for maps that maintain their mappings in key order.

    Classes

  • HashSet, HashMap, HashTable - implement hash tables
  • ArrayList, Vector - implement resizable arrays. Both classes extend the List interface.
  • TreeSet, TreeMap - implement balanced tree
  • LinkedList - extends the List interface to implement a linked list

    The Calendar class

    	import java.util.*;
    	public class Now {
    	public static void  main(String arg[]) {
    		Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    
    		String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    		java.text.SimpleDateFormat sdf =  new java.text.SimpleDateFormat(DATE_FORMAT);
    		sdf.setTimeZone(TimeZone.getDefault());
    
    		System.out.println("Now : " + sdf.format(cal.getTime()));
    		}
    	}
    The Date class

    The java.util.Date class encapsulates date and time information and allows Date objects to be accessed in a system-independent manner. Date provides methods for accessing specific date and time measurements and for displaying dates in a variety of standard formats.

    The Date class provides six constructors for creating Date objects. The default constructor creates a Date object with the current system date and time. Other constructors allow Date objects to be set to other dates and times.
    	import java.lang.System;
    	import java.util.Date;
    
    	public class DateApp {
    		public static void main(String args[]){
    		Date today = new Date();
    		String days[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    		System.out.println("Today's date (locale conventions): " +  today.toLocaleString());
    		System.out.println("Today's date (Unix conventions): " + today.toString());
    		System.out.println("Today's date (GMT conventions): " + today.toGMTString());
    		System.out.println("Year: " + today.getYear() + "Month: " + today.getMonth() + "Date: " + today.getDate());
    		System.out.println("Day: " + days[today.getDay()]);
    		System.out.println("Hour: " + today.getHours() + " Minute: " + today.getMinutes() + " Second: " + today.getSeconds());
    		Date newYears2000 = new Date(100,0,1);
    		System.out.println("New Years Day 2000: " + newYears2000.toString());
    		System.out.println("New Years Day 2000 is " + days[newYears2000.getDay()]);
    	 }
    	}
    The SimpleDateFormat class
    	import java.util.*;
    	import java.text.*;
    
    	public class ShowToday {
    		public static void main(String args[]) {
    			ShowToday st = new ShowToday();
    			st.demo();
    			}
    	  public void demo() {
    		System.out.println(easyDateFormat("dd MMMMM yyyy"));
    		System.out.println(easyDateFormat("yyyyMMdd"));
    		System.out.println(easyDateFormat("MM/dd/yy"));
    		System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
    		System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
    		System.out.println(easyDateFormat("h:mm a"));
    		System.out.println(easyDateFormat("H:mm:ss:SSS"));
    		System.out.println(easyDateFormat("K:mm a,z"));
    		System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
    		}
    
    	  public String easyDateFormat (String format) {
    		Date today = new Date();
    		SimpleDateFormat formatter = new SimpleDateFormat(format);
    		String datenewformat = formatter.format(today);
    		return  datenewformat;
    		}
    	  }
    The Stack class

    The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

    When a stack is first created, it contains no items.

    The StringTokenizer class

    The StringTokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
    	StringTokenizer st = new StringTokenizer("this is a test");
    	while (st.hasMoreTokens()) {
    		System.out.println(st.nextToken());
    	}
    prints the following output
    	this
    	is
    	a
    	test
    The Vector class

    The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
    	java.lang.Object
    	  |
    	  +--java.util.AbstractCollection
    			|
    			+--java.util.AbstractList
    				  |
    				  +--java.util.Vector
    Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

    The members of the Vector class include
  • void add (int index, Object element) - Inserts the specified element at the specified position in the Vector
  • boolean add (Object element) - Inserts the Object at the end of the Vector
  • int capacity - Returns the current capacity of this vector
  • Object elementAt (int index) - Returns the component at the specified index

    The below code illustrates the basic usage of the Vector class
    	Vector myVector = new Vector ();	// Create an instance of class Vector
    	myVector = new Vector(50);		// or suggest an initial vector size
    
    	for (int i = 1; i <= 20; i++)	//add some objects
    	{
    		myVector.addElement( new String ("I am string " + i));
    	}
    
    	System.out.println ("Element : " + myVector.elementAt(5) );	// Peek at element 5
    
    	for (Enumeration e = myVector.elements(); e.hasMoreElements();)
    			// Traverse entire list, printing them all out
    	{
    		String myString = (String) e.nextElement();
    		System.out.println(myString);
    	}

    Back



    Exception handling

    Hierarchy of Exception classes

    The Java language requires that exceptions derive from the Throwable class or one of its subclasses.
    When a dynamic linking failure or some other "hard" failure in the virtual machine occurs, the virtual machine throws an Error. Typical Java programs should not catch Errors. In addition, it's unlikely that typical Java programs will ever throw Errors either.
    Most programs throw and catch objects that derive from the Exception class. Exceptions indicate that a problem occurred but that the problem is not a serious systemic problem.
    The RuntimeException class represents exceptions that occur within the JVM (during runtime). An example is NullPointerException, which occurs when a method tries to access a member of an object through a null reference.
    		       Throwable
    			|
    		------------------------
    		|			|
    	        Error  	             Exception
    					|
    				   RuntimeException

    Checked exceptions

    Checked exceptions are all exception objects instantiated from subclasses of the Exception class other than those of the RuntimeException class. These exceptions are checked by the compiler and will result in compiler errors if they are neither caught nor declared.

    The try-catch block

    Exceptions are typically handled by using the try-catch-finally blocks. The try block encloses statements to be executed. One or more catch blocks then associate exception handlers with the try block. The finally block is used to clean up before control is passed elsewhere.

    Creating new exceptions

    For tackling exceptions other than those available in the Throwable family, developers can create their own exceptions by extending the Exception class which can then be thrown using the throw keyword. The below code models a customer in a coffee shop:
    	class TemperatureException extends Exception {}
    	class TooColdException extends TemperatureException {}
    	class TooHotException extends TemperatureException {}
    
    	class VirtualPerson {
    		private static final int tooCold = 65;
    		private static final int tooHot = 85;
    
    		public void drinkCoffee(CoffeeCup cup) throws
    			TooColdException, TooHotException {
    
    			int temperature = cup.getTemperature();
    			if (temperature <= tooCold) {
    				throw new TooColdException();
    			}
    			else if (temperature >= tooHot) {
    				throw new TooHotException();
    			}
    			//...
    		}
    		//...
    	}
    Exceptions which are thrown by a method but not tackled with appropriate catch blocks need to be specified in the signature of the method by using the throws clause. These exceptions are passed up to the subsequent methods in the invocation stack.

    While throwing parent and child exceptions and using catch blocks for both of them, the child exception's catch block should precede that of the parent exception. If not a compiler error would result, since the parent's catch block captures the child exceptions too leaving the child's catch block unreachable.


    File handling

    Reading data from a file

    The program below reads raw data from a file specified on the command line and writes it to the standard output.
    	import java.io.*;
    
    	class ReadRawData {
    
    	public static void main (String args[]) {
    
    	boolean done = false;
    	byte b[] = new byte[1024];
    	int num_bytes = 0;
    
    	FileInputStream fin = null;
    	try {
    		fin = new FileInputStream(args[0]);
    		}
    	catch(ArrayIndexOutOfBoundsException e) {
    		System.out.println("No file name");
    		System.exit(0);
    		}
    	catch (FileNotFoundException e) {
    		System.out.println("File not found " + args[0]);
    		System.exit(0);
    		}
    	catch(IOException e) {
    		System.out.println("Error opening input file " + args[0]);
    		System.exit(0);
    		}
    	catch (Exception e) {
    		System.out.println("Unexpected exception: " + e);
    		System.exit(0);
    		}
    
    	try {
    		num_bytes = fin.read(b);
    		}
    	catch(IOException e) {
    		System.out.println("Finished Reading: " + e);
    		done = true;
    		}
    	catch (Exception e) {
    		System.out.println("Unexpected exception: " + e);
    		System.exit(0);
    		}
    
    	while(!done) {
    		System.out.write(b, 0, num_bytes);
    		try {
    			num_bytes = fin.read(b);
    			}
    		catch(IOException e) {
    			System.out.println("Finished Reading: " + e);
    			done = true;
    			}
    		catch (Exception e) {
    			System.out.println("Unexpected exception: " + e);
    			System.exit(0);
    			}
    		if (num_bytes == -1) done = true;
    		} 	 }	
    	}
    To read data from a file line by line, a BufferedReader can be used:
    	String s;
    	FileInputStream  parmFile = new FileInputStream ("custmaint.ini");
    	BufferedReader br = new BufferedReader (new InputStreamReader (parmFile));
    	while ( (s = br.readLine()) != null)	{
    		System.out.println (s);
    		}
    Writing data to a file

    The following application writes data using a formatted output stream.
    	import java.io.*;
    
    	class PrintToAFile  {
    
    	public static void main (String args[]) {
    	try {
    		FileOutputStream fout =  new FileOutputStream("test.out");	//open file
    		PrintStream ps = new PrintStream(fout);	//convert the FileOutputStream into a PrintStream
    
    		ps.println("Hello There!");	//write some data
    		ps.println(1 + " + " + 1 + " = " + (1+1));
    		}
    	catch (IOException e) {
    		System.out.println("Error opening file: " + e);
    		System.exit(1);
    		}
    	} 	}
    Appending data to a file

    The FileOutputStream constructor takes a second boolean argument that can be specified as true to append data to a file.
    	public FileOutputStream(String name, boolean append)
    		throws IOException

    Back


    Networking

    The java.net package contains classes for implementing networking applications.

    The InetAddress class

    Used to resolve IP addresses to hostnames and vice versa
    	//get IP address
    	InetAddress inet = InetAddress.getByName("www.java.com");
    	System.out.println ("IP  : " + inet.getHostAddress());
    	//get hostname
    	InetAddress inet = InetAddress.getByName("209.204.220.121");
    	System.out.println ("Host: " + inet.getHostName());
    The Socket class

    Implements client sockets (also called just sockets). A socket is an endpoint for communication between two machines.

    The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.

    The main members of the Socket class include:
  • void bind(SocketAddress bindpoint) - Binds the socket to a local address.
  • void connect(SocketAddress endpoint) - Connects this socket to the server.

    The URL class

    The URL class represents a URL, a resource on the web. A resource can be something as simple as a file or a directory, or a reference to a more complicated object, such as a query to a database or to a search engine.

    Connecting to remote servers

    Fetching a web page using HTTP

    The easiest way to fetch files using HTTP is to use the java.net.URL class. The openStream() method will return an InputStream instance, from which the file contents can be read. For added control, openConnection method can be used, which will return a URLConnection object.
    Using the java.net.URL.openStream() method to return the contents of a URL specified as a command line parameter:
    	import java.net.*;
    	import java.io.*;
    
    	public class getwebURL
    	{
    	public static void main(String args[]) throws Exception
    	{
    		try
    		{
    		if (args.length != 1)  // check if a parameter was entered
    			{
    			System.err.println ("Invalid command parameters");	// print message, pause, then exit
    			System.in.read();
    			System.exit(0);
    			}
    			URL url = new URL(args[0]);		// create a URL instance
    			
    			InputStream in = url.openStream();  // get an input stream for reading
    			BufferedInputStream bufIn = new BufferedInputStream(in);	// create a buffered input stream for efficiency
    			for (;;)		            // repeat until EOF
    			{
    			int data = bufIn.read();
    			if (data == -1)		// check for EOF
    				break;
    			else
    				System.out.print ( (char) data);
    			}
    		}
    		catch (MalformedURLException mue)
    		{
    			System.err.println ("Invalid URL");
    		}
    		catch (IOException ioe)
    		{
    			System.err.println ("I/O Error - " + ioe);
    		}
    	}
    	}
    Sending data to a web server

    There are two methods to send data to a web server - GET and POST. In GET, the data is simply attached as parameters at the end of the URL. In the POST method, name/value pairs are written to the output stream.

    Posting data to a web server:
    	URL url = new URL("http://www.swengg.com/script");
    	URLConnection connection = url.openConnection();
    
    	connection.setDoOutput (true);
    
    	PrintWriter out = new PrintWriter (connection.getOutputStream());
    	out.print(name1 + "=" + URLEncoder.encode(value1) + "&");
    	out.print(name2 + "=" + URLEncoder.encode(value2) + "\n");
    	out.close();

    Serialization

    Java provides a feature - object serialization - that allows any object that implements the Serializable interface to be turned into a sequence of bytes that can be sent over the network or saved in a file or database and later be fully restored into the original object.

    The below code serializes a List of String objects and then deserializes them.
    	import java.io.*;
    	import java.util.*;
    
    	public class serialize1 {
    
    	 public static void main(String[] aArguments) {
    		List quarks = new ArrayList();		    //create a Serializable List
    		quarks.add("up");
    		quarks.add("down");
    		quarks.add("strange");
    		quarks.add("charm");
    		quarks.add("top");
    		quarks.add("bottom");
    
    		//serialize the List
    		//note the use of generic interface references
    
    		//declared here only to ensure visibility in finally clause
    		ObjectOutput output = null;
    		try{
    			OutputStream file = new FileOutputStream( "quarks.ser" );	//use buffering
    			OutputStream buffer = new BufferedOutputStream( file );
    			output = new ObjectOutputStream( buffer );
    			output.writeObject(quarks);
    		}
    		catch(IOException ex){
    			System.out.println ("Cannot perform output.");
    		}
    		finally{
    		  try {
    			if (output != null) {	//flush and close "output" and its
    			   output.close();		//underlying streams
    			}
    		  }
    		  catch (IOException ex ){
    			System.out.println ("Cannot close output stream.");
    		  }
    		}
    		//deserialize the quarks.ser file.  Note the use of generic interface references
    
    		ObjectInput input = null;	 //declared here only to ensure visibilty
    									 //	in finally clause
    		try{
    		  InputStream file = new FileInputStream( "quarks.ser" );	//use buffering
    		  InputStream buffer = new BufferedInputStream( file );
    		  input = new ObjectInputStream ( buffer );
    		  //deserialize the List
    		  ArrayList recoveredQuarks = (ArrayList)input.readObject();
    		  //display its data
    		  Iterator quarksItr = recoveredQuarks.iterator();
    		  while ( quarksItr.hasNext() ) {
    			System.out.println( (String)quarksItr.next() );
    		  }
    		}
    		catch(IOException ex){
    			System.out.println ("Cannot perform input.");
    		}
    		catch (ClassNotFoundException ex){
    			System.out.println ("Unexpected class found upon input.");
    		}
    		finally{
    			try {
    				if ( input != null ) {	//close "input" and its underlying streams
    				input.close();
    			}
    		}
    			catch (IOException ex){
    				System.out.println ("Cannot close input stream.");
    			}
    		}
    	  }
    	}

    Remote method invocation (RMI)

    RMI is a mechanism for invoking an object's methods, even though the object is executing on a foreign JVM. RMI is similar to remote procedure calls (RPCs), but has an added advantage - method signatures can contain Java objects as well as primitive data types. Even objects that a foreign JVM has never encountered before can be used, so new tasks and methods can be passed across a network.

    RMI can be utilised in the following cases:
  • to execute code on remote systems (distributed systems)
  • the network has all its machines capable of supporting JVMs
  • to prevent RMI applications from being used from machines that don't support JVMs

    RMI applications

    RMI applications (also refered to as distributed object applications) are usually comprised of two separate programs: a server and a client. A typical server application creates some remote objects, makes references to them accessible and waits for clients to invoke methods on these remote objects. A typical client application gets a remote reference to one or more remote objects in the server and then invokes methods on them. RMI provides the mechanism by which the server and the client communicate and pass information back and forth.
    RMI applications have the following functionalities encompassed:
  • Locating remote objects: Applications can use one of two mechanisms to obtain references to remote objects. An application can register its remote objects with RMI's simple naming facility, the rmiregistry, or the application can pass and return remote object references as part of its normal operation.
  • Communicate with remote objects: Details of communication between remote objects are handled by RMI; to the programmer, remote communication looks like a standard Java method invocation.
  • Loading class bytecodes for objects that are passed around: Because RMI allows a caller to pass objects to remote objects, RMI provides the necessary mechanisms for loading an object's code, as well as for transmitting its data.

    Remote object

    An object becomes remote by implementing a remote interface, which has the following characteristics.
  • A remote interface extends the interface java.rmi.Remote.
  • Each method of the interface declares java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions.

    Different layers in the RMI architecture

    Stub and Skeleton layer:
    It lies just beneath the view of the developer. This layer intercepts method calls made by the client to the interface reference variable and redirects these calls to a remote RMI service.
    Remote Reference layer:
    This layer understands how to interpret and manage references made from clients to the remote service objects.
    Transport layer: The transport layer is based on TCP/IP connections between machines in a network. It provides basic connectivity, as well as some firewall penetration strategies.

    Components of an RMI System

    A working RMI system is composed of the following parts.
  • Interface definitions for the remote services
  • Implementations of the remote services
  • Stub and Skeleton files
  • A server to host the remote services
  • An RMI Naming service that allows clients to find the remote services
  • A class file provider (an HTTP or FTP server)
  • A client program that needs the remote services

    The RMI Registry

    An RMI registry is a remote object that maps names to remote objects. A server registers its remote objects with the registry so that they can be looked up. When an object wants to invoke a method on a remote object, it must first lookup the remote object using its name. The registry returns to the calling object a reference to the remote object, using which a remote method can be invoked.

    Steps in bulding an RMI system

    1. Write and compile Java code for interfaces
    	public interface Calculator  extends java.rmi.Remote {
    		public long add(long a, long b)   throws java.rmi.RemoteException;
    		}
    2. Write and compile Java code for implementation classes
    	public class CalculatorImpl extends
    		java.rmi.server.UnicastRemoteObject implements Calculator {
    
    		// Implementations must have an explicit constructor
    		// in order to declare the RemoteException exception
    		public CalculatorImpl() throws java.rmi.RemoteException {
    			super();
    			}
    
    		public long add(long a, long b)  throws java.rmi.RemoteException {
    			return a + b;
    			}
    	}
    3. Generate Stub and Skeleton class files from the implementation classes
    The java compiler rmic can be used to generate the stub and skeleton files. After running rmic, the Calculator_stub.class and the Calculator_skel class should be created.
    	>rmic CalculatorImpl
    4. Write Java code for a remote service host program
    Remote RMI services are hosted in a server process. The below class provides a simple server to host the CalculatorService.
    	import java.rmi.Naming;
    
    	public class CalculatorServer {
    
    		public CalculatorServer() {
    			try {
    				Calculator c = new CalculatorImpl();
    				Naming.rebind("rmi://localhost:1099/CalculatorService", c);
    				} catch (Exception e) {
    				System.out.println("Trouble: " + e);
    			}
    		}
    
    	public static void main(String args[]) {
    		new CalculatorServer();
    		}
    	}
    5. Develop Java code for RMI client program
    	import java.rmi.Naming;
    	import java.rmi.RemoteException;
    	import java.net.MalformedURLException;
    	import java.rmi.NotBoundException;
    
    	public class CalculatorClient {
    		public static void main(String[] args) {
    			try {
    				Calculator c = (Calculator)
    				Naming.lookup("rmi://localhost/CalculatorService");
    				System.out.println( c.add(4, 5) );
    			}
    			catch (MalformedURLException murle) {
    				System.out.println("MalformedURLException" + murle);
    			}
    			catch (RemoteException re) {
    				System.out.println("RemoteException" + re);
    			}
    			catch (NotBoundException nbe) {
    				System.out.println("NotBoundException" + nbe);
    			}
    			catch (java.lang.ArithmeticException ae) {
    				System.out.println("java.lang.ArithmeticException" + ae);
    			}
    		}
    	}
    6. Install and run the RMI system
  • First the RMIRegistry is started.
    	>rmiregistry
  • Then the server that hosts the CalculatorService is started.
    	>java CalculatorServer
  • The client program is then started.
    	>java CalculatorClient

    Thread management

    Two ways of implementing threads

    1. Extending the "java.lang.Thread" class
  • a new class is created, extending the Thread class
  • a public void run method is provided, otherwise empty run in Thread class will be executed
  • an instance of the new class is created
  • the start method on the instance is called (run should not be called– it will be executed on the same thread)

    2. Implementing the "Runnable" interface
  • a new class implementing the Runnable interface is created
  • a public void run method is provided
  • an instance of this class is created
  • a Thread is created, passing the instance as a target – new Thread(object)
  • Target should implement Runnable, Thread class implements it, so it can be a target itself
  • the start method on the Thread is called

    The java.lang.Thread class

    Thread currentThread - Returns an object reference to the thread in which it is invoked.
    String getName - Retrieve the name of the thread object or instance.
    int getPriority - Retrieve the thread instance's priority.
    boolean isAlive - Determine if the thread is currently running.
    void run - This method is the entry point for threads, like the main method for applications.
    void start - Start the thread by calling its run method.
    void sleep - Suspend a thread for a specified amount of time (in milliseconds).
    void wait - moves an object into a waiting state where it waits for a lock to be released.
    void notify - Remove a thread from the waiting state and place it in the ready-to-run state.

    The synchronized keyword

    The synchronized keyword
  • allows only one thread to access data at a time.
  • prevents interruption by other threads.
  • prevents other threads from running.
    The below sample starts two synchronized threads, which output 1 to 10 twice
    	public class MyThread implements Runnable {
    		private String holdA = "This is ";
    		private int[] holdB = {1,2,3,4,5,6,7,8,9,10};
    		public static void main(String args[]) {
    			MyThread z = new MyThread();
    			( new Thread(z) ).start();
    			( new Thread(z) ).start();
    		}
    		// Any object implementing a synchronized keyword is a monitor:
    		public synchronized void run() {
    			for(int w = 0;w < 10;w++) {
    				System.out.println(holdA + holdB[w] + ".");
    			}
    		}
    	}

    Back


    JDBC

    The java.sql package

    The DriverManager class

    The DriverManager class is the basic service for managing a set of JDBC drivers.

    As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property. This allows a user to customize the JDBC Drivers used by their applications.

    A program can also explicitly load JDBC drivers at any time. For e.g, the my.sql.Driver is loaded with the following statement
    	 Class.forName("my.sql.Driver");
    	
    When the getConnection method is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.

    The Connection interface

    The Connection interface represents a connection (session) with a specific database. Within the context of a Connection, SQL statements are executed and results are returned.

    By default the Connection automatically commits changes after executing each statement. If auto commit has been disabled, the commit method must be called explicitly; otherwise, database changes will not be saved.

    Connecting to a DBMS

    This involves 2 steps - loading the appropriate driver and making the connection.
    	//step1   
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");    // loads the JDBC-ODBC bridge driver
    	//or
    	Class.forName("jdbc.Driverxyz")   // loads a class name jdbc.Driverxyz for a particular database
    	//step2
    	Connection con = DriverManager.getConnection(url, "myLogin", "myPassword");  // connects the driver to the DB
    					//For JDBC-ODBC driver, url = jdbc:odbc:data-source-name

    The Statement class

    The Statement object can be used to run a static SQL statement and return the results. The executeUpdate method is used to execute the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as a DDL statement.
    	Statement stmt = con.createStatement();		//where con is a valid connection
    	stmt.executeUpdate( "INSERT INTO COFFEES " +
    			"VALUES ('Colombian', 101, 7.99, 0, 0)");
    For select queries, the executeQuery method of the Statement class is used. It returns a ResultSet object.


    The ResultSet class

    The getXXX methods

    Database column values are usually retrieved using the getXXX methods for the appropriate datatype.
    	String query = "SELECT COF_NAME, PRICE FROM COFFEES";
    	ResultSet rs = stmt.executeQuery(query);         //result of query is stored in ResultSet class
    	while (rs.next()) {
    		String s = rs.getString("COF_NAME");      //getString method is used for string-type data
    		float n = rs.getFloat("PRICE");	       //getFloat for floating-point data		
    		System.out.println(s + "   " + n);
    	}

    Different getXXX methods for converting SQL datatypes to Java datatypes

    	Java method 	SQL Type 
    	----------- 	--------
    	getInt 		INTEGER
    	getLong		BIGINT
    	getFloat	REAL
    	getDouble 	FLOAT
    	getBignum 	DECIMAL
    	getBoolean 	BIT
    	getString 	VARCHAR
    	getString 	CHAR
    	getDate		DATE
    	getTime		TIME
    	getTimestamp 	TIME STAMP
    	getObject       any type
    The PreparedStatement class

    Unlike a Statement object, the PreparedStatement object is given an SQL statement when it is created. In most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but a precompiled SQL statement and when it is executed, the DBMS can just run its SQL statement without having to compile it leading to faster execution times.

    Passing parameters to the PreparedStatement object

    The following chunk of code executes a prepared statement with two parameters, an integer and a string, after setting values for the parameters.
    	PreparedStatement updateSales = con.prepareStatement("UPDATE ITEMS SET ITEM_PRICE = ? WHERE ITEM_NAME LIKE ?");
    	updateSales.setInt(1, 75);
    	updateSales.setString(2, "Road Atlas");
    	updateSales.executeUpdate();
    The CallableStatement class

    The following code creates a stored procedure and executes it using a CallableStatement object.
    	String createProcedure = "create procedure SHOW_SUPPLIERS " +
    				 "as " +
    				 "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " +
    				 "from SUPPLIERS, COFFEES " +
    				 "where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " +
    				 "order by SUP_NAME";
    	Statement stmt = con.createStatement();
    	stmt.executeUpdate(createProcedure);
    	CallableStatement cs = con.prepareCall("{call SHOW_SUPPLIERS}");
    	ResultSet rs = cs.executeQuery();
    The DatabaseMetaData class

    The DatabaseMetaData class can be used to enquire databases. e.g the following code fragment asks the database for its product name and how many simultaneous connections can be made to it.
    	Connection con = DriverManager.getConnection( "jdbc:odbc:mydatasource", "user", "password");
    	DatabaseMetaData md = con.getMetaData();
    	if (md==null) {
    	  System.out.println("No Database Meta Data");
    	} else {
    	  System.out.println("Database Product Name   : " +  md.getDatabaseProductName());
    	  System.out.println("Allowable active connections: "+  md.getMaxConnections());
    	}
    The ResultSetMetaData class

    The ResultSetMetaData class is used for finding out the number of columns in the output of a query and also for determining the data type of each column.
            try {
                ResultSet result = stmt.executeQuery(
                    "SELECT * FROM Items ORDER BY ItemCode DESC;");
                ResultSetMetaData meta = result.getMetaData();
    
                int numbers = 0;
                int columns = meta.getColumnCount();
                for (int i=1;i<=columns;i++) {
                    System.out.println (meta.getColumnLabel(i) + "\t"
                                  + meta.getColumnTypeName(i));
                    if (meta.isSigned(i)) { 	// is it a signed number?
                        numbers++;
                    }
                }
                System.out.println ("Columns: " + columns + " Numeric: " + numbers);
    
                con.close();
            }
            catch (Exception e) {
              e.printStackTrace();
            }

    Database connections

    Connecting to SQL Server

    The following code uses the Microsoft JDBC driver for SQL Server to connect to SQL Server.
    	import java.sql.*;
    	class sqlserver1
    		{
    		public static void main(String[] args)
    			{
    				try {
    	//	Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver" );
    				  DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
    		String connStr = "jdbc:microsoft:sqlserver://EMACHINES\\TANTRA:1433;";
    		connStr += "databasename=esupplies;SelectMethod=direct";
    		Connection conn = DriverManager.getConnection( connStr,"Kandas","ustatesa");
    		System.out.println("Connection established");
    
    			}
    				catch (Exception e) {
    				System.out.println("Error: " + e);
    			}
    		}
    	}
    For compilation
    - On Unix: $ javac -classpath ".:./lib/mssqlserver.jar:./lib/msbase.jar:./lib/msutil.jar" Test.java
    - On Windows: $ javac -classpath ".;./lib/mssqlserver.jar;./lib/msbase.jar;./lib/msutil.jar" Test.java

    Connecting to Microsoft Access

    	import java.sql.*;
    	class accessConnect
    		{
    		public static void main(String[] args)
    			{
    				try {
    				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    				String filename = "d:/java/mdbtest.mdb";
    				String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
    				database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
    
    				Connection con = DriverManager.getConnection( database ,"",""); 
    			}
    				catch (Exception e) {
    				System.out.println("Error: " + e);
    			}
    		}
    	}
    JDBC Exceptions

    JDBC provides three types of exceptions
  • SQLException - The SQLException is the basis of all JDBC exceptions. It consists of three pieces of information, a String message, like all children of Exception, another String containing the XOPEN SQL state, and a driver/source specific int for an additional error code. In addition, multiple SQLException instances can be chained together.
  • SQLWarning - The SQLWarning class is similar to SQLException, however it is considered a noncritical error and is not thrown. It is up to the programmer to poll for SQLWarning messages through the getWarnings methods of Connection, ResultSet, and Statement. Also, the next time something is done with a Connection, ResultSet, or Statement, the previous warnings are cleared out.
  • DataTruncation - The DataTruncation class is a special type of SQLWarning. It is reported with other SQLWarning instances and indicates when information is lost during a read or write operation. To detect a truncation, it is necessary to perform an instanceof DataTruncation check on each SQLWarning from a getWarnings chain.

    Transaction handling

    To commit a transaction, the commit method on the appropriate connection can be called and the rollback method can be used to roll back. By default, all new connections are in auto-commit mode, which means that each "execute" is a complete transaction. Connection.setAutoCommit can be called to change the default. Any locks held by a transaction are released upon the method commit.
    	Connection con = DriverManager.getConnection(...);
    	con.setAutoCommit(false);
    	Statement s = con.createStatement();
    	try
    	{
    	s.executeUpdate("SQL statement 1");
    	s.executeUpdate("SQL statement 2");
    	con.commit();
    	}
    	catch (SQLException sqlexc)
    	{
    	con.rollback();
    	}

    Back



    JAVABEANS

    The JavaBeans API makes it possible to write component software in the Java programming language. Components are self-contained, reusable software units that can be visually composed into composite components, applets, applications and servlets using visual application builder tools. Java Bean components are known as Beans.

    Introspection

    Builder tools discover a Bean's features (i.e its properties, methods, and events) by a process known as introspection. Beans support introspection in two ways

  • by adhering to specific rules, known as design patterns, when naming Bean features. The Introspector class examines Beans for these design patterns to discover Bean features. The Introspector class relies on the core reflection API. The JDK provides a collection of classes and interfaces for introspection and dynamic manipulation of objects, commonly known as the Reflection API. Reflection is one of the core Java APIs and is packaged in java.lang.reflect

  • Beans explicitly provide property, method, and event information with a related Bean Information class, which implements the BeanInfo interface. A BeanInfo class explicitly lists those Bean features that are to be exposed to application builder tools. The Introspector class can query the Bean directly or work with the complementary Bean Information class.

    Characteristics of JavaBeans

  • Properties - Properties are a Bean's appearance and behavior characteristics that can be changed at design time. Builder tools introspect on a Bean to discover its properties, and expose those properties for manipulation.
    The exposed properties can be customized at design time in two ways - by using property editors, or by using more sophisticated Bean customizers.
  • Events - Beans use events to communicate with other Beans. A Bean that wants to receive events (a listener Bean) registers its interest with the Bean that fires the event (a source Bean). Builder tools can examine a Bean and determine which events that Bean can fire (send) and which it can handle (receive).
  • Persistence - Persistence enables Beans to save and restore their state. Once Beans properties have been changed, the state of the Bean can be saved and restored later, property changes intact. JavaBeans uses Java Object Serialization to support persistence.
  • Methods - A Bean's methods are no different than Java methods, and can be called from other Beans or a scripting environment. By default all public methods are exported.

    JavaBeans and Java classes

    Beans are Java classes that can be manipulated in a Visual Builder tool and composed into applications. Any Java class that adheres to certain conventions regarding property and event interface definitions can be a JavaBean.

    Introspection, by which a builder tool analyzes how a Bean works, differentiates Beans from typical Java classes. Because Beans are coded with predefined patterns for their method signatures and class definitions, tools that recognize these patterns can "look inside" a Bean and determine its properties and behavior.

    Introspection allows a Bean's state to be manipulated at design time - the time it is being assembled as a part within a larger application. For this to work, method signatures within Beans must follow a certain pattern so that introspection tools recognize how Beans can be manipulated, both at design time and at run time.

    In effect, Beans publish their attributes and behaviors through special method signature patterns that are recognized by beans-aware application construction tools. However, these construction tools are not required to build or test beans. The pattern signatures are easily recognized by human readers, as well as builder tools.

    Invisible Beans

    Most Beans have a GUI representation. However developers can implement invisible Beans that have no GUI representation. They can call methods, fire events, save persistent state etc. like all Beans but they just do not have a screen appearance.

    Invisible Beans are used either as shared resources within GUI applications or as components in building server applications. Invisible Beans can be represented visually in an application builder tool and they can have a GUI customizer that configures the Bean.

    Back



    ENTERPRISE JAVABEANS (EJB)

    EJB provides a component architecture for building distributed, component-based applications. EJB are server side components managed by an EJB container. A normal Java Bean is basically an intra-process Component, whereas an EJB is an inter-process Component i.e an EJB component can be accessed and used by many processes existing in different JVMs.

    EJBs written using only features in the EJB specification are portable across J2EE application servers. The EJB model enables developers to concentrate on programming the business logic while system-level services are provided by the EJB container.

    A typical EJB Architecture consists of
  • an EJB server,
  • EJB containers that runs on these servers,
  • EJBs that run in these containers,
  • EJB clients and
  • other auxiliary systems like the Java Naming and Directory Interface (JNDI) and the Java Transaction Service (JTS).

    EJB container

    EJB containers are part of the EJB framework. They live and operate inside EJB servers. EJBs written by developers are installed into and executed within the confines of an EJB container.

    EJB containers act as the interface between an EJB and the outside world. An EJB client never accesses a bean directly. Any bean access is done through container-generated methods which in turn invoke the bean's methods.

    The two types of containers are session containers that may contain transient, non-persistent EJBs whose states are not saved at all and entity containers that contain persistent EJBs whose states are saved between invocations.

    Services provided by the EJB container

  • Lifecycle: Individual enterprise Beans do not need to explicitly manage process allocation, thread management, object activation or object destruction. The EJB container does this all on behalf of the Bean
  • State management: Individual enterprise Beans do not need to explicitly save or restore conversational object state between method calls
  • Security: The container performs all security checking on behalf of the Bean
  • Transactions: The EJB container can manage the start, enrolment, commitment and rollback of transactions
  • Persistence: The EJB container can automatically handle persistent data on behalf of the Bean

    Types of EJBs

    EJBs come in two flavours - a session Bean is a transient object while an entity Bean is a persistent object. The call back methods that are required for these are also different.
    These interfaces are
  • javax.ejb.EntityBean and
  • javax.ejb.SessionBean

    Entity Beans are components that represent some persistent data, usually a row in a database table. Thus an Entity EJB Object will have fields that correspond to columns of a table. There are two mechanisms by which the persistence of such data is managed by a typical EJB container.
  • Container managed Persistence - In Container managed Persistence or CMP, the Container generates all the JDBC code required to create, insert, update or delete rows from a table
  • Bean Managed Persistence - In Bean managed persistence or BMP, the Bean developer has to provide the code to achieve this. CMP is normally prefered, though there may be situations when developers have to use a BMP bean

    Session Beans are normally used to control transactions between a client and an Entity Bean. Session beans themselves do not represent any persistent data but can access databases through JDBC calls. There are two types of session beans:

    Stateless session beans are shared beans that can be used by many clients. They need not know the conversational state of any transaction i.e the bean does not retain any state information between two successive methods calls invoked on it. This is similar to the familiar http protocol which does not maintain any state information.

    Stateful Session beans on the other hand maintain conversational state in a transaction and retains state information between successive method calls till the transaction is completed, after which the bean is destroyed. Therefore stateful Session Beans are not shared and each transaction will have a unique bean associated with it.

    EJB interfaces

    Each EJB provides some interfaces that its clients use to work with the EJB.

    Remote Interface - is the business end of the EJB. This is the set of actual services provides by the EJB.

    Home Interface - is the book-keeping interface. It helps clients create a new instance of an EJB, or to find an existing instance of an EJB. The methods used to find existing EJBs are known as finder methods. Since session beans are not designed to be shareable, there are no session bean finder methods.

    The home interface doesn't actually need an implementation since its tasks are straightforward enough that the EJB container can automatically provide the implementation. The remote interface however needs an implementation which is supplied by the EJB programmer.


    Session beans

    Session Beans implement the javax.ejb.SessionBean interface which has the following methods:
    	public interface javax.ejb.SessionBean
    		extends javax.ejb.EnterpriseBean 	{
    	public abstract void ejbActivate  ();
    	public abstract void ejbPassivate  ();
    	public abstract void ejbRemove  ();
    	public abstract void setSessionContext (SessionContext ctx);
    	}
    ejbActivate - prepare a Session Bean for transition from a passive state to active
    ejbPassivate - prepare for transition from an active state to passive
    ejbRemove - prepare a Session Bean for removal or destruction like closing DB connections and file handles, permanently deallocating system resources etc.
    setSessionContext - pass to a Session Bean, immediately after the Bean has been instantiated, various container-supplied context information for storage within the Session Bean

    Choosing between stateful and stateless session beans

    Stateful session bean are required if any of the following conditions are true
  • The bean's state must be initialized when it is created
  • The bean needs to hold information about the client across method invocations
  • The client is an interactive application

    Since the main goal of a session bean is to represent a client in the J2EE server, most session beans are stateful. However, sometimes stateless session beans are used as in the following cases:
  • The bean performs a task that is not tailored to the needs of a particular client. For e.g, a stateless session bean might be used to fetch a commonly used set of data from a database
  • The bean doesn't need to hold information about the client across method invocations

    Differences between the stateful session bean and the servlet's HttpSession object

  • The Stateful Session Bean supports transaction service, lifecycle management, RMI, instance cache, thread safety but the HttpSession object does not
  • Stateful session beans can be used by both web-based and non-web-based clients (like Swing etc.)
  • The HttpSession object is a simple java object and takes very less time and resources to maintain a client state. The Stateful Session bean takes more time to process a request since it supports a number of services
  • The stateful session bean can be used for multiple operations for a single http request. The HttpSession object cannot process multiple operations for a single http request


    Entity beans

    An Entity Bean is comprised of the following elements, which are developed by the Enterprise Bean Provider:
  • The Component Interface is the client view of the bean. It contains all the "business methods" of the bean
  • The Home Interface contains all the methods for the bean life cycle (creation, suppression) and for instance retrieval (finding one or several bean objects) used by the client application. It can also contain methods called "home methods," supplied by the bean provider, for business logic which is not specific to a bean instance
  • The Primary Key class (for entity beans only) contains a subset of the bean's fields that identifies a particular instance of an entity bean. This class is optional since the bean programmer can alternatively choose a standard class (for e.g java.lang.String)
  • The bean implementation class implements the business methods and all the methods (described in the EJB specification) allowing the bean to be managed in the container
  • The deployment descriptor, containing the bean properties that can be edited at assembly or deployment time.

    The javax.ejb.EntityBean interface

    The javax.ejb.EntityBean interface includes the methods listed below used by the container to manage the bean, synchronize the bean with its underlying data store and provide information to the bean about its current environment.
    	Method 						Description 
    	ejbActivate() and ejbPassivate()		Used by the container to manage bean instances 
    	ejbLoad() and ejbStore()			Used to synchronize the data in the entity bean's persistent fields with the underlying data 
    								store. The implementation of these methods depends upon the bean's persistence model
    	setEntityContext() and unsetEntityContext()	Used to pass information to the entity bean from the container
    	ejbRemove()					Used to delete a record from the underlying data store

    Message-driven Beans

    A message-driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. It acts as a JMS message listener, similar to an event listener except that it receives messages instead of events. The messages may be sent by any J2EE component - an application client, another enterprise bean, a web component - or by a JMS application or system that does not use J2EE technology.

    Unlike session and entity beans, clients do not access message-driven beans through interfaces. It has only a bean class.

    In several respects, a message-driven bean resembles a stateless session bean.
  • A message-driven bean's instances retain no data or conversational state for a specific client
  • All instances of a message-driven bean are equivalent, allowing the EJB container to assign a message to any message-driven bean instance. The container can pool these instances to allow streams of messages to be processed concurrently
  • A single message-driven bean can process messages from multiple clients

    Back



    SERVLETS

    Servlets are the Java platform technology for extending and enhancing web servers. Servlets are java programs that run on java-enabled web or application servers and provide a component-based, platform-independent method for building web-based applications.

    Characteristics of servlets

  • A Servlet, in its most general form, is an instance of a class which implements the javax.servlet.Servlet interface. Most Servlets, however, extend one of the standard implementations of that interface, namely javax.servlet.GenericServlet and javax.servlet.http.HttpServlet.
  • To be a servlet, a class should extend HttpServlet and override doGet or doPost (or both), depending on whether the data is being sent by GET or by POST. These methods take two arguments: an HttpServletRequest and an HttpServletResponse.
  • The HttpServletRequest has methods that let developers find out about incoming information such as FORM data, HTTP request headers etc.
  • The HttpServletResponse has methods that lets programmers specify the HTTP response line (200, 404, etc.), response headers (Content-Type, Set-Cookie, etc.), and most importantly, helps obtain a PrintWriter used to send output back to the client. For simple servlets, most of the effort is spent in println statements that generate the desired page.
  • doGet and doPost throw two exceptions ServletException and IOException, so it is required to include them in the declaration. Also classes in java.io (for PrintWriter, etc.), javax.servlet (for HttpServlet, etc.), and javax.servlet.http (for HttpServletRequest and HttpServletResponse) have to be imported.
  • doGet and doPost are called by the service method, and sometimes programmers may want to override service directly, e.g. for a servlet that handles both GET and POST request.

    The lifecycle of servlets

    Servlets run on the web server platform as part of the same process as the web server itself. The web server is responsible for initializing, invoking, and destroying each servlet instance.

    A web server communicates with a servlet through a simple interface, javax.servlet.Servlet. This interface consists of three main methods:
  • init() - invoked when the servlet is first loaded. Used for initialization.
  • service() - performs all the functionality of the servlet.
  • destroy() - invoked when the servlet is unloaded. Used for cleaning up.
    and two ancillary methods:
  • getServletConfig()
  • getServletInfo()

    Components of the service() method

    Each request message from a client results in a single call to the servlet's service() method. The service() method reads the request and produces the response message from its two parameters:
  • A ServletRequest object with data from the client. The data consists of name/value pairs of parameters and an InputStream. Several methods are provided that return the client's parameter information. The InputStream from the client can be obtained via the getInputStream() method. This method returns a ServletInputStream, which can be used to get additional data from the client. If it is required to process character-level data instead of byte-level data, a BufferedReader can be obtained instead with getReader().
  • A ServletResponse represents the servlet's reply back to the client. When preparing a response, the method setContentType() is called first to set the MIME type of the reply. Next, the method getOutputStream() or getWriter() can be used to obtain a ServletOutputStream or PrintWriter, respectively, to send data back to the client.

    Structure of a servlet

    The below code represents a simple servlet that sends a small, static HTML page to the browser.
    	import java.io.*;
    	import javax.servlet.*;
    	public class SampleServlet implements Servlet {
    		private ServletConfig config;
    
    		public void init (ServletConfig config)	throws ServletException {
    			this.config = config;
    			}
    
    		public void destroy() {} //do nothing
    	  
    		public ServletConfig getServletConfig() {
    			return config;
    			}
    	  
    		public String getServletInfo() {
    			return "A Simple Servlet";
    			}
    	  
    		public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException  {
    			res.setContentType( "text/html" );
    			PrintWriter out = res.getWriter();
    			out.println( "<html><head><title>A Sample Servlet</title></head><body>" );
    			out.println( "<h1>A Sample Servlet</h1>" );
    			out.println( "</body></html>" );
    			out.close();
    			}
    	}
    Handling both GET and POST data

    A servlet can handle both GET and POST data, simply by having its doPost method call doGet or by overriding service (which calls doGet, doPost, doHead, etc.). This is a standard practice, since it requires very little extra work and permits flexibility on the part of the client.
    	import java.io.*;
    	import javax.servlet.*;
    	import javax.servlet.http.*;
    	import java.util.*;
    
    	public class ThreeParams extends HttpServlet {
    		public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    		{
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		String title = "Reading Three Request Parameters";
    		out.println("<HTML><BODY>\n" +
    			"  <LI>param1: "
    			+ request.getParameter("param1") + "\n" +
    			"  <LI>param2: "
    			+ request.getParameter("param2") + "\n" +
    			"  <LI>param3: "
    			+ request.getParameter("param3") + "\n" +
    			"</BODY></HTML>");
    	  }
    
    	  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    		{
    		doGet(request, response);
    		}
    	}
    Servlet response to a HTTP request made from a normal class and not a browser

    The servlet below is called by a normal class by passing some request parameters. The servlet puts them in a HashMap and returns the HashMap back to the class, and the class prints the HashMap object.
    Servlet
    
    	import java.util.HashMap;
    	import java.io.*;
    
    	import javax.servlet.*;
    	import javax.servlet.http.*;
    	import javax.servlet.http.HttpServletRequest;
    	import javax.servlet.http.HttpServletResponse;
    
    	public class HelloServlet extends HttpServlet {
    
    	public void service(HttpServletRequest request, HttpServletResponse response)
    		throws ServletException, IOException {
    
    		String name = request.getParameter("name");	//getting the parameters
    		String age = request.getParameter("age");
    
    		HashMap hm = new HashMap();	//putting them in a hashmap
    		hm.put("name", name);
    		hm.put("age", age);
    
    		try {	//returning them
    			ObjectOutputStream p = new
    			ObjectOutputStream(response.getOutputStream());
    			p.writeObject(hm);
    			p.flush();
    			p.close();
    			}
    		catch (Exception e) {
    			e.printStackTrace();
    			}
    		}
    	}
    
    This is the class that calls the servlet
    
    	import java.net.*;
    	import java.util.*;
    	import java.io.*;
    
    	public class Hello {
    	public static void main(String args[]) {
    
    	ObjectInputStream is;
    	URL url;
    	String  uri = "http://Server name:port/HelloServlet";
    	HashMap hash = new HashMap();
    	try {
    		url = new URL(uri + "?&name=MyName&age=25");	//calling the servlet by passing params
    
    		is = new ObjectInputStream(url.openStream());	// open input stream and read the hashmap
    		hash = (HashMap) is.readObject();	// returned by the servlet
    
    		System.out.println(hash);		// print it out	
    		} 
    	catch (Exception e) {
    		e.printStackTrace(System.err);
    		}
    	}
    	}

    Session tracking

    Servlets allow the maintenance of session info with the help of the HttpSession class. The HttpServletRequest provides the current session with the getSession(boolean) method. If the boolean parameter is true, a new session will be created when a new session is detected. In the event the parameter is false, then the method returns null if a new session is detected.

    Servlets implicitly use cookies to track sessions but if the browser has cookies disabled, the sessionid can be encoded onto the URL using the encodeUrl() method of the HttpServletResponse class.
    	protected void doGet(HttpServletRequest req, HttpServletResponse res)
    			  throws ServletException, IOException
    	{
    	  HttpSession session = req.getSession(false);
    	  res.setContentType("text/html");
    	  res.setHeader("pragma", "no-cache");
    	  PrintWriter out = res.getWriter();
    	  out.print("<HTML><HEAD><TITLE>Session Tracker</TITLE>"+
    				"</HEAD><BODY><FORM METHOD=POST ACTION=");
    	  out.print(res.encodeUrl(req.getRequestURI()));
    	  out.print("><INPUT TYPE=SUBMIT NAME=btnSubmit VALUE="+ "\"Submit\">"+
    				"</FORM></BODY></HTML>");
    	  out.close();
    	}

    Using Cookies

    Sending cookies from a servlet

    Cookies are sent as part of a HTTPServletResponse, using the addCookie() method.
    	public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException
    	{
    		response.addCookie(new Cookie("userid", "4716"));	//cookie name,value
    	}

    Reading cookies in a servlet

    HttpServletRequest offers a method, getCookies() which returns an array of Cookie objects.
    	Cookie[] cookie_jar = request.getCookies();
    
    	if (cookie_jar != null)	//check if any cookies exist
    	{
    		for (int i =0; i< cookies.length; i++)
    		{
    			Cookie aCookie = cookie_jar[i];
    			pout.println ("Name : " + aCookie.getName());
    			pout.println ("Value: " + aCookie.getValue());
    		}
    	}

    Database access

    The below code establishes a connection to a data source and tabulates the results of a query into HTML sent to the browser
    	import java.io.*;
    	import java.sql.*;
    	import javax.servlet.*;
    	import javax.servlet.http.*;
    
    	public class DBservlet extends HttpServlet {
    		private ServletConfig config;
    
    		public void init (ServletConfig config) throws ServletException {
    			this.config = config;
    		}
    
    		public void destroy() {} // do nothing
    	  
    		public ServletConfig getServletConfig() {
    			return config;
    		}
    	  
    		public String getServletInfo() {
    			return "A Simple Servlet";
    		}
    	  
    		public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException
    		{
    		try{
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" );
    		String connStr = "jdbc:odbc:esupplies_dsn;";
    		connStr += "databasename=esupplies;SelectMethod=cursor";
    		Connection conn = DriverManager.getConnection( connStr,"Kandas","ustatesa");
    		Statement stmt = conn.createStatement();
    		String query = "SELECT ItemCode, ItemName FROM Items";
    		ResultSet rs = stmt.executeQuery(query);     //result of query is stored in ResultSet class
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.println( "<html><head>"<title>A Sample Data Access Servlet</title></head>" );
    		out.println( "<body>" );
    		out.println( "<table>" );
    		while (rs.next()) {
    				String s = rs.getString("ItemCode");      
    				String n = rs.getString("ItemName");
    				out.println("<tr><td>" +  s + "</td><td>" + n + "</td></tr>");
    			}
    		conn.close();
    			out.println( "</table></body></html>" );
    		out.close();
    		}
    		catch(Exception e)
    			{
    			response.setContentType("text/html");
    			PrintWriter out = response.getWriter();
    			out.println( "<html><head>"<title>A Sample Data Access Servlet</title></head>" );
    			out.println( "<body>" );
    			out.println( "Exception Occurred:" + e + "</body></html>" );
    			out.close();
    			}
    		}
    	}

    Back



    JAVA SERVER PAGES (JSP)

    JSP provides the ability to display dynamic content within web pages. JSP pages are implemented in Java, so they are cross platform and inherit all of the Java language strengths. JSP pages are built on top of servlets, so they are fast and can be easily changed.

    Lifecycle of JSP pages

  • The remote client (a web browser) makes a request to the JSP page.
  • The JSP engine parses the contents of the JSP file.
  • The JSP engine creates temporary servlet source code based on the contents of the JSP.
  • The servlet source code is compiled by the Java compiler into a servlet class file.
  • The servlet is instantiated. The init() and service() methods of the servlet are called, and the servlet gets executed.
  • The contents of JSP are sent back to the client (the contents are static HTML, graphics, dynamic contents etc.)


    JSP ELEMENTS

    Types of JSP tags:
  • Directives
  • Expressions
  • Declarations
  • Scriptlets
  • Comments
  • Implicit Objects
  • JSP Actions

    Common JSP directives

    page - used to specify the attributes of the page like
    	language = "scriptinglanguage"
    	import = "Package/Class"
    	buffer = "none | sizekb"
    	isThreadSafe = "true | false"
    	errorPage = "url"
    	Scope = "REQUEST/PAGE/SESSION/APPLICATION"
    
    	<%@ page language="Java" extends="" import="<class> or <package>"  %>
    include - used to include a HTML, JSP or Sevlet file into a JSP file. This is a static inclusion of file i.e. included at the time of compilation and any later changes in the included file will not be reflected.
    	<%@ include file="<filename>"  %>
    Declarations

    A declaration block contains Java variables and methods that are called from an expression block within the JSP file. Code within a declaration block is usually written in Java and is used to perform additional processing on the dynamic data generated by a JavaBean property.
    	<%!
    	private int getDateCount = 0;
    	private String getDate(GregorianCalendar gc)
    	{ ...method body  here...}
    	%>
    Comments in a JSP page

    There are two types of comments:
    Visible comments which are delivered to the browser
    	<!-- comment text -->
    Hidden comments which are not delivered and are invisible to the browser
    	<%-- comment text --%>
    Implicit/predefined objects available in JSP

    request - With this object, all the parameters passed by the clients can be read in the form of name/value pairs or as incoming HTTP headers. The name/value pairs are values sent by the GET, POST methods, whereas HTTP headers are cookies, or a servlet output. This is an instance of HttpServletRequest
    response - This is response to the client and is an instance of HttpServletResponse
    out - This is the PrintWriter instance used to send output to the client (uses JspWriter class to buffer the output)
    session - the HttpSession object which is created automatically, so this variable is bound even if there was no incoming session reference
    application - an instance of ServletContext as obtained via getServletConfig().getContext()
    config - The config variable provides access to the javax.servlet.ServletConfig object for the JSP page
    pageContext - The page context is this JSP page
    page - This is an instance of the current page implementation class and references the current instance of the JSP page. page is declared as a java.lang.Object, so it must be cast to the name of the generated servlet class name to use it. An easier alternative is to just refer to the current instance of the page with the Java this keyword.

    Methods in the request object
    	<HTML>
    	<HEAD><TITLE>Request object fields</TITLE></HEAD> 
    	<BODY> 
    	Request method:  <%= request.getMethod() %>
    	Protocol used for the request:  <%= request.getProtocol() %> 
    	Server name:  <%= request.getServerName() %> 
    	Server port: <%= request.getServerPort() %>
    	Computer address: <%= request.getRemoteAddr() %>
    	Computer name:  <%= request.getRemoteHost() %>
    	Path info portion of the URL: <%= request.getPathInfo() %>
    	Server login name: <%= request.getRemoteUser() %>
    	Full request URI:  <%= request.getRequestURI() %> 
    	Browser:  <%= request.getHeader("User-Agent") %>
    	</BODY>
    	</HTML>

    JSP ACTIONS

    With the help of JSP actions, developers can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.
    Available actions include
    jsp:include - Include a file at the time the page is requested.
    jsp:useBean - Find or instantiate a JavaBean.
    jsp:setProperty - Set the property of a JavaBean.
    jsp:getProperty - Insert the property of a JavaBean into the output.
    jsp:forward - Forward the requester to a new page.
    jsp:plugin - Generate browser specific code that makes an OBJECT or EMBED tag for the Java plugin.

    The jsp:useBean action
    The below code is a sample of how a bean can be included in a JSP page.

    	The HTML File 
    	<HTML>
    	<HEAD><TITLE>Using JavaBeans in JSP</TITLE></HEAD>
    	<BODY> 
    	<jsp:useBean id="myBean" class="MyBean" />
    	<jsp:setProperty name="myBean" property="string"  value="Welcome to JavaServer Pages" />
    	<H1>Hello!: <B> 
    	<jsp:getProperty name="myBean" property="string" />
    	</B></H1>
    	</BODY>
    	</HTML> 
    
    	The Bean File 
    	public class MyBean { 
    	  private String str = "rajtek.tripod.com"; 
    
    	  public String getString() { 
    		return(str); 
    	  } 
    
    	  public void setString(String str) { 
    		str = str; 
    	  } 
    	}
    The include action
    The include action is used to include a JSP or HTML page within another page. Unlike the include directive, this is done at request time and not at compile time, so if the included page is changed, the next request picks up the new changes.
    	<jsp:include page="sample1.html" flush="true" />     //or
    	<jsp:include page="/docs/sample2.jsp" flush="true"> </jsp:include>
    The flush="true" parameter indicates whether the output should be flushed before the included page is called. This must be set to true.

    The jsp:forward action
    It has a single attribute - page. jsp:forward terminates the execution of the current page.
    	<jsp:forward page="/dir1/sample1.jsp" />     //or
    	<%public String s1 = "sample2.jsp"%>
    	<jsp:forward page="<% =s1 %>" ></jsp:forward>
    Running applets using the jsp:plugin action
    	<jsp:plugin type=applet code="SampleApplet.class" codebase="classes/applets" >
    	  <jsp:params>
    	  <jsp:param name="sampleapplet" value="applets"/>
    	  </jsp:params>
    	  <jsp:fallback>
    	  <p> unable to start plugin </p>
    	  </jsp:fallback>
    	</jsp:plugin>

    JSP & DATABASES

    Executing SQL statments in JSP scripts
    	<%@ page contentType="text/html;charset=UTF-8" pageEncoding="iso-8859-1" %>
    	<%@ page import="java.sql.*,java.util.*,java.io.*" %> 
    	<html>
    	<head>
    	<title>JSP Database connection example</title>
    	</head>
    	<%
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    		String connStr = "jdbc:odbc:esupplies_dsn;";
    		connStr += "databasename=esupplies;SelectMethod=cursor";
    		Connection conn = DriverManager.getConnection( connStr,"Kandas","ustatesa");
    		Statement stmt = conn.createStatement();
    		String query = "Select ItemCode, ItemName FROM Items";
    		ResultSet rs = stmt.executeQuery(query);         //result of query is stored in ResultSet class
    	%>
    	<body><table>
    	<%
    		while (rs.next()) {
    				String s = rs.getString("ItemCode");       
    				String n = rs.getString("ItemName");
    	%><tr><td><%=s%></td><td><%=n%></td></tr>
    	<%
    		}
    		conn.close();%>
    	</table>
    	</body>
    	</html>
    	

    Back



    JFC and SWING

    Java Foundation Classes (JFC) encompass a group of features to help developers build GUIs. JFC contains the following features
  • The Swing Components - Include everything from buttons to split panes to tables.
  • Pluggable Look and Feel Support - Gives any program that uses Swing components a choice of looks and feels. For e.g the same program can use either the Java look and feel or the Windows look and feel
  • Accessibility API - Enables assistive technologies such as screen readers and Braille displays to get information from the UI
  • Java 2D API - Enables developers to easily incorporate high-quality 2D graphics, text, and images in applications and in applets.
  • Drag and Drop Support - Provides the ability to drag and drop between a Java application and a native application.

    Main packages available in Swing

    javax.accessibility - defines a contract between UI components and an assistive technology that provides access to those components
    javax.swing - set of 'lightweight' (all-Java language) components that, to the maximum degree possible, work the same on all platforms
    javax.swing.border - classes for drawing specialized borders around a Swing component
    javax.swing.colorchooser - classes and interfaces used by the JColorChooser component
    javax.swing.event - events fired by Swing components
    javax.swing.filechooser - used by the JFileChooser component
    javax.swing.plaf - one interface and many abstract classes that Swing uses to provide its pluggable look-and-feel capabilities.
    javax.swing.plaf.basic - UI objects built according to the Basic look-and-feel
    javax.swing.plaf.metal - UI objects built according to the 'metal' look-and-feel
    javax.swing.plaf.multi - the multiplexing look and feel allows users to combine auxiliary look and feel with the default look and feel
    javax.swing.table - dealing with java.awt.swing.JTable
    javax.swing.text - dealing with editable and noneditable text components
    javax.swing.text.html - class HTMLEditorKit and supporting classes for creating HTML text editors
    javax.swing.tree - classes and interfaces for dealing with java.awt.swing.JTree
    javax.swing.undo - support for undo/redo capabilities in an application such as a text editor

    Compiling and running Swing applications

    Swing programs can be compiled by the command
    	javac -deprecation applicationname.java
    Assuming that the program uses a standard look and feel - such as the Java or Windows look and feel, the program can be run without adding anything to the classpath. For e.g
    	java SwingApplication
    For a nonstandard look and feel, its package must be included in the class path. For e.g
    	Solaris:
    		java -classpath .:/home/me/lnfdir/newlnf.jar SwingApplication
    	
    	Windows:
    		java -classpath .;C:\java\lnfdir\newlnf.jar SwingApplication
    Advantages of Swing Components over AWT Components

  • The biggest difference is that the Swing components are implemented with absolutely no native code. Since Swing components aren't restricted to the least common denominator - the features that are present on every platform - they can have more functionality than AWT components
  • Swing buttons and labels can display images instead of, or in addition to, text
  • the borders drawn around most Swing components can be easily added or changed. For e.g it's easy to put a box around the outside of a container or label
  • the behavior or appearance of a Swing component can be easily changed by either invoking methods on it or creating a subclass of it
  • Swing components don't have to be rectangular. Buttons, for example can be round
  • Assistive technologies such as screen readers can easily get information from Swing components. For e.g a tool can easily get the text that's displayed on a button or label


    Swing Components

    Types of Swing Components

    Top-level containers - Applets, Dialogs, Frames
    General-purpose containers - Panel, Scroll pane, Split pane, Tabbed pane, Toolbar
    Special-purpose containers - Internal frame, Layered pane, Root pane
    Basic controls - Buttons, Combo box, List, Menu, Slider, Spinner, Text field or Formatted Text field
    Uneditable Information Displays - Label, Progress bar, Tool tip
    Interactive Displays of highly formatted information - Color chooser, File chooser, Table, Text, Tree

    Containers

  • Window - Provides a top-level window without a title, border or menus. It is seldom used directly (Frame and Dialog subclasses are used.)
  • Frame - The Frame is typically the starting point of a GUI application. Provides a top-level window with title, border and menus.
  • Dialog - Provides a top-level window with a title bar.
  • ScrollPane - Can contain a single component. If the component is larger than the scrollpane, it acquires vertical/horizontal scrollbars.
  • Panel - It provides an intermediate level of spatial organization and containment. It is not a top-level window and does not have title, border or menubar.
  • Applet - Provodes a specialized Panel, run inside other applications (typically browsers).

    Layout Managers

    Layout Managers are classes that handle the positioning and appearance of components within a container. Components are added to a container using the add method and a Layout Manager is associated with a container.

    The different Layout Manager classes are:
  • FlowLayout - Lays out the components in row-major order. Rows growing from left to right, top to bottom
  • GridLayout - Lays out the components in a specified rectangular grid, from left to right in each row and filling rows from top to bottom
  • BorderLayout - Up to 5 components can be placed in particular locations: north, south, east, west and center
  • CardLayout - Components are handled as a stack of indexed cards. Shows only one at a time
  • GridBagLayout - Customizable and flexible layout manager that lays out the components in a rectangular grid

    A Layout Manager is associated with a container by invoking the container's setLayout method. For e.g
    	JPanel panel1 = new JPanel();
    	panel1.setLayout(new BorderLayout());
    Swing classes for creating basic GUI components

  • JButton
  • JCheckBox
  • JColorChooser
  • JComboBox
  • JFileChooser
  • JLabel
  • JList
  • JScrollbar
  • JTextfield
  • JTextArea


    Event handling

    Java handles events through
  • Event classes - Events are Java objects. All the pertinent information is encapsulated in that object. The super class of all events is java.util.EventObject
  • Event listeners - Each listener interface extends java.util.EventListener interface. There are 11 listener interfaces corresponding to particular events. Any class that wants to handle an event should implement the corresponding interface
  • Event adapter classes - Event Adapters are convenient classes implementing the event listener interfaces. They provide empty bodies for the listener interface methods, so only the methods of interest can be implemented without providing empty implementation. They are useful when implementing low-level event listeners. There are 7 event adapter classes, one each for one low-level event listener interface.
  • Explicit event enabling - is carried out by 4 steps.
    1. Create a subclass of a component
    2. Call enableEvents(XXX_EVENT_MASK) in the constructor.
    3. Provide processXXXEvent and/or processEvent in the subclass component. To also notify the listeners, call parent method.
    4. Create an instance of the subclass component

    Types of Events

    Event classes can be classified into
  • semantic events - These classes are used to represent interaction with a GUI item (like clicking a button). They include ActionEvent, AdjustmentEvent, ItemEvent, TextEvent
  • low-level events - These classes are used to represent low-level input or window operations. Several low-level events can constitute a single semantic event. They include ComponentEvent, ContainerEvent, FocusEvent, KeyEvent, MouseEvent, PaintEvent, WindowEvent

    Sources of the different types of event classes

    	Event			ClassSourceEvent			Types
    	ActionEvent		Button - when clicked			ACTION_PERFORMED
    				List - when doubleclicked
    				MenuItem - when clicked
    				TextField - when ENTER is pressed
    	AdjustmentEvent		Scrollbar				ADJUSTMENT_VALUE_CHANGED
    	ItemEvent		Choice, List, Checkbox, CheckboxMenuItemITEM_STATE_CHANGED
    	TextEvent		TextField, TextArea			TEXT_VALUE_CHANGED
    	ComponentEvent		All Components				COMPONENT_SHOWN, COMPONENT_HIDDEN, COMPONENT_MOVED, COMPONENT_RESIZED
    	ContainerEvent		All Containers				COMPONENT_ADDED, COMPONENT_REMOVED
    	FocusEvent		All Components				FOCUS_GAINED, FOCUS_LOST
    	KeyEvent		All Components				KEYPRESSED, KEYRELEASED, KEYTYPED 
    	MouseEvent		All Components				MOUSE_PRESSED/_RELEASED/_CLICKED/_DRAGGED/_MOVED/_ENTERED/_EXITED
    	WindowEvent		All Windows				WINDOW_OPENED, WINDOW_CLOSING, WINDOW_CLOSED, WINDOW_(DE)ICONIFIED, WINDOW_(DE)ACTIVATED
    Event Listeners

    There are 11 listener interfaces corresponding to particular events.

    ActionListener AdjustmentListener ItemListener TextListener ComponentListener ContainerListener FocusListener KeyListener MouseListener MouseMotionListener WindowListener


    Programming in Swing

    Using the JButton class

    The below code creates a button and a label and updates the label with the number of button clicks.
    	import javax.swing.*;
    	import java.awt.*;
    	import java.awt.event.*;
    
    	public class SwingApplication {
    		private static String labelPrefix = "Number of button clicks: ";
    		private int numClicks = 0;
    
    		public Component createComponents() {
    			final JLabel label = new JLabel(labelPrefix + "0    ");
    
    			JButton button = new JButton("I'm a Swing button!");
    			button.setMnemonic(KeyEvent.VK_I);
    			button.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					numClicks++;
    					label.setText(labelPrefix + numClicks);
    				}
    			});
    			label.setLabelFor(button);
    
    			/* An easy way to put space between a top-level container and its contents is to put the contents in a JPanel
    				that has an "empty" border */
    			JPanel pane = new JPanel();
    			pane.setBorder(BorderFactory.createEmptyBorder(
    							30, //top
    							30, //left
    							10, //bottom
    							30) //right
    								);
    			pane.setLayout(new GridLayout(0, 1));
    			pane.add(button);
    			pane.add(label);
    
    			return pane;
    		}
    
    		public static void main(String[] args) {
    			try {
    				UIManager.setLookAndFeel(
    					UIManager.getCrossPlatformLookAndFeelClassName());
    			} catch (Exception e) { }
    
    			//Create the top-level container and add contents to it.
    			JFrame frame = new JFrame("SwingApplication");
    			SwingApplication app = new SwingApplication();
    			Component contents = app.createComponents();
    			frame.getContentPane().add(contents, BorderLayout.CENTER);
    
    			//Finish setting up the frame, and show it.
    			frame.addWindowListener(new WindowAdapter() {
    				public void windowClosing(WindowEvent e) {
    					System.exit(0);
    				}
    			});
    			frame.pack();
    			frame.setVisible(true);
    		}
    	}
    Using an image within a button

    The ImageIcon class can be used to add an image to a button.
    	import java.awt.*;
    	import java.awt.event.*;
    	import javax.swing.*;
    	 
    	public class PictureButton extends JFrame {
    	 
    	  public PictureButton() {
    		super("PictureButton v1.0");
    		setSize(200, 200);
    		setLocation(200, 200);
    		
    		Icon icon = new ImageIcon("rhino.gif");
    		JButton button = new JButton(icon);
    		button.addActionListener(new ActionListener(  ) {
    		  public void actionPerformed(ActionEvent ae) {
    			System.out.println("God!");
    		  }
    		});
    		
    		Container content = getContentPane();
    		content.setLayout(new FlowLayout());
    		content.add(button);
    	  }
    	 
    	  public static void main(String[] args) {
    		JFrame f = new PictureButton();
    		f.addWindowListener(new WindowAdapter() {
    		  public void windowClosing(WindowEvent we) { System.exit(0); }
    		});
    		f.setVisible(true);
    	  }
    	}
    List and Combo boxes

    The below code creates a Combobox and a Listbox.
    	import java.awt.*; 
    	import java.awt.event.*;
    	import javax.swing.*;
    	 
    	public class combobox1 {
    	  public static void main(String[] args) {
    		JFrame f = new JFrame("List and Combo Box Sample v1.0");
    		f.setSize(200, 200);
    		f.setLocation(200, 200);
    		f.addWindowListener(new WindowAdapter() {
    		  public void windowClosing(WindowEvent we) { System.exit(0); }
    		});
    		
    		// create a combo box
    		String [] items = { "un", "deux", "trois", "quatre", "cinq", "six" };
    		JComboBox comboBox = new JComboBox(items);
    		comboBox.setEditable(true);
    	 
    		// create a list with the same data model
    		final JList list = new JList(comboBox.getModel());
    		
    		// create a button; when it's pressed, print out the selection in the list
    		JButton button = new JButton("Pour favor");
    		button.addActionListener(new ActionListener() {
    		  public void actionPerformed(ActionEvent ae) {
    			Object[] selection = list.getSelectedValues();
    			System.out.println("-----");
    			for (int i = 0; i < selection.length; i++)
    			  System.out.println(selection[i]);
    		  }
    		});
    		
    		Container c = f.getContentPane(  );		// put the controls in the content pane
    		JPanel comboPanel = new JPanel(  );
    		comboPanel.add(comboBox);
    		c.add(comboPanel, BorderLayout.NORTH);
    		c.add(new JScrollPane(list), BorderLayout.CENTER);
    		c.add(button, BorderLayout.SOUTH);
    	 
    		f.setVisible(true);
    	  }    
    	}

    Back

    Maintained by: VINCENT KANDASAMY, Database Architect/Administrator (kandasf@hotmail.com)
    Last updated: Nov 25, 13:00