Wednesday, February 4, 2009

Setting Session Timeout (J2EE)

There are two ways of setting session timeout in Java. One is by utilizing the deployment descriptor and the other is by calling setMaxInactiveInterval() under HttpSession interface.

Using Deployment Descriptor:



Setting time-out at the deployment descriptor gives impact to the whole web-application. Looking at the example above, the session for the whole web application will terminate in 5 minutes (note: In deployment descriptor the time used for the timeout is measured in minutes)


Using setMaxInactiveInternal() :

HttpSession session = request.getSession() ;
session.setMaxInactiveInterval( 5 * 60) ;

The difference between using setMaxInactiveInterval() in a program and using in deployment descriptor are:

  • setMaxInactiveInterval() method will only cause session time-out for a particular session instance and not the whole web-application.
  • Other than that in setMaxInactiveInterval() method, time is measured in seconds not minutes. Take a look at the code above, to reach 5 minutes expiry (5*60) is used.

Author: Thiagu

Thursday, January 15, 2009

Interfaces in JAVA (Java Fundamentals)

An interface is similar to Abstract classes but the only difference here is an interface cannot have any code implementation within it's body. Interfaces serves as a prototype for the whole project, so that all the clasess implementing an interface would override the methods in it.

Here are the features and rules of an Interface:


  1. Interface can only be declared with public or default access modifiers
  2. Any method declared within an Interface is "public abstract"
  3. Any method declared within an interface should not have code implementations
  4. Any variables declared within an Interface is "public static final", in other words all of them will be constant values.
  5. A sub-class that does not override an Interface method/s should be declared abstract.
  6. An Interface can implement several interfaces.
  7. "extends" keyword is used for an interface to implement another interface

Let's take an example to figure out how actually an interface works. Let's expand the previous example "The Car Scenario". Now what are the common features of a Vehicle? Every modern Vehicle will have it's own engine, color and maximum speed (kmh). This feature will vary in accordance to the type of Vehicle. An economic car could have 4 seats, metalic or non-metalic color, and etc. Have a look at this code to see how actually interfaces are implemented for the Economic car.


interface Vehicle {
int engine() ;
int speed() ;
}

interface Color
{
String colorType() ;
boolean isMetalic() ;
}

interface Car extends Vehicle, Color
{
int numSeats() ;
}

public class EconomicCar implements Car
{
public int engine()
{
int cylinder = 6 ;
return cylinder ;
}

public int speed()
{
int maxSpeed = 200 ;
return maxSpeed ;
}

public String colorType()
{
String color = "black" ;
return color ;
}

public boolean isMetalic()
{
boolean metalic = true ;
return metalic ;
}

public int numSeats()
{
int seats = 4 ;
return seats ;
}
}


Every car will have it's own engine, color and max speed so, the Car interface implements both Color and Vehicle interface. The Car interface has it's own abstract method called numSeats beacuse number of seats vary for different type of cars. The sub-class of the whole hirearchy is EconomicCar, and this sub-class has to override all the abstract methods of it's super-classes.





Author: Thiagu

Sunday, January 11, 2009

Abstract Classes and Abstract Methods (Java Fundamentals)

You have to know two important concepts for this part. One is Abstract classes and the other is Interface. Depends on the nature of the project, you need to set certain level of standards and rules that the other developers must follow. This is where Abstract classes and Interfaces come into picture. Abstract classes are usually used on small and big time projects, where it can have code implementation like general classes and also declare Abstract methods (empty methods that require implementation from the sub-classes). Abstract methods usually serves as a prototype to developers.

Lets take an example of a Car, every car has seats but the number of seats vary based on the type of car. A sports car will have 2 seats and an economic family car has 4 seats. At this point the common feature for every car are seats, so we need to create an Abstract class called Car and an abstract method called seat. Than we can create 2 sub-classes called SportsCar and EconomicCar that will override the seat method of Car class.


public abstract Car{
public abstract int seat() ;
}

public class SportsCar extends Car{
public int seat()
{
int seat = 2 ;
return seat ;
}
}

public class EconomicCar extends Car{
public int seat()
{
int seat = 4 ;
return seat ;
}
}


Here are the rules of an Abstract class and method:

  1. Abstract classes cannot be instantiated
  2. Abstract class can extend an abstract class or a concrete class and implement several interface classes
  3. Abstract class cannot extend an interface
  4. If a class contains Abstract method, the class has to be declared Abstract class
  5. An Abstract class may or may not contain an Abstract method
  6. Abstract method should not have any code implementations, the sub-classes must override it (sub-class must give the code implementations).
  7. If a sub-class of an Abstract class does not override the Abstract methods of its super-class, than the sub-class should be declared Abstract also.
  8. Abstract method cannot be declared with any other non-access modifiers such as static, final, native, transient, volatile
  9. Abstract classes can only be declared with public and default access modifiers.
  10. Abstract methods can only be declared with public, default and protected modifier.



Author: Thiagu

Java Modifiers (Java Fundamentals)

There are two types of modifiers in Java one is Access modifiers and the other is Non-access modifiers.

Examples of Access Modifiers are:


  1. private
  2. public
  3. protected
  4. Default (Package)

Examples of non-access modifiers:


  1. static
  2. final
  3. abstract
  4. synchronized
  5. native
  6. transient
  7. volatile
Some developers or lecturers do term access modifiers as access specifiers but the official name used on books and SUN Microsystem is "Access Modifiers"


Author: Thiagu