Servlets Interview Questions and Answers
by Nithyanandham, on Sep 11, 2022 6:11:06 PM
Q1. What is servlet?
Ans: Servlet is a server side programming language which is used for generating dynamic web pages. It generates web-page as a response of the request received from client(browser).
Q2. What are the uses of Servlets?
- To process the input data submitted by user from the screen.
- Generate and return the dynamic response to the user based on the request.
- Ideal programming language for interacting with database based on the user’s request.
- A servlet can handle multiple request simultaneously which makes them a perfect choice for the high performing applications.
Q3. What is ServletConfig?
ServletConfig interface belongs to the package
javax.servlet.ServletConfig. It is used for passing the configuration parameters to the servlet. Servlet container instantiate it implicitly.
Q4. What is ServletContext?
Ans: Each web application has a common
ServletContext. All the servlets in the web application can access the ServletContext. It has the web-application information & resources which are common and accessible to all the servlets present in the web application.
Q5. Explain Life cycle of a servlet?
Ans: Following the the stages of servlet life cycle:
- Loading of Servlet class: The servlet container finds the servlet class mentioned in web.xml file and loads it.
- Servlet instantiation: The object of servlet class gets created in this phase.
- Initialization: Servlet initialization by calling
- Servicing the request: In this phase the servlet service the client request by calling the
- Destroy: Last phase of servlet life cycle. The destroy() method free up the servlet instance so that it can be garbage collected.
Q6. When Servlet is loaded?
- When servlet container receives the first request from client(browser).
- Admin of the application manually loads the servlet.
- When the webserver(in which the servlet is deployed) gets started.
Q7. Why Servlet is better than CGI?
- Servlet responses faster than CGI because it uses the multithreading concept to service each request. CGI performance is not that good as it creates a new object for each request while servlet allots a new thread for each request.
- Learning and implementing servlet is quite easier compared to CGI.
- Memory consumption is low in servlet compared to CGI.
Q8 . What is Servlet chaining?
Ans: Servlet chaining is a concept where the request is processed in a chain of servlets. First Servlet processes the request partially and passes to the second one, then second servlet process it and passes to third one and so on. The last servlet returns the response to the client (browser).
Q9. What are the different types of session tracking mechanism supported by Servlets?
- URL rewriting
- Hidden Form Fields
- Secure Socket Layer(SSL) Sessions
Q10. Static webpage vs Dynamic webpage?
The webpages which are same for all the users are static webpages and the webpages that are dynamically generated based on the user’s request (that may be different for each user depending on the request) are known as dynamic webpages. Servlet is mainly used for dynamic webpages.
Q11. What are the main functions of Servlet container?
- Servlet life cycle management
- Maintains the interaction between servlet and webserver.
- Providing multithreading support for processing more than one request simultaneously.
- Managing of deployment descriptor web.xml file.
Q12. What is Servlet interface and what’s the use of it?
Ans: Servlet interface is an API for servlets. Every Servlet should either implement the servlet interface or extends the class which already implements the interface.
javax.servlet.http.HttpServlet are the Servlet classes that implements Servlet interface, hence every servlet should either implement Servlet interface directly or by extending any of these classes.
Q13. Differences between ServletConfig & ServletContext?
Following are the two main differences between
- ServletConfig is used for passing the configuration information to the Servlet while ServletContext provides set of methods which a Servlet class can use to communicate with Servlet container.
- Each Servlet has a separate ServletConfig object while ServletContext is common for all the servlets in the web application.
- Parameters of ServletConfig are defined under <init-param> tags in web.xml file. Parameters of ServletContext are defined under <context-param> tags in web.xml.
Q14. Difference between GenericServlet and HTTPServlet?
- GenericServlet is an abstract class which implements Servlet interface while HTTPServlet abstract class extends the GenericServlet class. In short: GenericServlet class is a parent class for HTTPServlet.
- GenericServlet does not support any protocol. HTTPSeervlet support HTTP and HTTPS protocol.
- GenericServlet cannot handle cookies and session while HTTPServlet can handle them.
Q15. Differences between forward() and sendRedirect()?
forward()the same request is forwarded to the another resource. In
sendRedirect()new request is send to the redirected resource.
- forward() is taken care by the Servlet container while sendRedirect() is handled by the browser.
- In forward() the URL(uniform resource locator) remains same on web browser. In sendRedirect() the URL changes in the web browser address bar.
- forward() is faster compared to sendRedirect().
Q16. What is deployment descriptor?
Ans: web.xml file of a web application is known as deployment descriptor. It is usually placed inside WEB-INF folder of application. It has the information like Servlet name, Servlet mapping etc. This file tells the Servlet container which Servlet class needs to be called for the given URL pattern.
Q17. When the Servlet is unloaded?
- Admin manually unloads the servlet.
- web server shut down.
Q18. How URL rewriting maintains session?
Ans: In URL rewriting method, the session tracking data has been appended at the end of the URL to track the session.
Q19. How to invalidate a session in servlet?
Ans: By calling
Q20. What is Servlet lazy loading and how it can be avoided?
Ans: The Servlet container does not initialize the Servlet on server startup by default. It only initializes a servlet when the it receives the request from the client. This is called lazy loading of Servlet. By specifying <load-on-startup> element for a Servlet we can avoid lazy loading. The servlet files specified in <load-on-startup> are loaded as soon as the web sever starts.
Q21. Why do we need constructor in servlet even though we have a init() method?
Ans: init() method is used for initializing the servlet however constructor is required in order to instantiate the Servlet class. Servlet container instantiate the Servlet class.
Q22. How Servlet maintains session using cookies?
- Cookie is a small piece of information, which is sent by a servlet to the Web browser.
- Cookies gets stored in the browser and returned back to the server when needed.
- A cookie has a name, a single value, and few other attributes.
Q23. Why using cookies for session tracking is a bad practice?
- There are several disadvantages of using cookies for session tracking. Few of them are:
- Since cookies are stored on client-side (in the client’s browser), It will not be available if client browser clears or disables the cookies.
- Implementing cookies for session tracking is much more difficult compared to other session management mechanism.
- Cookies only work for HTTP protocol.
Q24. doGet() Vs doPost() methods?
- In doGet(), the parameters are visible in the address bar, they get appended to the URL. In doPost() parameters are not visible in the address bar.
- You can maximum transfer 1024 characters through GET request. doPost() doesn’t have any limitations.
- doGet() is not good for sensitive data as the parameters do not get encrypted. In doPost() the parameters are encrypted hence it is more secure compared to doGet().
- Method doGet() allow you to bookmark the resource. doPost() doesn’t allow bookmarks.
- doGet() is faster compared to the doPost() method.
Q25. What is the use of <load-on-startup>?
<load-on-startup> is used for specifying the Servlet files which needs to be loaded during server startup. The servlet files specified in this element are loaded as soon as the server starts, it does not wait for the first request for loading them up. This is how it is specified in web.xml file.
If more than one files are specified then the files will be loaded in the same order in which they have been specified in it.