Servlets Interview Q

  • What is a servlet?
Servlet is a Java Program which provides the implementation of servlet interface directly or indirectly.
  • What does a web server do?
A web server takes the client request and responds back to the client.
  • What are the important headers that are sent through HTTP Request Format? 
There are two important headers that are sent through HTTP Request Format. They are: 
  1. User Agent (gives the info about the browser we are using)
  2. Acceptance Language (specifies what language is acceptable to the client)
  • Describe a brief about status codes in HTTP Response Format?
HTTP guys have given some status codes to know the status of our request. 
100 - 199 ---> Information
200 - 299 ---> Success
300 - 399 ---> Redirect
400 - 499 ---> Requested Resource not available.
500 - 599 ---> Failed to execute the requested resource.
  • What does Content Type specifies in a servlet program?
This is the Header, that is sent by the Server to Client. Based on this Content Type client will display the output to the user.
  • What is the difference between "doGet()" and "doPost()" methods?
  •  Draw the folder structure of a web based project?

  • Why WEB-INF folder is called as a private folder?
WEB-INF is called as private folder, because the files in this folder are accessible only by the server. 
  •  What are the important classes and interfaces in Servlets?
  •  Describe Deployment descriptor?
In any web-based applications, we call "web.xml" file as deployment descriptor. This is because, when ever we deploy the project, server first searches for the web.xml file. Server reads the data in it and keeps in JVM.
  • What happens if the .class file is not present in the classes folder?
If the .class file is not present in the classes folder, server throws an exception "java.lang.classNotFound" exception.
  • What are the parameters that are supplied to service() method?
We supply two parameters to the service() method. They are Request Object and Response Object
  • When will we get the exception "illegal access specifier"
We get such exception when we does not declare the servlet as Public.
  • In how many ways we can write a servlet program?
We can write a servlet program in 3 ways. The following are the three ways:
  1. By implementing Servlet Interface
  2. By extending GenericServlet Class
  3. By extending HttpServlet Class
It is recommended to use HttpServlet, so that we can reduce the amount of code.
  • How can we remove hard coding in Servlet programming?
We can removing Hard coding in Servlet programming by using Servlet Context and Servlet Config. We can supply the values to Servlet Context and Servlet Config through deployment descriptor(web.xml file).
  • What happens when multiple clients try to access a single servlet?
When Multiple clients try to access one servlet, only one servlet object will be created and this object is shared between multiple clients.
  • What is Thread Pool? What is its use?
Thread Pool is a program that starts whenever a servlet is started. When a client sends the request to the server, servlet pics a thread from Thread Pool and create request and response objects. These two objects are supplied to service( ). After the server has finished its work, server will remove request and response objects and it returns the thread to the Thread Pool. By using Thread Pool, Multiple clients can access a single Servlet at a time.
  • Can we have constructor in a servlet?
Yes, we can have only default constructor. We cannot have parametrized constructor.
  • Why we cannot have parameterized constructor in Servlets?
Every servlet uses class.forName() to create servlet object. When we have a parameterized constructor, class.forName() will fail to create an object. It throws an exception java.lang.instantiation exception.
  • Can we call destroy() method from service()?
Yes, we can call. When we call destroy() from service(), every time server executes service() it will call destroy().
 
 

No comments:

Post a Comment