Servlet Interview Questions and Answers

Servlet Interview Questions and Answers

                                                           Servlet Interview Questions

1.What is use of JEE?

A) JEE stands for Java Enterprise Edition, after adding servlet into java this flavor came into the picture, the main intention of this flavor is to develop following applications like
a. Web applications
b. Distributed applications
c. Enterprise applications.

2.What are the types of web applications?

A) Mainly web applications are two types.
1. Static web applications
2. Dynamic web applications

Static web applications mean the content or response of the web application is common for all the end user is called static web application.

To develop any static web applications we required static web components.
A language or technology is used to develop for web applications is called web component.

b. java script
c. css
d. ajax


Dynamic web applications means the content or response of the web application is changing from one end user to another end user is called dynamic web applications.

To develop dynamic web applications we required the following web components

a. Servlets
b. Jsp
c. Php
d. Asp.Net


3.What is Common Gateway Interface?

A) CGI will provide a separate process/address for each and every request to generate appropriate dynamic response.
Here we are facing the one main problem like context switching, that means to providing the services for different client simultaneously, CGI will shift the control from one process to another process for that CGI will take more time.(time consuming process).
More resources are utilized.
Platform dependent.
Poor in performance

4.What is Servlet?

  • Servlet is one JEE web technology is used to generate dynamic web content.
  • Servlet is one JEE Web technology is used to create single instance-multiple response process.
  • Servlet is one JEE Web technology, will increases services of web and application servers.
  • Servlet is one JEE Web technology will provides rules and guide lines to develop our own web components.
  • Servlet is one class (user define) it is used to extends new features.

5.What are the main important components in web server?

A) The following are the important components in web server.
a. Http Implementation classes
b. Servlet Container.
c. Servlet Implementation classes.
d. JSP Container.
e. JSP Implementation classes.
f. Middleware implementation classes.

6.Is it mandatory to declare Servlet class as public?

A) Yes. For compiling Servlet program, there is no problem, but executing Servlet Program we required “public”. The reason is Servlet Container will create object for our class, once we write Servlet class as public then only that class can visible to Servlet Container and creating object for that class.

7.Is it mandatory to declare service (-,-) as public?

A) Yes. That is the principal of method overriding concept. Sub class method access modifier must be same as super class method access modifier or increasable.

8.Can we change service () return type?

A) No. Sub class method return type must be same super class method return type.

9.Is it mandatory to write throws ServletException, IOException?

A) No. But our logic required or demands definitely we need to write.

10.What is meaning of “out” variable and can we change or not?

A) It is just reference variable, if we want we can change the variable name also.

11.Can we change service (-,-) name to other method?

A: No. We need to use same method name, Servlet container only recognize the method name like service (). Sub Class Override method must be same as super class method.

12.What is functionality of out.println()?

A) It will add response/information into ServletResponse object and carry that response/information from web server to browser and print that information/response on browser body.

13.What is use of resp.setContentType (“text/html”)?

A) This method is used to setting content-type HTTP response header to provide, information to the browser about the type of content we are sending.
This is also called MIME. (Multipurpose Internet Mail Extension).

14.Can we create more than one web.xml file for one project?

A) No.

15.How many web.xml files are using for configure more than one servlet?

A) Only one.

16.How can we configure more than one servlet details in one web.xml file?

A) By using multiple <servlet> and <servlet-mapping> tag.

17.Can we write <url-pattern> content without ‘/’ or ‘*’ ?

A) No, server not starts.

18.Can we write same <url-pattern> content for both Servlet?

A) Yes no problem server start but we are unable send request for servlet the reason servlet container will ambiguous.

19.Can we more than one servlet class in one “.java” file?

A) NO. Every servlet and its constructor should be public within the one “.java” file we can create more than one “public” class.

20.How many Servlet Execution Model do we have servlet?

A) There are two execution models in servlet.
a. Single Instance Single Thread Model
b. Single Instance Multi Thread Model.

21. How many servlet life cycle phases do we have and what are those?

A) There are five life cycle phases in servlet.
a. Loading Phase
b. Instantiation Phase
c. Initialization Phase
d. Servicing Phase
e. Destroy Phase.

22.How many life cycle methods do we have and what are those?

A) There are three life cycle methods in servlet
a. init(ServletConfig) throws ServletException
b. service(ServletRequest, ServletRespone)throws ServletException,IOException
c. destroy()

23.What is the difference between init() and init(ServletConfig) of GenericServlet?

A) init() is convince method to call GenericServlet init(ServletConfig) method to initialize the ServletConfig object.If we overridden only init() in our program internally our control goes to GenericServlet class init(ServletConfig) and initializing ServletConfig object and calling init().

24.What is difference between Lifecycle method Lifecycle call back methods?

A) The methods which are calling by servlet container automatically are called lifecycle methods, the methods which are calling from lifecycle methods are called lifecycle call back methods.

25.What is difference between ServletConfig and ServletContext?

A) ServletConfig is useful for reading the initialization values from web.xml file under the <init-param>, these are useful for only one servlet, the reason servlet container will creates one associate ServletConfig object for every servlet, ServletContext is useful for reading the context values from web.xml file under the <context-param>, these are useful for every servlet, the reason servlet container will crates only one ServletContext object for entire project.

26.What is the use of <load-on-startup>?

A) While deploying our project into server, if we want to execute loading, instantiation, initialization phase for providing equal response to all end user, then we should use <load-on-startup>.

27.What is the use of <welcome-file-list>?

A) To provide facilities and comfortableness to end user to interact with our website we should give user friendly interaction page through either .html file or .jsp file or .java. To perform above operation we should use <welcome-file-list> and <welcome-file> tags.

28.What is use of url-patterns in servlet?

A) The main intention of url-pattern is, to interact with our servlet through alias or nick or alternative name for providing security.

29.How many types of url patterns do we have in servlet?

A) There are three types of url patterns in servlet.
a. Exact match url pattern
b. Directory match url pattern
c. Extension match url pattern

30.How many scope objects do we have in servlet?

A) There are three scope objects in servlet
a. request
b. session
c. context

31.Can we able to make communication between one servlet to another servlet by creating object through programmatically?

A) Yes, we can. If we are creating object for servlet programmatically servlet container won’t create ServletConfig object for that class.

32.What is use of RequetDispatcher object?

A) This is useful for making a communication between one servlet to another servlet, jsp, html, txt.

33.How many approaches do we have in RequestDispatcher?

A) We have two approaches in RequestDispatcher. Those are
a. include(-,-)
b. forward(-,-)

34.What is the difference between include(-,-) and forward(-,-)?

A) in the include mechanism both source and servlet web component responses will added to browser, where as in forward mechanism only destination web component response will added to browser.

35.What is difference between the bellow object creation statements?

RequestDispatcher rd = request.getRequestDispatcher(“-“);
RequestDispatcher rd = context.getRequestDispatcher(“-“);
A) In the first approaches we can forward the control to source web component to destination web component in the same project.
B) In the second approaches we can forward the control to source web component to destination web component in the different projects.

36.What are the drawbacks of GenericServlet?

When ever client sending the request data, that data will be attached to browser URL and visible to everyone so security problem.
We cannot get http protocol specification like
? Security:
? Request Header information.
? Session tracking
?No redirecting process.
To overcome the above problems we should go for HttpServlet interface.

37.When should we use doGet(-,-), doPost(-,-) in Servlet?

1. doGet(-,-) : Is useful for reading/accessing web resource from web server.
2. doPost(-,-): Is useful for placing/creating new resource in the web server.

38.Who will calls doXxx(-,-)?

A) doXxx(-,-) methods always calling by protected service(-,-), this was calling from public service(-,-), this was calling by servlet container.

39.What are safe and unsafe methods in Servlet?

A) The methods, which are not modifying data on server is called safe method, otherwise we can called as unsafe methods.
Safe methods: Get, Head, Options, Trace
Unsafe methods: Post, Put, Delete

40. How many can we call doGet(-,-) in servlet?

A) This is default method in servlet, mainly we can call this method by four ways.
a. directly sending data from browser url.
b. <form> with method attributes.
c. <form> without method attributes.
d. by using hyperlink.

41.What are the differences between doGet() and doPost()?

1. It is design for getting the data from server
2. Data will visible in brower url.
3. We can’t send secure data.
4. We can send only limited data.
5. We can send only 1024 characters data
6. We can’t upload files the reason is url supports only characters but not binary format data
7. We have four approaches to work with doGet().
a. By default browser
b. By method=’get’
c. Writing data directly in URL.
d. By using HyperLink.

1. It is design for uploading the data into server
2. Data will not visible in brower url.
3. We can send secure data.
4. We can send unlimited data.
5. There is no limitation for data sending.
6. We can upload files
7. We have only one approaches to work with doPost().
a. By method=’get’

42.What is use of sendRedirect process in servlet?

A) If we want make a communication between one servlet project to another servlet project within the two different servers we should go for sendRedirect process.

43.What is Session Tracking?

A) Recognize client state at some particular period of time as a same client is called Session Tracking. HttpServlet is always working with HttpProtocol. HttpProtocol is stateless protocol. Every time this protocol will treat client as a new client. To recognize client as an old client or same client we should go for Session Tracking.

We can achieve session tracking by using the following four ways.
a. Cookie
b. Hidden Form Fields
c. URL Rewriting
d. HttpSession

44.What is cookie in servlets?

  • It is plain textual information. It contains key and value. It is useful recognize the client persistence (state). Whenever we make request to server at first time only request data are handover to server, but in the response we can add cookie object in the form key-value pair combinations. These are stored in browser cache.
  • Whenever client making second request, along with request cookie object is also coming to server, then server will read that cookie and finally recognize that client/request old client/request.

45.How many types of cookies do we have in Servlet?

A) We have two types of cookies.
a. Persistence cookie.
b. Non-persistence cookie.
Non-Persistence cookie:
The cookie is available up to before closing browser is called Non-persistence cookies.
Persistence cookie:
The cookies are available after closing the browser also available up to end user sign out or logout is called persistence cookie.
This is simple technique and existed at client side. The drawback of cookie is we can send only textual information. Once browser side, if we are disable the cookie we unable maintain session tracking.

46.How to store, delete and read the data in scope objects?

A) To insert the data into scope objects, we have setAttribute(-,-) and to read the data from scope objects, we have getAttributed(-) and to delete the data from scope objects we have removeAttribute().

47.What is Filter object in servlet?

A) It is one java object (JEE). It is one web component is used for developing web application like servlet and jsp. It is one servlet container/server managed java object. Like Servlet, Filter is also having life cycle methods. Like init(-), destroy(), doFilter(-,-,-).

48.Who will call Filter life cycle methods?

A) All the Filter life cycle methods are calling by ServletContainer. That’s why we are calling filter is one servlet container managed object.

49.What is the use of Filter in servlet?

A) It is acting as a mediator between browser and servlet.
Checks Logging details valid or not
Checks whether end user valid or not
Doing some common operations for all servlets.
Creating our Request and Response objects.

50.How many types of Filters in Servlet?

A) If we want to execute or process any logic before servlet logic after servlet logic execution, we should use filter.
Based on above point Filters are classified into three types.
a. Pre Filter:
Before logic of servlet execution, filter logic will be executes.
b. Post Filter:
After logic of servlet execution, filter logic will be executes.
c. Pre-Post Filter:
Before and after servlet code execution, if we are executing Filter code, that type of filter we are consider as pre-post filter.

51.How many important interfaces do we have in filter concept?

a. Filter: It is useful for developing web applications.
b. FilterConfig: It is useful for reading the data from web.xml file under <init-param> tags. Every Filter object having their own Filter-Config object
c. FilterChain: It is useful for making communication between Filter to Servlet.

52.What is difference between doFilter(-,-,-) of Filter interface and doFilter(-,-) FilterChain interface?

doFilter(-,-,-) of Filter interface:
1. This is a life cycle method of Filter.
2. We should override this method in our filter class.
3. Calling and execution everything taken care by servlet container by default.
4. It is having 3 parameters like ServletRequest, ServletResponse, FilterChain.
5. Acutal logic like authentication, logging details verification and writing common logic of all servlets we need write in this method.

doFilter(-,-) of FilterChain interface:
1. This is a not life cycle method of FilterChain.
2. No need override this method in our filter class.
3. Servlet Container not calling and execute this method by default, program should call.
4. It is having 2 parameters like ServletRequest, ServletResponse.
5. This method is useful making communication between filter to servlet.

Share this post