TOP Servlet Interview Questions and Answers
What is Java Servlet?
Java servlet is used to provide secure access to the web based date. This can extend functionality present in the web servers. As it is platform and server independent, it is used for many purposes.
What is String Tokenizer?
A String Tokenizer is used to break the string into tokens and the token value is passed as an argument in the constructor.
What is HttpServlet and how it is different from GenericServlet?
HttpServlet extends from GenericServlet and inherits the properties of Genericservlet. HttpServlet defines a HTTP protocol servlet while GenericServlet defines a generic, protocol-independent servlet.
How to get the current HttpSession object?
GetSession method is used to get the current HttpSession object on HttpservletRequest.
What do you mean by Default initialization in Java Servlet?
This is one of the servlet initialization and it is initialized when it is called for the first time.
What is Servlet Invoker?
Servlet Invoker allows web application to dynamically register new servlet definitions with the servlet tag in the /WEB-INF/web.xml.
What is called Servlet mapping?
Servlet mapping maps URL patterns with the servlets. If there is a request from the client, servlet container decides on which application it needs to map.
What are all the protocols supported by HttpServlet?
HttpServlet supports HTTP and HTTPS protocol.
Which exception is thrown if servlet is not initialized properly?
Servlet Exception or Unavailable Exception is thrown if servlet is not initialized properly.
Who is responsible for writing a constructor?
Container is responsible for writing constructor without arguments in servlet
What are all the advantages of Servlet over CGI?
Following are the advantages of Servlet over CGI:
- Cannot be run in an individual process.
- Servlet stays in the memory while requests. For every CGI request, you must load and start a CGI program.
- web.xml conveniences
What are the different mode that servlets can be used?
Following are the modes that servlets can be used:
- Filter chains can be used to collect servlets together
- Support HTTP protocol
- Used for CGI based applications
- Dynamic generation of servlets
What are the uses of servlets?
Servlets are used to process and store data submitted by HTML form, dynamic content, handle multiple request concurrently and manage state information on top of stateless HTTP.
Whether we can get deadlock situation in Servlets?
Yes, it can be achieved by writing doGet method in doPost method and writing doPost method in doGet method.
What is the default HTTP method in the servlet?
Default method is GET method for HTTPservlet.
Whether thread can be used in Servlets?
Yes, Single thread can be used in servlets.
What exception should be thrown when servlet is not properly initialized?
Servlet exception or an Unavailable exception is thrown when it is not properly initialized.