Showing posts with label to. Show all posts
Showing posts with label to. Show all posts

Wednesday, June 18, 2008

Explanation on Struts flow.


Note before reading this article : this is not for professionals and it is for the people who stepped into Struts(Beginners) and it is for my future reference.



1. When the jsp page is loaded into server, the "" (what you wrote in corresponding jsp page) creates the neccessary html code to represent the form and then checks for the instance of the corresponding ActionForm in the current session scope.If there was ActionForm object available in the session scope then the data members of the ActionForm is mapped with the input form fields respectively. and then that jsp page is presented to the user.

Let me explain with one example.

JSP pages: login.jsp, success.jsp
Action class: LoginAction
Form name: LoginForm

in login.jsp
two fields username and password
login.jsp

2.When you click on Login button the server receives that request and it wil check in web.xml for the form action with login.do
In web.xml there you can see the following url mapping with .do extension. then it calls the ActionServlet
From there the ActionServlet will take the request processing.





3.the ActionServlet will then looks for the LoginForm and populates the data members with form fields and then the LoginForm will be associated with the session.
4.Once, the request comes to the ActionServlet the ActionServlet [ActionServlet will call the RequestProcessor's process()method.
The RequestProcessor's process() method] wil read the struts-config.xml file and looks for the following entry:




if the path attribute equals to the form action attribute i.e login
5. it then creates the LoginAction object and calls the execute() method on that object by passing the ActionMapping[creates ActionMapping instance and passed to execute()], ActionForm,HttpServletRequest,HttpServletResponse objects.
the execute() method will process the business logic [by calling the model component] and then calls the ActionMapping.findForward("string argument") can be success or failure.
6.this ActionMapping.findForward("string argument") will looks for the subelement of the tag with the name attribute equal to the "string argument" . It then returns the ActionForward object which contains results of the
lookup, which is the value of the path attribute /welcome.jsp (upon success) or /login.jsp (upon failure).

If it is success then the LoginAction returns the ActionForward object to the ActionServlet, which will then forward to the view for presentation.
here now you can see welcome.jsp

uhhhhhh......, I did it. please give me feedback.

resources i have used to write this
Mastering Jakarta Struts book by James Goodwill[It is a good book for struts]
and my class notes.


Monday, June 16, 2008

how to clone an object in Java? what is shallow copying and deep copying?

how to clone an object in Java?

Cloning is a way of creating copies of objects in java.

protected Object clone() throws CloneNotSupportedException

the above method creates and returns a copy of the this object.
i.e classes that implement the Cloneable interface should override Object.clone (which is protected) with a public method.

Note: The Cloneable interface doesnot contain the clone method. It is not possible to clone an object by implementing the clone() method.
Note1: invoking the Object.clone() method on instance that doesnot implementing the Cloneable interface will result in Exception " CloneNotSupportedException"

There are two ways of making copy of objects using Cloning.
1. Shallow copying
2. Deep copying

1.Shallow copying : Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same subobjects.
2.Deep copying : Deep copy is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made. A deep copy generates a copy not only of the primitive values of the original object, but copies of all subobjects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.
Java supports shallow and deep copy with the Cloneable interface to create copies of objects. To make a clone of a Java object, you declare that an object implements Cloneable, and then provide an override of the clone method of the standard Java Object base class. Implementing Cloneable tells the java compiler that your object is Cloneable. The cloning is actually done by the clone method.

example to shallow and deep copying see here http://forum.java.sun.com/thread.jspa?threadID=487186&messageID=2281852

example program

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;

public class CloningExample implements Cloneable {

private LinkedList names = new LinkedList();
public CloningExample() {
names.add(”Alex”);
names.add(”Melody”);
names.add(”Jeff”);
}
public String toString() {
StringBuffer sb = new StringBuffer();
Iterator i = names.iterator();
while (i.hasNext()) {
sb.append(”\n\t” + i.next());
}
return sb.toString();
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(”This should not occur since we implement Cloneable”);
}
}
public Object deepClone() {
try {
CloningExample copy = (CloningExample)super.clone();
copy.names = (LinkedList)names.clone();
return copy;
} catch (CloneNotSupportedException e) {
throw new Error(”This should not occur since we implement Cloneable”);
}
}

public boolean equals(Object obj) {

/* is obj reference this object being compared */
if (obj == this) {
return true;
}

/* is obj reference null */
if (obj == null) {
return false;
}

/* Make sure references are of same type */
if (!(this.getClass() == obj.getClass())) {
return false;
} else {
CloningExample tmp = (CloningExample)obj;
if (this.names == tmp.names) {
return true;
} else {
return false;
}
}

}


public static void main(String[] args) {

CloningExample ce1 = new CloningExample();
System.out.println(”\nCloningExample[1]\n” +
“—————–” + ce1);

CloningExample ce2 = (CloningExample)ce1.clone();
System.out.println(”\nCloningExample[2]\n” +
“—————–” + ce2);

System.out.println(”\nCompare Shallow Copy\n” +
“——————–\n” +
“ ce1 == ce2 : ” + (ce1 == ce2) + “\n” +
“ ce1.equals(ce2) : ” + ce1.equals(ce2));

CloningExample ce3 = (CloningExample)ce1.deepClone();
System.out.println(”\nCompare Deep Copy\n” +
“——————–\n” +
“ ce1 == ce3 : ” + (ce1 == ce3) + “\n” +
“ ce1.equals(ce3) : ” + ce1.equals(ce3));

System.out.println();

}

}


** The above program is copied from http://www.idevelopment.info/data/Programming/java/object_oriented_techniques/CloningExample.java this location . please follow
this link to get better explanation.
how to find the 2nd, 3rd ,4th ... nth highest salary of the employee from emp table?

select sal from emp e1 where (n-1)=(select count(DISTINCT sal)
from emp where sal > e1.sal )