Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday 6 March 2018

Servlet Interview Questions and Answers

Servlet Interview Questions

Servlet Interview Questions

Here we are going to focus on some most useful java servlet interview questions and answers.

Let's start java j2ee interview questions on servlet one-by-one.


(1) What is Servlet?

Servlet is a technology and server-side programming language which is used to create dynamic web pages. For request, Servlet generates dynamic web pages as the response.


(2) What is the Servlet life-cycle in Java?

  • Loading Servlet : Servlet class loaded by servlet container.
  • Instantiation of Servlet : Object of servlet class created.
  • Initialization of Servlet : Initialization of servlet by calling init() method.
  • Calling service() method of Servlet : Calling service() method of servlet for service the client's request.
  • Calling destroy() method of Servlet : Destroy the instance of servlet by calling destroy() method.

(3) Servlet life-cycle methods in Java?

There are 3 life-cycle methods of Servlet.

  1. init(ServletConfig config) method.
  2. service(ServletRequest req, ServletResponse res)throws ServletException, IOException method.
  3. destroy() method.

(4) What is Container in Java?

Server container is the container which provides runtime environment to manage an application component developed according to API specification and to provide access to the J2EE API.


(5) What is the work of container?

There is some work which is performed by the container.
  • Life-cycle management of servlet.
  • Communication support.
  • Providing multithreading support for handling multiple requests at the same time.
  • Manage web.xml file.
  • Security support.

(6) When servlet object is created?

At the time of the first request, servlet object is created.

(7) How many objects of a servlet is created?

At the time of the first request, Only one object is created by the web container.


(8) What is the difference between get() and post() method?

Method get()
  • We can send the limited amount of data through get() method.
  • This method is not fully secured because data is exposed in the URL bar.
  • This method is faster than post() method.
  • This method allows bookmarking of the resources.
  • This method is not good important and confidential data like password, personal documents, etc.  
Method post()
  • We can large amount of data through post() method. There is no limitation.
  • This method is fully secured because data is not exposed in the URL bar.
  • This method is slower than get() method.
  • This method does not allow bookmarking of the resources.
  • This method is good for important and confidential data like password, personal documents, etc.

(9) Difference between GenericServlet and HttpServlet?

GenericServlet class is protocol independent i.e It can work other protocols just like HTTP.

HttpServlet is protocol dependent i.e It can work only HTTP protocol not other.


(10) When servlet is loaded?

When web container receives the first request from the client(web browser).


(11) What is the use of web.xml file in servlet?

Web.xml file is also known as deployment descriptor in a web application. Web.xml file contains all the information of web project components like servlet name, servlet class, URL pattern etc. The container receives all the information from this deployment descriptor file to execute particular servlet class for the given URL pattern.

We can map multiple servlets in a single web.xml file.


(12) What is ServletConfig?

  • ServletConfig is an interface.
  • There is one ServletConfig object per servlet.
  • Use it to pass deploy time information to servlet that you do not want to hardcode into the servlet.
  • Parameters are configured in the deployment descriptor. 

(13) What is ServletContext?

  • ServletContext is an interface.
  • There can be only one ServletContext object per web application.
  • Use it as kind of application bulletin board where you can put up messages that other part application can access.
  • Parameter is also configured in the deployment descriptor.

(14) Difference between ServletConfig and ServletContext?

One ServletConfig object per servlet in the web application project whereas one ServletContext object per web application.

(15) Why use welcome-file-list?

It is used to define welcome file for the project. We can define welcome-file-list in web.xml file.


(16) Why use load-on-startup in servlet?

The load-on-startup is used to fast response of request because it loads the servlet at the time of deploying the project or server start. We can define load-on-startup in web.xml file.

<load-on-startup>1</load-on-startup> 

We can pass negative value also.


(17) What is session tracking in servlet?

Session tracking is used to track a session of a particular interval of time. Session tracking is used to maintain the state of a user to recognize to the particular user.

(18) How many ways session tracking can be performed in servlet? 

There are 4 ways we can perform session tracking in java servlet.
  • Cookies
  • URL rewriting
  • Hidden form field
  • HttpSession

(19) What is a war file?

War file (web archive) file specifies all the web components. All the web component or elements(servlet, jsp, etc) compressed into a single file called war file and then we can deploy this war file to the server.


(20) What is Servlet Collaboration?

When one servlet connects with another servlet is known as servlet collaboration.


(21) What is RequestDispatcher?

RequestDispatcher is an interface in servlet and it is used to dispatch the request to another resource e.g html, servlet, jsp, etc.

(22) Difference between forward() and sendRedirect() method in servlet?

Method forward()
  • This method sends same requests to another resource.
  • This method works at the server side.
  • This method is faster than sendRedirect() method.
Method sendRedirect()
  • This method sends the new request.
  • This method works at the client side.
  • This method is slower than forward() method. 

(23) Can we call jsp from a servlet?

Yes.

(24) Can we define a constructor in servlet?

Yes.

(25) Can we call destroy() method inside init() method of servlet?

Yes.

(26) What is filter?

Filter is an object which is invoked either at the pre-processing or post-processing of the request.

Check out more java interview questions : 

OOPS interveiw questions in java
Java collection interview questions.
String programming interview questions in java.

The above all the java servlet interview questions and answers are quite useful for both fresher and experienced.

Share:

13 comments:

  1. Great post fir java servlet interview questions

    ReplyDelete
    Replies
    1. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. javatutorial blog is very informative.. best blog..
    java course

    ReplyDelete
  3. Thanks for sharing this valuable information to our vision Microservices Online Training

    ReplyDelete
  4. "very nice and amazing blog you have written i have read all this blog many things mentioned in this blog please keep updating the blog and posting because Dot net is a very easy and simple language. it is easy to understand and use and also support multiple languages.
    Dot Net training in Chandigarh
    Data Science training Chandigarh
    Big data training Mohali"

    ReplyDelete
  5. I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete
  6. I should assert barely that its astounding! The blog is informational also always fabricate amazing entitys. live crypto ticker

    ReplyDelete
  7. Thanks, this is generally helpful.
    Still, I followed step-by-step your method in this Java online training
    Java online course

    ReplyDelete
  8. This is a great blog. I am pretty much impressed with your good work. You put really very helpful information. Keep it up
    hr Payroll in velachery

    HR Payroll in chennai

    ReplyDelete
  9. This is really a helpful blog I am really impressed with your work, keep it up the good work
    Oracle Java Certifications

    ReplyDelete
  10. Blog has covered good interview questions and answers and helped me to find Delhi metro job vacancy for 12th pass according to my profile on Job vacancy result with 100% job assistance in some easy steps, keep sharing more informative blog with us.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate