I have a requirement, I need to build a servlet page which take input of key in encoded format containing user id, access and page id. when two users enters the page second user should get a popup that another user is already in the page (I have stored the user already in a page in DB and when second user goes to the page I am able to retrieve from DB and show the message and make the page as read)
I am stuck at fulfilling below request
second user can request access upon which first user should get message to approve it. If the first user agrees then second user should get a message back to continue work and first user will get have the access as read and second should have access of write.
It's easy to use the DB as a place to store state, but immediate notification and handshaking is more difficult. You'll either need to poll from the pages, or limit your audience to modern browsers and use constant-connection technology. Either way, it's not an easy thing to set up.
Servlets are not "programs". They are not continuously-running tasks. They are, in effect, "subroutines" that get called when a web URL request comes in and they are expected to process and exit as quickly as possible. The only time that 2 users would simultaneously be executing a servlet's code is if they had made simultaneous web requests, in which case they would each be running under separate temporarily-assigned threads.
What you are describing is a lot more complex than you realize - it's a workflow (business process) that requires the actions of 2 separate agents.
Such things are hardly unknown, but it's not something you get from some simple hand-waving "All You Have To Do Is..." process or brute-force stomping around in servlet code. At a bare minimum, you're probably going to need some sort of transaction management on your database, and there are entire frameworks and standards custom-designed for this kind of stuff when really serious workloads are required.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, that will fit in a stocking