Servlet interview Questions - 1


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?

The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet: 
Servlet class is loaded.
Servlet instance is created.
init method is invoked.
service method is invoked.
destroy method is invoked.


As displayed in the above diagram, there are three states of a servlet: new, ready and end. The servlet is in new state if servlet instance is created. After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet performs all the tasks. When the web container invokes the destroy() method, it shifts to the end state.


1) Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.

2) Servlet instance is created
The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.

3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:

public void init(ServletConfig config) throws ServletException

4) service method is invoked
The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below

public void service(ServletRequest request, ServletResponse response)

throws ServletException, IOException

5) destroy method is invoked
The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below:

public void destroy()

3- What are the life-cycle methods for a servlet?

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 -
1) Limited amount of data can be sent because data is sent in header.
2) Not Secured because data is exposed in URL bar.
3) Can be bookmarked
4) Idempotent
5) It is more efficient and used than Post
Post
1) Large amount of data can be sent because data is sent in body.
2) Secured because data is not exposed in URL bar.
3) Cannot be bookmarked
4) Non-Idempotent
5) 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?

When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:
RequestDispacher interface
sendRedirect() method etc.

10- What is the purpose of RequestDispatcher Interface?

The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interceptor can also be used to include the content of antoher resource.

11- Can you call a jsp from the servlet?

Yes, one of the way is RequestDispatcher interface for example:
RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");
rd.forward(request,response);

12- Difference between forward() method and sendRedirect() method ?

forward() method
1) forward() sends the same request to another resource.2) forward() method works at server side.
3) forward() method works within the server only.

sendRedirect() method
1) sendRedirect() method sends new request always because it uses the URL bar of the browser.
2) sendRedirect() method works at client side
3) sendRedirect() method works within and outside the server.

13- What is difference between ServletConfig and ServletContext?

The container creates object of ServletConfig for each servlet whereas object of ServletContext is created for each web application.

14- What is Session Tracking?

Session simply means a particular interval of time.
Session Tracking is a way to maintain state of an user.Http protocol is a stateless protocol.Each time user requests to the server, server treats the request as the new request.So we need to maintain the state of an user to recognize to particular user.

HTTP is stateless that means each request is considered as the new request. It is shown in the figure given below:


Why use Session Tracking?
To recognize the user It is used to recognize the particular user.
Session Tracking Techniques
There are four techniques used in Session tracking:

Cookies
Hidden Form Field
URL Rewriting
HttpSession

15- What are Cookies?

There are 2 types of cookies in servlets.
Non-persistent cookie
Persistent cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.
Persistent cookie

It is valid for multiple session . It is not removed each time when user closes the browser. It is removed only if user logout or signout.

Advantage of Cookies
Simplest technique of maintaining the state.
Cookies are maintained at client side.
Disadvantage of Cookies
It will not work if cookie is disabled from the browser.
Only textual information can be set in Cookie object.

16- What is difference between Cookies and HttpSession?

Cookie works at client side whereas HttpSession works at server side.

17- What is filter?

A filter is an object that is invoked at the preprocessing and postprocessing of a request.
It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.

18- How can we perform any action at the time of deploying the project?

By the help of ServletContextListener interface.

19- What is the disadvantage of cookies?

It will not work if cookie is disabled from the browser.

20- How can we upload the file to the server using servlet?

One of the way is by MultipartRequest class provided by third party.

21- What is load-on-startup in servlet?

The load-on-startup element of servlet in web.xml is used to load the servlet at the time of deploying the project or server start. So it saves time for the response of first request.

22- What if we pass negative value in load-on-startup?

It will not affect the container, now servlet will be loaded at first request.

23- What is war file?

A war (web archive) file specifies the web elements. A servlet or jsp project can be converted into a war file. Moving one servlet project from one place to another will be fast as it is combined into a single file.

24- How to create war file?
The war file can be created using jar tool found in jdk/bin directory. If you are using Eclipse or Netbeans IDE, you can export your project as a war file.
To create war file from console, you can write following code.

jar -cvf abc.war *

25- What are the annotations used in Servlet 3?

There are mainly 3 annotations used for the servlet.
@WebServlet : for servlet class.
@WebListener : for listener class.
@WebFilter : for filter class.

26- Which event is fired at the time of project deployment and undeployment?

ServletContextEvent.

27- Which event is fired at the time of session creation and destroy?

HttpSessionEvent.

28- Which event is fired at the time of setting, getting or removing attribute from application scope?

ServletContextAttributeEvent.

29- What is the use of welcome-file-list?

It is used to specify the welcome file for the project.


30- What is the use of attribute in servlets?
Attribute is a map object that can be used to set, get or remove in request, session or application scope. It is mainly used to share information between one servlet to another.