• +91 9723535972
  • info@interviewmaterial.com

Servlet Interview Questions and Answers

Servlet Interview Questions and Answers

Question - 71 : - How do you configure a centralized error handler in servlets?

Answer - 71 : -

If we wish to include a central Error Handler for all the exceptions then we can define following error-page instead of defining separate error-page elements for an individual exception

  •    java.lang.Throwable
  •    /ErrorHandler

Question - 72 : - Explain the steps involved in placing a servlet within a package?

Answer - 72 : -

The Packages are used to separate the servlets under a directory to eradicate confusion among programmers working simultaneously on creating servlets on the same server. This can be done through the following steps:

  • Sorting of files into different subdirectories by matching the package name to the subdirectory name to organize the packages.
  • After sorting the packages into subdirectories insert the package statement in the class file.
Let us understand through this an example as follows:

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class servlet1 extends HttpServlet{
  5.       public void doGet(HttpServletRequest request, HttpServletResponse response)
  6.       throws ServletException, IOException {
  7.              response.setContentType("text/html");
  8.              PrintWriter out = response.getWriter();
  9.              String docType = "n";
  10.              out.println(docType + "n" + "Edurekan" + "n" + "");
  11.       }
  12. }

Question - 73 : -
How can you use a servlet to generate a plain text instead of HTML?

Answer - 73 : -

A servlet basically generates HTML. We can set the content-type response header by any of the methods. The example for producing text message EDUREKA using the HTML code inside the servlets.

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class EDUREKA extends HttpServlet{
  5.        public void doGet(HttpServletRequest request, HttpServletResponse response)
  6.        throws ServletException, IOException{
  7.               response.setContentType("text/html");
  8.               PrintWriter out = response.getWriter();
  9.               out.println("n" +"n" +"EDUREKAn" + "n" + "

    HAPPY LEARNING

      n" + "");
  10.        }
  11. }

Question - 74 : - How many objects of a servlet is created?

Answer - 74 : -

Only one object at the time of first request by servlet or web container.

Question - 75 : - What is the life-cycle of a servlet?

Answer - 75 : -

  • Servlet is loaded
  • servlet is instantiated
  • servlet is initialized
  • service the request
  • servlet is destroyed

Question - 76 : - What are the life-cycle methods for a servlet?

Answer - 76 : -

Method

Description

public void init(ServletConfig config)

It is invoked only once when first request comes for the servlet. It is used to initialize the servlet.

public void service(ServletRequest request,ServletResponse)throws ServletException,IOException

It is invoked at each request.The service() method is used to service the request.

public void destroy()

It is invoked only once when servlet is unloaded.

Question - 77 : - Who is responsible to create the object of servlet?

Answer - 77 : -

The web container or servlet container.

Question - 78 : -
What is difference between PrintWriter and ServletOutputStream?

Answer - 78 : -

PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.

Question - 79 : - What is difference between GenericServlet and HttpServlet?

Answer - 79 : -

The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.

Question - 80 : - What is servlet collaboration?

Answer - 80 : -

When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:

  • RequestDispacher interface
  • sendRedirect() method etc.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners