Showing posts from January, 2012

Session Management in Struts2 using Session Aware Interface

Session Management in Struts2 using Session Aware Interface

Session Management in Struts2 can be done in many ways.

One of the ways is to implement the Session Aware Interface.

Let us look at a sample login application that uses the Session Aware Interface.

Step-1: Create a LoginAction class that implements a SessionAware Interface.

Session Aware interface is available as a part of – interceptor package org.apache.struts2.interceptor.SessionAware
Implement the setSession() method which is an abstract method of SessionAware Interface

package com.anjana.struts2;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;
import java.util.Map;
public class Login extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
private String userName;

private String password;
private String variableValue = "ffddf";
private Map sessionObj;
public Login() {

public String execute() {
sessionObj.put("userName", g…

Template method design pattern

Template Method Design Pattern: provides a template method for performing certain common operations. The template method design pattern can be implemented using an abstract class. The template method in the abstract class should be a concrete method which has the common process and implementation logic. The template method in the abstract class should have calls to the abstract methods.The template method in the abstract class must be marked final . Example:1 package com.anjana.templatepattern; import java.util.HashMap; import java.util.Date; publicabstractclass OrderTemplate { publicfinalvoid Orderprocess(){                    System.out.println("calling order process -template method pattern"); int customerID= getCustomerId();                    HashMap productmap=getproductIds();                    Date dateofOrder=getdateofOrder();                   // for(int i=0;i //calculate the total amount of the order.                   // }                    } protectedabstractint  getCustomerId(…

custom exceptions in java

Three types of exceptions in java
·     Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
·     Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compliation.
·     Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.
Custom exceptions Example:
package com.anjana.customexceptions;

publicclass MyException extends Exce…