JAVA Servlet Interview Questions and Answers

Explain the life cycle methods of a Servlet?

The javax.servlet.Servlet interface defines the three methods known as life-cycle method.
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()


First the servlet is constructed, then initialized wih the init() method. 
Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet. 
The servlet is removed from service, destroyed with the destroy() method, then garbaged collected and finalized. 

What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?

The getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a "/" it is interpreted as relative to the current context root.

The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths. All path must sart with a "/" and are interpreted as relative to curent context root.

Explain the directory structure of a web application?.

The directory structure of a web application consists of two parts. 
A private directory called WEB-INF
A public resource directory which contains public resource folder.
WEB-INF folder consists of 
1. web.xml
2. classes directory
3. lib directory

What are the common mechanisms used for session tracking?

Cookies
SSL sessions
URL- rewriting

Explain ServletContex?.
ServletContext interface is a window for a servlet to view it's environment. A servlet can use this interface to get information such as initialization parameters for the web application or servlet container's version. Every web application has one and only one ServletContext and is accessible to all active resource of that application.
What is preinitialization of a servlet?
A container doesnot initialize the servlets as soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.

What is the difference between Difference between doGet() and doPost()?

A doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this limitation. A request string for doGet() looks like the following: 
http://www.abc.com/svt1?p1=v1&p2=v2&...&pN=vN
doPost() method call doesn't need a long text tail after a servlet name in a request. All parameters are stored in a request itself, not in a request string, and it's impossible to guess the data transmitted to a servlet only looking at a request string. 

What is the difference between HttpServlet and GenericServlet?

A GenericServlet has a service() method aimed to handle requests.
HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1). Both these classes are abstract.

What is the difference between ServletContext and ServletConfig?

ServletContext: Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized. 

ServletConfig: The object created after a servlet is instantiated and its default constructor is read. It is created to pass initialization information to the servlet.

Latest Updates

General Knowledge of India

Which of the following dance forms was introduced by the great Vaishnava saint and reformer Mahapurusha Sankaradeva ?

Which of the following is the only classical dance of india having links with Muslim culture and represents a unique synthesis of Hindu and Muslim genius in art ?

Time and Distance

Starting from the same point at the same time, Ruhan and Yuvan had to reach the same destination. While Ruhan drove at 45 km/hr, Yuvan was slower by 5 km/hr. If Yuvan arrived at the destination an hour after Ruhan did, the distance covered by each, in Kilometres, was ?

Simple Interest

A sum of money was invested at a certain rate of simple interest for 4 years. Had the sum been invested at a rate 3% higher than the actual rate, the interest earned would have been 720 Rs more. Find the sum initially invested?

Ratio and Proportion

A bag contains red, blue and black balls. The ratio of red to blue balls is 3 : 5. The ratio of black to red balls is 3 : 4. What is the ratio of blue to black balls?

Profit and Loss

Selling a pack of incense sticks for 136 Rs resulted in a loss of 15%. To make a profit of 10%, the selling price should be ?

Percentage

23% of a bucket full of diluted milk was water. If the quantity of pure milk in the bucket was 15.4 liter, then the capacity of the bucket was ?

Average

The average of the marks obtained by three girls in a test was twice the average of the marks obtained by eight boys in that test. If the total marks obtained by all these examinees was 168, find the average marks each of the girls obtained ?

Pipes and Cistern

44 pipes can fill a large water tank in 27 hours. How many hours it take for 66 pipes to fill four such tanks ?

HCF and LCM

A natural number, when divided by 3, 4, 5, 6, and 7 respectively, leaves corresponding remainder of 2, 3, 4, 5 and 6. What is the smallest of all such numbers fulfilling the above condition ?

Coding-Decoding

If in a certain code language, PRINT is written as 34825 and TRUE is written as 5479. Then, how will TENURE be written in that language?

Order Sequence

Arrange the given words from smallest to biggest-
1.Jug
2.Mug
3.Bucket
4.Drum
5.Cup

Statement and Conclusion

Statements
All socks are shoes
Some shoes are black

Conclusions
I. Some shoes are socks.
II. No scoks is black

Numbers

The difference between the place value of 4 and 2 in the number 833749502 is ?

Analogy

GIKM : HJLN :: PRTV : .........?

Order Sequence

In a row of girls, Nanda is eighth from the left and Chanda is nineteenth from right. When Nanda and Chanda interchange their positions, Chanda becomes twnty-third form right. How many girls are there in the row?

Analogy

Summer: Day :: Winter : ........?

Bird:Winges :: Fish:........?