Servlet Interview Questions and Answers Part 11


101.Explain the Form Data Reading Methods available in Servlet?

  • Servlets handles form data parsing automatically the following 3 methods are used depending on the situation:

  • getParameter(): Returns the value of a request parameter as a String, or null if the parameter does not exist. request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. You should only use this method when you are sure the parameter has only one value.

  • getParameterValues(): Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

  • getParameterNames(): Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.


102.What is Exception Handling?

  • An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

  • The process of responding to the occurrence, during computation, of exceptions anomalous or exceptional events requiring special processing often changing the normal flow of program execution.

  • When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred.

  • Creating an exception object and handing it to the runtime system is called throwing an exception.

  • In servlet application when exception occurs in servlet the web container search for a configuration in web.xml for which type of exception to thrown.

  • You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.


103.Explain Finally blocking in try catch?

  • The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

  • But finally is useful for more than just exception handlingit allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break.

  • Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

  • In general, a try..catch..finally block looks like the following:



//Servlet Code


catch(Exceptiontype name)



catch(Exceptiontype name)







//finally code.



104. Different between Error and Exception


Sr No




An error is an irrecoverable condition occurring at runtime.

Exceptions are conditions that occur because of bad input.


Such as Out Of Memory error. These JVM errors and you can not repair them at runtime.

FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference.


Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable.

In most of the cases it is possible to recover from an exception probably by giving user a feedback for entering proper values etc.


Error in Java are unchecked

Exception in Java are checked and unchecked.



105. Different between SendRedirect and setHeader


Sr No




setHeader Sets a response header with the given name and value.

sendRedirect Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.


If the header had already been set, the new value overwrites the previous one.

sendRedirect cannot send any information except new URL to the browser.


setHeader contains two parameter name and value

sendRedirect contain one parameter as a URL location.



106.How to find Number of Hit on a Web Page using Servlet?

  • Below are the steps to be taken to implement a simple page hit counter which is based on Servlet Life Cycle:

  • Initialize a class level variable in init() method.

  • Increase global variable every time either doGet() or doPost() method is called.

  • If required, you can use a database table to store the value of global variable in destroy() method. This value can be read inside init() method when servlet would be initialized next time. This step is optional.

  • If you want to count only unique page hits with-in a session then you can use isNew() method to check if same page already have been hit with-in that session. This step is optional.

  • You can display value of the global counter to show total number of hits on your web site. This step is also optional.


107.What is Packaging?

  • Web applications use a standard directory structure defined in the Java EE specification. You can deploy a Web application as a collection of files that use this directory structure, known as exploded directory format, or as an archived file called a WAR file.

  • Packaging your Web application as part of an Enterprise application allows you to take advantage of the split development directory structure, which provides a number of benefits over the traditional single directory structure.

  • The WEB-INF directory contains the deployment descriptors for the Web application web.xml.

  • The WEB-INF directory is not part of the public document tree of the application. No file contained in the WEB-INF directory can be served directly to a client by the container.

  • However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream() method calls on the ServletContext or includes/forwards using the RequestDispatcher. Hence, if the application developer needs access, from servlet code, to application specific configuration information that should not be exposed directly to the Web client, the application developer may place it under this directory.


108.What is JDBC?

  • The JDBC (short for Java Database Connectivity) interface is a pure Java API used to execute SQL statements.

  • JDBC is an application programming interface (JDBC API) that defines a set of standard operations for interacting with relational database management systems (DBMSs).

  • The DBMSs may be located on a remote machine connected to the Internet. In order to access a database under a specific DBMS, for example, MS-SQL Server, one must have a driver for that DBMS and the driver must implement JDBC API. JDBC is a trademark name and not an acronym.


109.What are different types of Drivers available in JDBC?

  • Sun-Microsystm now acquired by Oracle has define four JDBC driver types:

  • JDBC-ODBC Bridge, plus ODBC driver

  • Native-API, partly Java driver

  • JDBC-net, pure Java driver

  • Native-protocol, pure Java driver.


110. What are the functions of the Servlet container?

  • The functions of the Servlet container are as follows:

  • Lifecycle management : It manages the life and death of a servlet, such as class loading, instantiation, initialization, service, and making servlet instances eligible for garbage collection.

  • Communication support : It handles the communication between the servlet and the Web server.

  • Multithreading support : It automatically creates a new thread for every servlet request received. When the Servlet service() method completes, the thread dies.

  • Declarative security : It manages the security inside the XML deployment descriptor file.

  • JSP support : The container is responsible for converting JSPs to servlets and for maintaining them.


Pr.Pg border                                             

Warning: include_once(bottompad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-11.php on line 517

Warning: include_once() [function.include]: Failed opening 'bottompad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-11.php on line 517

Warning: include_once(rightpad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-11.php on line 523

Warning: include_once() [function.include]: Failed opening 'rightpad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-11.php on line 523