SERVLET INTERVIEW QUESTIONS

1.      What is a Servlet? 
Servlet is a java object that provides the implementation of Servlet interface. 
2.      In which package Servlet interface in present? 
Servlet interface is present in javax.servlet package. 
3.      How many methods does Servlet interface consist of?
Servlet interface consists of 5 methods.
4.      List the methods present in Servlet interface.
public   void   init(ServletConfig  config)
public  ServletConfig   getServletConfig()
public  void  service(ServletRequest  request,ServletResponse  response)
public  String  getServletInfo()
public  void destroy()
5.     What is life cycle of a servlet?
The life cycle of a servlet consists of three methods. They are init (), service () and destroy ().

6.     When init () method is invoked?
The init () method is invoked when the servlet object is created.

7.     When service () method is invoked?
The service () method is invoked when we send a request to the servlet.

8.     When destroy () method is invoked?
The destroy () method is invoked when the servlet object is about to destroyed.
9.      What are ServletRequest and ServletResponse? 
ServletRequest and ServletResponse are the interfaces present in javax.servlet package. 
10.  What is ServletConfig? 
ServletConfig is the interface present in javax.servlet package and this interface is used to get the init parameters to a single servlet. 
11.  How servlet object is created? 
Servlet object is created by web container like tomcat web server. 
12.  What is the limitation of Servlet interface?
Servlet interface consists of five methods. When we implement the servlet interface, we have to implement all the abstract methods present in that interface even though there is no use of the method. To overcome this limitation we can use a class known as HttpServlet class present in javax.servlet.http package.  
13.  What is HttpServlet?
HttpServlet is the class present in javax.servlet.http package. 
14.  What is ServletContext?
The servlet context is an object that contains information about the Web application and container.  Using the context, a servlet can log events, obtain URL references to resources, and set and store attributes that other servlets in the context can use.
15.  What is servlet mapping?
The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to Servlets. 
16. What are context initialization parameters?
Context initialization parameters are specified in the web.xml file, these are initialization parameter for the whole application.

17. What are initialization parameters?
Initialization parameters are specified in the web.xml file under servlet tag, these are initialization parameter for a single servlet.

18. What are different ways for session tracking?
Cookies, URL rewriting, HttpSession, Hidden form fields.

19. What is the difference between GET and POST methods?
In GET, your entire form submission can be encapsulated in one URL, like a hyperlink. Query length is limited to 255 characters, not secure, faster, quick and easy. The data is submitted as part of URL.

In POST data is submitted inside body of the HTTP request. The data is not visible on the URL and it is more secure.

20. What is Redirection in a web application?
In Redirection, when we give one url in the browser then we get output from another url. For Example, when we give url like www.oldmail.com, then we get output from www.newmail.com where both websites belong to same company.

In Redirection, there are multiple requests to execute multiple servlets.

21. What are servlet configuration tags?
These are the tags that are provided in web.xml and these tags provide information to the web server which .class file is to be executed for a given request.

22. How servlet configuration tags are written in web.xml.
               
               
               
               

23.What is web.xml known as?
web.xml file is known as deployment descriptor.

24.What is a war file?
A war file consists of the web information of a web application.

25.What does lib folder consists of in a servlet web application?
The lib folder consists of jar files which are required for a web application.

26.What does classes folder consists of in a servlet web application?
          The classes folder consists of .class files which are executed in a web application.

27.What is a web application?
A web application is a set of web resources like html file or a servlet.

28.What is root directory in a web application?
Root directory is nothing but a project name given to a web application.

29.How many web.xml files are present in a servlet web application?
There in only one web.xml present in a servlet web application and this web.xml file is known as deployment descriptor.

30.How many servlet context objects are present in a servlet web application?
There in only one servlet context object present in a servlet web application and this object is known as application object.

31.What is an application object present in servlet web application?
Servlet context object is known as application object in servlet web application.

32.What is RequestDispatcher?
RequestDispatcher is an interface present in javax.servlet package and this interface consists of two methods. They are include () and forward () methods.

33.What is the difference between include () and forward () methods of RequestDispatcher interface?
When we use include () method, output from both servlets are included in the output.
Whereas when we use forward () method, output is as that of redirection i.e. from second servlet or last servlet.

34.What is the difference between redirection and forward () method of RequestDispatcher interface?
In redirection, there are multiple requests to execute multiple servlets whereas in forward () method there is single request to execute multiple servlets.

35.What is a stateful protocol?
If the protocol remembers the conversational state of a client then that protocol is known as stateful protocol.

36.What is a stateless protocol?
If the protocol doesnot remembers the conversational state of a client then that protocol is known as stateless protocol.

37.What is http protocol?
http protocol is a stateless protocol.

38.What techniques are used to maintain the state of a client?
There are 3 techniques to maintain the state of a client. They are hidden fields, cookie and session.

39.What is a cookie?
Cookie is a small piece of information that is set by the server on the browser.
Cookie is the class present in javax.servlet.http package.

40.What is session?
The session is an object used by a servlet to track a user’s interaction with a web application across multiple HTTP request. The session object is stored on the server.