Thursday, December 1, 2011

What is serialization ?

Answer : Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.  

What is "System", "out", "println" in System.out.println ? Explain !

System.out.println()

System is a built-in class present in java.lang package.
This class has a final modifier, which means that, it cannot be inherited by other classes.
It contains pre-defined methods and fields, which provides facilities like standard input, output, etc.

out is a static final field (ie, variable)in System class which is of the type PrintStream (a built-in class, contains methods to print the different data values).
static fields and methods must be accessed by using the class name, so ( System.out ).

out here denotes the reference variable of the type PrintStream class.

println() is a public method in PrintStream class to print the data values.
Hence to access a method in PrintStream class, we use out.println() (as non static methods and fields can only be accessed by using the refrence varialble)

System.out.println();

eg:

int i = 3;
System.out.println(i);


the above code prints the value of 3 in the screen and brings the control to the next line

Different ways to create objects in java

1. Using new keyword
This is the most common way to create an object in java. 

MyObject object = new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
 
MyObject object = (MyObject) Class.forName("ravi.rnd.MyObject").newInstance();

 3. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
 
5) object using classloader

this.getClass().getClassLoader().loadClass(“com.raj.myobject”).newInstance();
 

Struts Flow

1) When application server gets started,container loads the
web.xml

2) When first the request is made from a JSP/HTML/XSLT to
the server with a particular URI(/something.do) the control
first reaches Web.xml file.

3) It checks the mapping for /something.do in web.xml and
finds the ActionServlet and loads ActionServlet. 
 *************
action
        org.apache.struts.action.ActionServlet   


        action
        *.do
    
**********  
4) As part of loading ActionServlet calls the init() as
every other servlet does.
(Note: ActionServlet extends HttpServlet only) 
 
******************** 

            config            /WEB-INF/struts-config.xml         
**************
 
5) In init() it takes the init-parameters as
struts-config.xml and loads the xml file.

6) Then the ActionServlet calls the process() method of
RequestProcessor class.

7) process() method checks for the appropriate action in the
action mapping for current request and maps the URI to the
action in the struts-config.xml.

8) Then it creates an object to ActionForm associated to
that action.

9) It then calls the setters and reset() methods in the form
bean(Which extends ActionForm) corresponds to the mapped action.

10) If the action tag in struts-config.xml contains validate
"true" then the validate method is called.

11) if any validation errors it return to view component
(any jsp XSLT) which is specified as an attribute of input 
"/error.jsp"

12) if there are no validation errors ,it then search for
execute method in the action class and executes it.

13) execute method performs the bussinesslogic which you
provide and returns with an ActionForward key.

14) Now the returned key is searched in the mapped action
which is specified as forward tag.

15) It looks for the appropriate view component and performs
the getters on that action form corresponding to this view
component and loads the view page in the browser.