• +91 9723535972
  • info@interviewmaterial.com

Servlet Interview Questions and Answers

Servlet Interview Questions and Answers

Question - 51 : - How is an application exception handling is done using a Servlet?

Answer - 51 : -

The doGet() method and doPost() method throw the ServletException and IOException. The browser understands only HTML. Hence, when an exception os is thrown by an application, then, the servlet container processes the exception and generates an HTML response. Same happens with other errors such as error 404.

Servlet API supports customized Exception and Error Handler servlets that can be configured in the deployment descriptor, the purpose of these servlets is to handle the Exception thrown by application and send HTML response that is useful for the user. We can provide a link to the application home page or the details that let the user know what went wrong.

The Web.XML configuration is as follows:

    404
    /AppExceptionHandler
    javax.servlet.ServletException
    /AppExceptionHandler

Question - 52 : - What is Servlet Interface?

Answer - 52 : -

The central abstraction in the Servlet API is called as the Servlet Interface. All servlets in the Web Application implement this interface, either directly or, most commonly by extending a class that implements it.

Question - 53 : -
How to get the path of servlet in the server?

Answer - 53 : -

We can use the following code snippet to get the actual path of the servlet in the file system.

  • getServletContext().getServerInfo()

Question - 54 : - What do you mean by CGI and what are its drawbacks?

Answer - 54 : -

CGI is an abbreviation for Common Gate Interface. It consists of a set of code segments at the server-side using which the server interacts with the client running on the web. The drawbacks of CGI are as follows:

  • If there are multiple incoming requests, then the response generated will be very slow, which results in low efficiency.
  • CGI is platform dependent.

Question - 55 : - How do we translate JSP?

Answer - 55 : -

In a servlet, the Java code is written in HTML but JSP(Java Server Page) allows us to write Java code in HTML. JSP allows easy development of web pages and allows web designer and web developer to work independently. All JSP pages are translated into servlet and web container is responsible for translating JSP into the servlet.

Question - 56 : - Which exception is thrown if the servlet is not initialized properly?

Answer - 56 : -

The Servlet Exception or Unavailable Exception is thrown if the servlet is not initialized properly.

Question - 57 : - What do you mean by HttpServlet and how it is different from the GenericServlet?

Answer - 57 : -

HttpServlet is basically extended from GenericServlet and it also inherits the properties of Genericservlet. HttpServlet defines an HTTP protocol servlet while GenericServlet defines a generic, protocol-independent servlet.

Question - 58 : -
What is Pure Servlet?

Answer - 58 : -

Pure servlet is known as a servlet that is used to create java objects that can be implemented from javax.servlet.Servlet interface.

Question - 59 : -
Can you refresh servlet in client and server-side automatically?

Answer - 59 : -

Yes, There are a couple of primary ways in which a servlet can be automatically refreshed. One way is to add a “Refresh” response header containing the number of seconds after which a refresh should occur. The TestServlet class below shows an example of this.

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.util.Date;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7.  
  8. public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  9.       private static final long serialVersionUID = 1L;
  10.       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11.             performTask(request, response);
  12.       }
  13.       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
  14.       IOException {
  15.             performTask(request, response);
  16.       }
  17.       private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
  18.       IOException {
  19.             response.setContentType("text/html");
  20.             response.addHeader("Refresh", "5");
  21.             PrintWriter out = response.getWriter();
  22.             out.println("TestServlet says hi at " + new Date());
  23.       }
  24. }

Question - 60 : -
What is the difference between Context Parameter and Context Attribute?

Answer - 60 : - The main difference is, Context Parameter is a value stored in the deployment descriptor, which is the web.xml and is loaded during the deployment process. On the other hand, Context Attribute is the value which is set dynamically and can be used throughout the application.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners