You don't write programs in servlets. Servlets may be components in a web application though.
What you perhaps want is a good book. I suggest you give http://pdf.coreservlets.com a read, not only because it is free but also because it is very good. Whatever you do, you are not going to learn this by asking questions in a forum.
If you're asking how to make your servlet threadsafe, then:
1) Making sure the Servlet itself has no member variables is one step. That will make that class threadsafe.
2) Of course, your servlet might still me unsafely using other classes. For that, you make it threadsafe just like you do any other code.