Servlets Interview Questions and Answers
Related differences
Ques 1. What is the difference between an application server and a Web server?
Ques 2. What are advantages of servlets over CGI?
In CGI for every request there is a new process started which is quiet an overhead. In servlets JVM stays running and handles each request using a light weight thread. In CGI if there are 5000 request then 5000 CGI program is loaded in memory while in servlets there are 5000 thread and only one copy of the servlet class.
Ques 3. What are the two important APIs in Servlets?
Two important packages are required to build servlet "javax.servlet" and "javax.servlet.http". They form the core of Servlet API. Servlets are not part of core Java but are standard extensions provided by Tomcat.
Ques 4. Can you explain in detail "javax.servlet" package?
Interfaces in javax.servlet :-
Servlet Interface: This interface has the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. Following are the method in Servlet interface :-
String getServletName():- Returns the name of the invoking servlet.
Ques 5. What's the use of ServletContext?
ServletContext Interface: It gives information about the environment. It represents a Servlet's view of the Web Application.Using this interface servlet can access raw input streams to Web Application resources, virtual directory translation, a common mechanism for logging information, and an application scope for binding objects.
Most helpful rated by users:
- What is Servlet?
- Why is Servlet so popular?
- What is servlet container?
- When a client request is sent to the servlet container, how does the container choose which servlet to invoke?
- If a servlet is not properly initialized, what exception may be thrown?