Interview :: Servlets
1) How many objects of a servlet is created?
Only one object at the time of first request by servlet or web container.
2) What is the life-cycle of a servlet?
3) What are the life-cycle methods for a servlet?
Method | Description |
---|---|
public void init(ServletConfig config) | It is invoked only once when first request comes for the servlet. It is used to initialize the servlet. |
public void service(ServletRequest request,ServletResponse)throws ServletException,IOException | It is invoked at each request.The service() method is used to service the request. |
public void destroy() | It is invoked only once when servlet is unloaded. |
4) Who is responsible to create the object of servlet?
The web container or servlet container.
5) When servlet object is created?
At the time of first request.
6) What is difference between Get and Post method?
Get | Post |
---|---|
1) Limited amount of data can be sent because data is sent in header. | Large amount of data can be sent because data is sent in body. |
2) Not Secured because data is exposed in URL bar. | Secured because data is not exposed in URL bar. |
3) Can be bookmarked | Cannot be bookmarked |
4) Idempotent | Non-Idempotent |
5) It is more efficient and used than Post | It is less efficient and used |
7) What is difference between PrintWriter and ServletOutputStream?
PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.
8) What is difference between GenericServlet and HttpServlet?
The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.
9) What is servlet collaboration?