Servlet interview questions
There is a list of 30 servlet interview questions for beginners and professionals. If you know any servlet interview question that has not been included here, kindly post your question in the Ask Question section.
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?
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?
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:
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.more details...
11) Can you call a jsp from the servlet?
Yes, one of the way is RequestDispatcher interface for example:more details...
12) Difference between forward() method and sendRedirect() method ?
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.more details...
15) What are Cookies?
A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.more details...
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 either at the preprocessing or postprocessing of a request. It is pluggable.more details...
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.more details...
20) How can we upload the file to the server using servlet?
One of the way is by MultipartRequest class provided by third party.more details...
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.more details...
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.more details...
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.more details...
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.
Now all the files of current directory will be converted into abc.war file.more details...
25) What are the annotations used in Servlet 3?
There are mainly 3 annotations used for the servlet.
26) Which event is fired at the time of project deployment and undeployment?
27) Which event is fired at the time of session creation and destroy?
28) Which event is fired at the time of setting, getting or removing attribute from application scope?
29) What is the use of welcome-file-list?
It is used to specify the welcome file for the project.more details...
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.more details...