Servlet Interview Questions

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Q: What is servlet?

A: Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.

Q: What are the classes and interfaces for servlets?

A: Most commonly used interfaces and classes
javax.servlet.Servlet interface and
javax.servlet.ServletConfig interface
by implementing them we get


by extending GenericServlet we get

most people used HttpServlet to create Servlets

Q: What is the difference between an applet and a servlet?

A: a) Servlets are to servers what applets are to browsers. b) Applets must have graphical user interfaces whereas servlets have no graphical user interfaces.

Q: What is the difference between doPost and doGet methods?

A: a) doGet() method is used to get information, while doPost( ) method is used for posting information.

b) doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost( ) requests passes all of its data, of unlimited length.

c) A doGet( ) request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.

Q: What is the life cycle of a servlet?

A: Each Servlet has the same life cycle:

a) A server loads and initializes the servlet by init( ) method.
b) The servlet handles zero or more client’s requests through service( ) method.
c) The server removes the servlet through destroy() method.

Q: Who is loading the init() method of servlet?

A: Web server

Q: What are the different servers available for developing and deploying Servlets?

A: a) Java Web Server
b) JRun
g) Apache Server
h) Netscape Information Server
i) Web Logic

Q: How many ways can we track client and what are they?

A: The servlet API provides two ways to track client state and they are:
a) Using Session tracking and
b) Using Cookies.

Q: What is session tracking and how do you track a user session in servlets?

A: Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:

a) User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password

b) Hidden form fields - fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server

c) URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extvra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.

d) Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.

e) HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session.maxresidents property.

Q: What is Server-Side Includes (SSI)?

A: Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include server-side includes must be stored with an .shtml extension.

Q: What are cookies and how will you use them?

A: Cookies are a mechanism that a servlet uses to have a client hold a small amount of state-information associated with the user.

a) Create a cookie with the Cookie constructor:

public Cookie(String name, String value)

b) A servlet can send a cookie to the client by passing a Cookie object to the addCookie() method of HttpServletResponse:public void HttpServletResponse.addCookie(Cookie cookie)

c) A servlet retrieves cookies by calling the getCookies() method of HttpServletRequest:

public Cookie[ ] HttpServletRequest.getCookie( ).

Q: Is it possible to communicate from an applet to servlet and how many ways and how?

A: Yes, there are three ways to communicate from an applet to servlet and they are:

a) HTTP Communication(Text-based and object-based)
b) Socket Communication
c) RMI Communication (You can say, by using URL object open the connection to server and get the InputStream from URLConnection object).

Steps involved for applet-servlet communication:

1) Get the server URL.
URL url = new URL();

2) Connect to the host
URLConnection Con = url.openConnection();

3) Initialize the connection

4) Data will be written to a byte array buffer so that we can tell the server the length of the data.
ByteArrayOutputStream byteout = new ByteArrayOutputStream();

5) Create the OutputStream to be used to write the data to the buffer.
DataOutputStream out = new DataOutputStream(byteout);

Q: What is connection pooling?

A: With servlets, opening a database connection is a major bottleneck because we are creating and tearing down a new connection for every page request and the time taken to create connection will be more. Creating a connection pool is an ideal approach for a complicated servlet. With a connection pool, we can duplicate only the resources we need to duplicate rather than the entire servlet. A connection pool can also intelligently manage the size of the pool and make sure each connection remains valid. A number of connection pool packages are currently available. Some like DbConnectionBroker are freely available from Java Exchange Works by creating an object that dispenses connections and connection Ids on request.The ConnectionPool class maintains a Hastable, using Connection objects as keys and Boolean values as stored values. The Boolean value indicates whether a connection is in use or not. A program calls getConnection( ) method of the ConnectionPool for getting Connection object it can use; it calls returnConnection( ) to give the connection back to the pool.

Q: Why should we go for interservlet communication?

A: Servlets running together in the same server communicate with each other in several ways.The three major reasons to use interservlet communication are:

a) Direct servlet manipulation - allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object)
b) Servlet reuse - allows the servlet to reuse the public methods of another servlet.
c) Servlet collaboration - requires to communicate with each other by sharing specific information (through method invocation)

Q: Is it possible to call servlet with parameters in the URL?

A: Yes. You can call a servlet with parameters in the syntax as (?Param1 = xxx || m2 = yyy).

Q: What is Servlet chaining?

A: Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request.In servlet chaining, one servlet’s output is piped to the next servlet’s input. This process continues until the last servlet is reached. Its output is then sent back to the client.

Q: How do servlets handle multiple simultaneous requests?

A: The server has multiple threads that are available to handle requests. When a request comes in, it is assigned to a thread, which calls a service method (for example: doGet(), doPost( ) and service( ) ) of the servlet. For this reason, a single servlet object can have its service methods called by many threads at once.

Q: What is the difference between TCP/IP and UDP?

A: TCP/IP is a two-way communication between the client and the server and it is a reliable and there is a confirmation regarding reaching the message to the destination. It is like a phone call. UDP is a one-way communication only between the client and the server and it is not a reliable and there is no confirmation regarding reaching the message to the destination. It is like a postal mail.

Q: What is Inet address?

A: Every computer connected to a network has an IP address. An IP address is a number that uniquely identifies each computer on the Net. An IP address is a 32-bit number.

Q: What is Domain Naming Service(DNS)?

A: It is very difficult to remember a set of numbers(IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters.For example, implies com is the domain name reserved for US commercial sites, moscom is the name of the company and www is the name of the specific computer, which is mascom’s server.

Q: What is URL?

A: URL stands for Uniform Resource Locator and it points to resource files on the Internet.URL has four components: http - protocol, Pentafour - IP address or host name, 80 - port number and index.html - file path.