I want Complete code for handling user session.

what i hava to do when user is logging,and when user is logout.
i required servlet code.
please explain me in details if possible.