Posts

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...

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; public abstract class OrderTemplate {                     public   final void Orderprocess(){                    System. out .println( "calling order process -template method pattern" );        ...

custom exceptions in java

Exceptions: 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 Examp...

Jar command

Jar is a java archive file.Its like a zipped file. All the java files can be archived into a jar file using the jar command. jar - cf  MyJar.jar Myapp.--> archives Myapp into a jar file named MyJar.jar To list the files in the Jar file. jar-tf MyJar.jar

Relative Paths and Absolute Path in Java

Relative paths is the one the do not start with a  forward slash(/) or a backward slash(\).Example for Relative path:  -classpath dirB: dirB/dirC.  The relative path is meaningfull only when the user current directory is directory A where B is its sub directory. Absolute paths start with a slash. Indicates the root directory.

Java Class Path

Java class path can be thought as class search paths.Classes are searched from left to right. Example of classpath:  -classpath  /com/foo Classpaths can be declared in two places:    as an environment variable in the operating system    as a command line option The classpath declared as an environment variable is a default option.The classpath declared as an systems environment variable is used by default whenever java or javac is invoked. The classpath declared as an command line option is temporary and exists only for the length of invocation.

Java Reflection Examples

Java Reflection is used the extensively in following areas: JUNIT JAR-Junit 3.0 uses reflection to find methods that start with" test".The algorithm was changed in 4.0.Now reflections are used to find methods with @ test annotations. Java Editors and IDE: Most of the java editors like eclipse,Netbeans and others use reflection to populate the methods associated with the class.For example in most of the java editors when you type "classname." and press space or tab you get all the methods associated with it.The method suggestion is shown using java refelection. Java Ant Build Files: A user can use taskdef property to instantiate and compile a user defined task. Spring framework:Spring uses reflection to create an Object of each Bean.The spring xml file has the classname attribute which is read to create bean objects.All the Bean Objects will be created using a default constructor and the proprties of the objects are examined. Runtime code generation:This is us...