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?
Ans:
Q3. What is ServletConfig?
Ans: 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:init()
method.service()
method.Q6. When Servlet is loaded?
Ans:
Q7. Why Servlet is better than CGI?
Ans:
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?
Ans:
Q10. Static webpage vs Dynamic webpage?
Ans:
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?
Ans:
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.GenericServlet
and 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?
Ans:
Following are the two main differences between ServletConfig
and ServletContext
:
Q14. Difference between GenericServlet and HTTPServlet?
Ans:
Q15. Differences between forward() and sendRedirect()?
Ans:
forward()
the same request is forwarded to the another resource. In sendRedirect()
new request is send to the redirected resource.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?
Ans:
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 session.invalidate()
method.
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?
Ans:
Q23. Why using cookies for session tracking is a bad practice?
Ans:
Q24. doGet() Vs doPost() methods?
Ans:
Q25. What is the use of <load-on-startup>?
Ans: <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.
<servlet>
<servlet-name>MyServletNameHere</servlet-name>
<servlet-class>ServletClassHere-FullyQualified</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
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.