Saturday, November 30, 2013

Synchronization Concept

What is synchronization ?
-----------------------------

I will explain with bad example....
But easy to understand

I will compare... java object having several methods
as
House with various rooms(Bathroom is one of the room)

Generally.. if one person enters bathroom no other person
will enter right ?

The other person waits... until the person in the bathroom
comes out.

I will compare persons in the house with threads..

Bathroom and synchronized methods behave same...

When one person entered bathroom,no other person in
the house can enter bathroom right ?
(synchronized methods)

But still the person waiting for bathroom can enter
other rooms like dining room,kitchen room etc..
(non synchronized methods)

Now if we compare with threads

When one thread enters synchronized method no other
thread can enter any synchronized method...(there may
be multiple methods with keyword synchronized right)
So,if one thread enters synchronized no other thread
should enter any other synchronized method..

But... the other threads can still... enter non synchronized
methods....

Finally...

If one thread enters synchronized method in object A ,other
threads cannot enter.. other synchronized methods in object A
but..... synchronized method in object B can be accessed
as it is completely different object(room in another house)

Very very imp:
-----------------
If there are 3 synchronized methods...
You have to compare

3 synchronized methods(A,B,C) collectively as a bathroom
2 non synchronized methods E,F.
What I want to say is

When one person enters bathroom.. no one can enter
right

Similarly

When thread A enters any one synchronized method out of 3 say A,
Thread B can't enter A or B or C.

But Thread B can still enter E,F...

If you don't understand this... you will implement coding
badly.........

But Thread A can enter all the methods....synchronzied (A,B,C) and non synchronized (E,F)

Thread concept looks simple..... But in realtime... it will be tough
to identify.. where there can be potential.. thread related issues....
unless u r really really good in this theory....

Friday, November 15, 2013

Sample Struts Login Application Flow

I don't go right away in to minute details
I will explain in my own way.


1)In login.jsp There are 2 text fields
    a)uname
    b)pword

2)Generally you submit to a page or servlet... Right...?
   But if you observe action value in form tag there is some thing ending with .do
   ./login.do

3)So here comes the question... To which page the login.jsp need to be submitted

4)In struts... here comes the use of a configuration file called struts-config.xml.
    It should be in WEB-INF folder of your webapplication.

5)So how is struts-config.xml going to help me to find out the page to which login.jsp
   need to be submitted.

6)When submit button is executed...,the url in the browser would be
   webapplicationname/login.do

7)Whenever request url ends with .do,There is a servlet called ActionServlet which
   takes the path after webapplicationname i.e login.do removes .do and checks
   in struts-config.xml,if there is any action element with path as /login.

8)So in our example,it will find the following action element because its path = /login
   <action path="/login" name="loginform" type="org.jlearners.actions.LoginAction" scope="session">
           <forward name="success" path="/success.jsp"/> 
           <forward name="failure" path="/failure.jsp"/>
       
        </action>
  

9)So, an object of LoginAction is created and method called execute gets automatically called.

10)Action class is nothing but any javaclass which extends org.apache.struts.action.Action.

11)In our example LoginAction extends org.apache.struts.action.Action, we have to override
     the extends method.

12)Generally when a page is submitted to servlet or jsp, we will get the values submitted using
     request.getParameter right ?

     In Struts... if You create a class which extends ActionForm and having properties which
     exactly match the html input elements in login.jsp,an instance of your class will be instantiated
     and struts autopopulates the properties with the values from the login.jsp

13)You have to specify... the class which extends ActionForm in struts-config.xml in
     <form-beans> section.

     <form-beans>
        <form-bean name="loginform" type="org.jlearners.forms.LoginForm"/>
    </form-beans>

     <action-mappings>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
        <action path="/login" name="loginform" type="org.jlearners.actions.LoginAction" scope="session">
           <forward name="success" path="/success.jsp"/> 
           <forward name="failure" path="/failure.jsp"/>
       
        </action>
    </action-mappings>

14)Before the execute method in LoginAction gets executed,the value of name attribute is picked up,
     in this case it is loginform right... so, it will go to <form-beans> section and see the form-bean
    with name loginform.. and instantiates the class specified in type attributes value,So automatically
    LoginForm instance is created and populated with the values submitted from login.jsp

15)That populated instance will be passed as one argument to execute method.


16)So.. you are in execute method... and you can get submitted values by calling
     logForm.getUname() or ,logForm.getPword()

17)You call the method in LoginDAO with the values submitted from login.jsp and
      if the user exists you will say mapping.findforward("success").What is success ?
     It is not jsp.. so to which page it should be forwarded

18)So, struts checks if there is any forward element declared for that action(LoginAction).

19)<action path="/login" name="loginform" type="org.jlearners.actions.LoginAction" scope="session">
           <forward name="success" path="/success.jsp"/> 
           <forward name="failure" path="/failure.jsp"/>
       
        </action>

20)So if you say mapping.findForward("success") we will be forwareded to success.jsp

21)If you say mapping.findForward("failure") it will check the forward element in LoginAction
   with name failure... and forwards to the page specified as path attribute value i.e failure.jsp


So if I summarize the above steps

1)When  a page is submitted it truncates .do from login.do
2)Checks if there is any action with path value as /login
3)Finds the formbean in to which the submitted values will be loaded with the help of
    name attribute in action element
4)Populates formbean and execute in actionclass(LoginAction) gets executed.. and
   you have to write business logic here and forward to the page which displays UI.


Hope you guys understood..... :