Wednesday 9 November 2016

Servlet Interview Question and Answers


1. What are Servlets?
Java Servlet is a server side program which is run to connect http/https request to application or web server.

2. What is called servlet container?
Servlet container resides in the web server where servlet run. It is a part of the web server which handles the request and response. Servlet container manages the servlet life cycle.

3. What is servlet life cycle?
Servlet life cycle is a process from creation of servlet to destroy. Steps of servlet life cycle is as follows.
  • Initialize of servlet – init()
This init() method is called only once when servlet is requested. It is one time initialization.
  • Service of servlet – service()
Each time servlet get the request, service() method is called. Depending on the request type, doGet(), doPost() etc are called.
  • Terminate of servlet – destroy()
destroy() method is called only once, the time of destruction of servlet.

4. What is the difference between doGet() and doPost() method?
  • In doGet(), the parameters are appended to the URL and sent along with header information. Where as in doPost() it is not. In doPost(), parameters are sent separately.
  • doGet() has the limitation constrain of sending header information, whcih is 1024 bytes. But doPost() doesn’t have the limitation.
  • doGet() is used in the case of insensitive data transfer. doPost() is used for the case of large number of sensitive data.
  • doGet() is the default method, where as to use of doPost() developer needs to call explicitly

5. What is Session?
Session is a object used by Servlet. It is used to track user information across the web application for multiple request.

6. What is Generic Servlet Class?
Generic servlet is the basic implementation of a servlet. It comes in javax.servlet package. Generic servlet is an abstract class and unlike HttpServlet, GenericClass class can handle any types of request which means protocol independently.

7. What is Http Servlet?
HttpServlet class is the sub class of GenericServlet class and HttpServlet can handle only Http protocols request. It provides specific methods such as doGet, doPost, doPut, doDelete etc.

8. What is ServletConfig Interface?
ServletConfig get initiated during the initialization of servlet. ServletConfig is available in the life span of servlet. It comes in javax.servlet package. Servlet config is accessible from a particular servlet for which is set in web.xml

9. What is ServletContext Interface?
ServletContext get initiated during loading of web application. ServletContext is available once per web application. It is global object. It comes in javax.servlet package. ServletContext is defined in tag in web.xml.

10. What is Servlet Request?
ServletRequest helps to provide the information about client request. Information about Parameter name and value, request header, request attribute etc. To implement the ServletRequest we need to use javax.servlet.ServletRequest or javax.servlet.http.HttpServletRequest packages. We will discuss mainly on HttpServletRequest and its important methods.

11. What is Send Redirect?
sendRedirect() method will redirect the control to other resource. It could be other domain also. sendRedirect() works only on response object.
Importent features of sendRedirect() method:
  • Request and response object are lost for every sendRedirect() method call.
  • sendRedirect() can transfer the control to the other domain also.
  • It is happening in the client side.

12. What is MIME Type?
The “Content-Type” of response header is known as MIME Type. Server sends MIME type to client to let them know the kind of data it is sending. It helps client in rendering the data for user. Some of the mostly used mime types are text/html, text/xml, application/xml etc.

13. What are servlets filters?
Filters is a java class used by Servlet. Servlet Filter is used to intercept user request before accessing the server resources.

14. What are cookies?
Cookies are text files stored on the client computer and they are kept for various information tracking purpose. Java Servlets transparently supports HTTP cookies.

15. What is internalization?
Internalization means enabling a web site to provide different versions of content translated into the visitor’s language.

Share This

0 comments: