Servlets Interview Questions and Answers
Q1. What is servlet?
Ans: Servlet is a server aspect programming language that is used for producing dynamic web pages. It generates net-web page as a reaction of the request acquired from customer(browser).
Q2. What are the makes use of of Servlets?
To technique the input facts submitted by person from the display screen.
Generate and go back the dynamic reaction to the person based at the request.
Ideal programming language for interacting with database based totally on the user’s request.
A servlet can cope with a couple of request simultaneously which makes them an excellent preference for the high acting applications.
Q3. What is ServletConfig?
Ans: ServletConfig interface belongs to the bundle javax.Servlet.ServletConfig. It is used for passing the configuration parameters to the servlet. Servlet field instantiate it implicitly.
Q4. What is ServletContext?
Ans: Each internet application has a commonplace ServletContext. All the servlets within the internet utility can get admission to the ServletContext. It has the internet-software records & resources which might be common and reachable to all of the servlets gift within the web application.
Q5. Explain Life cycle of a servlet?
Ans: Following the the ranges of servlet existence cycle:
Loading of Servlet elegance: The servlet field unearths the servlet class cited in internet.Xml report and loads it.
Servlet instantiation: The object of servlet class gets created on this phase.
Initialization: Servlet initialization by using calling init() approach.
Servicing the request: In this section the servlet service the purchaser request through calling the service()approach.
Destroy: Last section of servlet lifestyles cycle. The destroy() technique unfastened up the servlet example so that it could be rubbish amassed.
Q6. When Servlet is loaded?
When servlet field receives the first request from client(browser).
Admin of the application manually hundreds the servlet.
When the webserver(wherein the servlet is deployed) gets started.
Q7. Why Servlet is better than CGI?
Servlet responses quicker than CGI because it uses the multithreading concept to carrier each request. CGI performance isn't always that correct because it creates a new object for every request while servlet allots a new thread for every request.
Learning and imposing servlet is pretty easier in comparison to CGI.
Memory consumption is low in servlet compared to CGI.
Q8 . What is Servlet chaining?
Ans: Servlet chaining is a concept wherein the request is processed in a series of servlets. First Servlet approaches the request partially and passes to the second one, then 2nd servlet process it and passes to 1/3 one and so forth. The last servlet returns the response to the patron (browser).
Q9. What are the different types of session monitoring mechanism supported through Servlets?
Hidden Form Fields
Secure Socket Layer(SSL) Sessions
Q10. Static website vs Dynamic web site?
The webpages which might be identical for all the customers are static webpages and the webpages which can be dynamically generated primarily based on the user’s request (that can be one of a kind for every consumer relying on the request) are called dynamic webpages. Servlet is specially used for dynamic webpages.
Q11. What are the principle features of Servlet container?
Servlet life cycle control
Maintains the interplay among servlet and webserver.
Providing multithreading guide for processing a couple of request simultaneously.
Managing of deployment descriptor web.Xml record.
Q12. What is Servlet interface and what’s using it?
Ans: Servlet interface is an API for servlets. Every Servlet need to either put in force the servlet interface or extends the elegance which already implements the interface. Javax.Servlet.GenericServlet and javax.Servlet.Http.HttpServlet are the Servlet lessons that implements Servlet interface, for this reason every servlet ought to both enforce Servlet interface at once or through extending any of those instructions.
Q13. Differences among ServletConfig & ServletContext?
Following are the two primary variations between ServletConfig and ServletContext:
ServletConfig is used for passing the configuration records to the Servlet whilst ServletContext affords set of strategies which a Servlet magnificence can use to communicate with Servlet field.
Each Servlet has a separate ServletConfig object while ServletContext is commonplace for all of the servlets within the internet application.
Parameters of ServletConfig are defined beneath <init-param> tags in net.Xml record. Parameters of ServletContext are defined underneath <context-param> tags in web.Xml.
Q14. Difference between GenericServlet and HTTPServlet?
GenericServlet is an summary magnificence which implements Servlet interface even as HTTPServlet abstract class extends the GenericServlet magnificence. In quick: GenericServlet class is a discern magnificence for HTTPServlet.
GenericServlet does no longer support any protocol. HTTPSeervlet help HTTP and HTTPS protocol.
GenericServlet cannot take care of cookies and consultation while HTTPServlet can handle them.
Q15. Differences between forward() and sendRedirect()?
In ahead() the same request is forwarded to the another aid. In sendRedirect() new request is send to the redirected resource.
Forward() is taken care with the aid of the Servlet container at the same time as sendRedirect() is treated by means of the browser.
In forward() the URL(uniform aid locator) remains identical on net browser. In sendRedirect() the URL modifications within the web browser deal with bar.
Ahead() is quicker as compared to sendRedirect().
Q16. What is deployment descriptor?
Ans: web.Xml report of a web application is referred to as deployment descriptor. It is commonly located inside WEB-INF folder of software. It has the facts like Servlet call, Servlet mapping and so forth. This document tells the Servlet container which Servlet magnificence desires to be referred to as 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 approach, the consultation monitoring records has been appended at the cease of the URL to music the consultation.
Q19. How to invalidate a session in servlet?
Ans: By calling session.Invalidate() approach.
Q20. What is Servlet lazy loading and how it can be averted?
Ans: The Servlet field does now not initialize the Servlet on server startup through default. It only initializes a servlet whilst the it gets the request from the consumer. This is referred to as lazy loading of Servlet. By specifying <load-on-startup> detail for a Servlet we will keep away from lazy loading. The servlet files laid out in <load-on-startup> are loaded as soon as the net sever begins.
Q21. Why can we need constructor in servlet even though we've got a init() method?
Ans: init() technique is used for initializing the servlet however constructor is required so one can instantiate the Servlet class. Servlet container instantiate the Servlet elegance.
Q22. How Servlet keeps consultation the usage of cookies?
Cookie is a small piece of data, that's sent via a servlet to the Web browser.
Cookies receives saved inside the browser and back lower back to the server whilst needed.
A cookie has a name, a unmarried value, and few different attributes.
Q23. Why using cookies for session tracking is a terrible practice?
Since cookies are stored on consumer-side (in the consumer’s browser), It will no longer be to be had if customer browser clears or disables the cookies.
Implementing cookies for consultation monitoring is tons more tough in comparison to different consultation control mechanism.
Cookies best work for HTTP protocol.
Q24. DoGet() Vs doPost() techniques?
In doGet(), the parameters are visible in the address bar, they get appended to the URL. In doPost() parameters are not seen inside the deal with bar.
You can maximum switch 1024 characters thru GET request. DoPost() doesn’t have any barriers.
DoGet() isn't exact for touchy records because the parameters do no longer get encrypted. In doPost() the parameters are encrypted subsequently it's miles extra comfy as compared to doGet().
Method doGet() let you bookmark the resource. DoPost() doesn’t allow bookmarks.
DoGet() is faster in comparison to the doPost() technique.
Q25. What is the usage of <load-on-startup>?
Ans: <load-on-startup> is used for specifying the Servlet documents which wishes to be loaded throughout server startup. The servlet files designated in this detail are loaded as quickly because the server starts offevolved, it does now not watch for the first request for loading them up. This is how it's far laid out in internet.Xml document.
If more than one documents are specific then the documents may be loaded inside the same order in which they had been laid out in it.