Wednesday, June 13, 2007

Passing parameters between servlets and jsp

There are four methods to pass parameters between servlets,.

First method is using session object depending on the application environment, for example

HttpSession ses = req.getSession(true);
ses.putValue("key","value");

you can get back this data in the second servlet by :

HttpSession ses = req.getSession(true);
String val1 = (String)ses.getValue("key");

Second method is using the,
request.getParameter ("key"); and
request.setParameter("key","value");
methods. These methods are used to retrieve parameters from forms or through querystring.

Third method is using the,
request.getAttribute("key"); and
request.setAttribute("key","value");
methods. These methods are used with RequestDispatcher class's object.

Ex. <% @ page import="java.util.*" %>
<%
request.setAttribute("username","mahesh");
RequestDispatcher dispatcher = request.getDispatcher("abc.jsp");
if(dispatcher != null) {
dispatcher.forward(request,response);
}
%>

abc.jsp
<% out.println(request.getAttribute("username"));
%>

Fourth method is using Java Beans,
getXXX() and
setXXX() methods.

Saturday, June 9, 2007

Regular Expression- Pattern Matching in java

Java provides a package called 'java.util.regex' for pattern matching.

The java.util.regex package has two classes and one exception class, the two classes namely - Pattern and Matcher.

A small example code given below validates the string with the given regular expression.

 import java.util.regex.Matcher;  
import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\d*"); //reg exp for 0 or more integers
Matcher matcher = null;
matcher = pattern.matcher("1111");

if (matcher.matches()) {
System.out.println("Matched");
} else {
System.out.println("Not Matched");
}
}
}

Similarly, there are more regular expression format and quantifiers.

Using Reflections in Java

Reflection is a powerful approach to analyze the class at runtime. If new classes are added into your application dynamically then Reflection is used to get the structure of the class.

Reflection uses special kind of java class: Class. The object of the Class type can hold all the information of the class and also have its own methods to retrieve all the information.

This example code extracts the structure of the String class. It will display the name of the constructors, declared fields and methods to the console.

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectionExample {

public static void main(String[] args) {
try {
Class cl = Class.forName("java.lang.String");
Constructor cnst[] = cl.getConstructors();
Field fld[] = cl.getDeclaredFields();
Method mtd[] = cl.getMethods();
System.out.println("Name of the Constructors of the String class");
for (int i = 0; i < cnst.length ; i++) {
System.out.println(cnst[i].getName());
}
System.out.println("Name of the Declared fields");
for (int i = 0; i < fld.length; i++) {
System.out.println(fld[i].getName());
}
System.out.println("Name of the Methods");
for (int i = 0; i < mtd.length; i++) {
System.out.println(mtd[i].getName());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

To find the structure details of user defined classes, we need not use the Class.forName() method. Instead simply give the class name itself. And don't forget to import the package of that class.

Example:

Class cl = Userdefinedclass.class;

The Class.forName() method searches for the class given as argument, in the Java Class Library.


Passing unspecified number of arguments to a method in Java

Java provides a feature to pass unspecified number of argument to a method. It treats the variable-length argument list as an array. This is represented by argument type followed by three dots in the declaration of the method. This facility is included from JDK 5.0.
An example code is given below..

public int add(char a,int... num ) //variable length argument
{
int no=num.length;
int sum=0;
for(int i=0;i!=no;i++)
sum+=num[i];
sum+=a;
return sum;
}

public static void main(String[] args) {
Main m=new Main();
System.out.println(m.add('A',10,20));
System.out.println(m.add('A',10,20,30));
}

Dynamic Method Dispatch in Java

The sub classes object can be assigned to a super classes reference. This reference can in turn access the methods of the sub class. But the sub class reference cannot hold the super class's object. This is known as Dynamic Method Dispatching in Java.
A small example code is given below...

 class A {  
A() {
System.out.println("Constructor of Class A");
}
void show() {
System.out.println("I m in Class A");
}
}

class B extends A {
B() {
System.out.println("Constructor of Class B");
}
void show() {
System.out.println("I m in Class B");
}
}

public class DynamicMethodDispatch {
public static void main(String a[]) {
A aobj;
aobj = new A();
aobj.show();
aobj = new B();
aobj.show();
}
}


Output:

Constructor of Class A
I m in Class A
Constructor of Class A
Constructor of Class B
I m in Class B

Anonymous Inner Classes in Java

Anonymous Inner Classes are mainly used for using the method of a class without creating class or class objects. These classes don't have any name also. These classes are mostly used in event handling. A small code is given below to show how the anonymous classes are used.

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

public class WindowAdapterTest {
public static void main(String args[]) {
JFrame frame = new JFrame("Window Listener");
WindowListener listener = new WindowAdapter() { // anonymous class starts here
public void windowClosing(WindowEvent w) {
System.exit(0);
}
}; //anonymous class ends here
frame.addWindowListener(listener);
frame.setSize(300, 300);
frame.show();
}
}