PCMC: 9960935965, KOTHRUD: 9960935600

Servlets Interview Questions

Q1. What is Servlet? 

ANS: A servlet is a Java technology and it is managed by a container called a servlet engine. It generates dynamic content and interacts with clients through Request and Response. 

 

Q2. Why servlet is mostly used? 

ANS: Servlets are mostly used because they are platform-independent Java classes and are compiled to platform-neutral byte code. Java byte code can be loaded dynamically into and run by a Java Java-enabled web server. 

 

Q3. What is called servlet container? 

ANS: A servlet container is a part of a Web server that provides network services depending on request and response are sent. It contains and manages servlets through their life cycle. 

 

Q4. What is called a session? 

ANS: A session is an object which is used by a servlet and it is used to track user interaction with a web application across multiple HTTP requests. 

 

Q5. What is servlet mapping? 

ANS: Servlet Mapping is an association mapping between a servlet and a URL pattern. This is used to map servlets with the requests. 

 

Q6. What is the life cycle of Servlet? 

ANS: Following is the life cycle of Servlet: 

Loaded 

Initialized 

Destroy 

Unloaded 

 

Q7. What is the difference between Servlet Request and Servlet Context when calling a Request Dispatcher? 

ANS: Relative URL can be called when Servlet Request is used and Relative URL is not used when using Servlet Context. 

 

Q8. What are the supporting protocols by HttpServlet? 

ANS: HttpServlet supports only HTTP and HTTPS protocol 

 

Q9. What are the types of Session Tracking? 

ANS: There are the following types of session tracking: 

1. URL rewriting 

2. Hidden Form Fields 

3.4Cookies 

.Secure Socket Layer (SSL) 

 

Q10. What are the advantages of cookies? 

ANS: Cookies are used to store long-term information that can be maintained without server interaction. Small and Medium size data are kept in a queue. 

 

Q11. What is servlet lazy loading? 

ANS: A servlet container that does not initialize at the start-up, this is known as servlet lazy loading. 

 

Q12. What is Servlet Chaining? 

ANS: Chaining is one of the methods where out of one servlet is given to the second servlet. This chaining can happen for any number of servlets. 

 

Q13. What are the functions of a Servlet container? 

ANS: The following are the functions of the Servlet container: 

1. Lifecycle management 

2.3Communication support 

3. Multithreading support 

4. Declarative security 

5. JSP support 

 

Browse our course links: Full Stack Training in Pune

To Join our FREE DEMO Session: Click Here

 

Q14. What is the difference between JSP and Servlets? 

ANS: JSP supports HTTP protocol which is mainly used for presentation. But a servlet can support any protocol like HTTP, FTP, SMTP etc. 

 

Q15. What is called Scriptlet? 

ANS: A scriptlet contains any language statements, variables, or expressions that can be valid in the page scripting language. A scriptlet is a part of the generated servlet service method. 

 

Q16. What is Pure Servlet? 

ANS: Pure servlet is a servlet that is used to create Java objects that can be implemented from Javax. servlet.Servlet interface. 

 

Q17. What is Generic Servlet class? 

ANS: Generic servlet is the superclass of all servlets. This class is extended by all other classes and it is protocol independent. 

 

Q18. How to get the current HttpSession object? 

ANS: The GetSession method is used to get the current HttpSession object on HttpservletRequest. 

 

Q19. What is Servlet Invoker? 

ANS: Servlet Invoker allows web applications to dynamically register new servlet definitions with the servlet tag in the /WEB-INF/web.xml 

 

Q20. What is the default HTTP method in the servlet? 

ANS: The default method is the GET method for the HTTP servlet. 

 

Q21. What happens if the doGet method is called in a Servlet that only overrides doPost? 

ANS: If a client sends a GET request to a Servlet that overrides only the doPost method, the server responds with an HTTP 405 error (Method Not Allowed). This happens because the default implementation of doGet in the HttpServlet class sends this error when not overridden. 

 

Q22. What is the impact of overriding the service method in a servlet? 

ANS: Overriding the service method allows you to handle all HTTP request types (GET, POST, etc.) in a single method. However, it bypasses the default doGet, doPost, doPut, etc., methods of HttpServlet, making the code less modular and potentially harder to maintain. 

 

Q23. Why is it important to explicitly close database connections in a servlet? 

ANS: If database connections are not closed explicitly: 

It can lead to resource leaks, exhausting the connection pool. 

Other parts of the application may face performance issues. 

It might cause SQLException due to exhausted connections. 

 

Q24. Thread can be used in Servlets? 

ANS: Yes, Single thread can be used in servlets. 

Q25. Can a servlet be used to handle file uploads? How? 

ANS: Yes, servlets can handle file uploads using the Apache Commons FileUpload library or by using the Servlet 3.0 @MultipartConfig annotation

Browse our course links: Full Stack Training in Pune

To Join our FREE DEMO Session: Click Here

Get More Information