1) What are Servlets?
Ans :Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
2) What are the types of servlet?
Ans : The Types of a servlet are :
- Generic Servlet.
- HTTP Servlet.
3) How to create servlet?
Ans : Servlets can be created using :
- javax.servlet.http package
4) What is the life-cycle of a servlet?
Ans : The life-cycle of a servlet is :
- Servlet class is loaded.
- Servlet instance is created.
- Init method is invoked.
- Service method is invoked.
- Destroy method is invoked.
5) What are the life-cycle methods for a servlet?
Ans :The life-cycle methods of a servlet are :
- public void init().
- public void service().
- public void destroy().
6) When init() method of servlet gets called?
Ans :The init method is designed to be called only once. It is called when the servlet is first created, and It is used to initialize the servlet.
7) When service() method of servlet gets called?
Ans :It is invoked at each request.The service() method is used to service the request.
8) When destroy() method of servlet gets called?
Ans : It is invoked only once when servlet is unloaded. This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.
9) What is the difference between GET and POST method?
Ans :The difference between GET and POST method are :
|GET Method||POST Method|
|1. Limited amount of data(2k) can be sent||1. Large amount of data can be sent|
|2. Data is sent in header||2. Data is sent in body|
|3. Can be bookmarked||3. Cannot be bookmarked|
|4. Parameters are not encrypted||4. Parameters are encrypted|
|5. Faster||5. slower|
10) What are the advantages of servlets?
Ans :The advantages of servlets are :
- the web application developed with Servlet can be run on any standard web container such as Tomcat, JBoss, Glassfish servers and on operating systems such as Windows, Linux, Unix, Solaris, Mac etc.
- Servlets are robust because container takes care of life cycle of servlet and we don’t need ` to worry about memory leaks, security, garbage collection etc.
- Servlets are maintainable and learning curve is small because all we need to take care is business logic for our application.