Menu Close

servlet interview questions

What is a servlet?

  • Servlet is an interface
  • Servlet is a class.
  • Servlet works on server side.
  • Servlet provides dynamic response according to the request received by client.

What is deployment descriptor?

  • It is the configuration file for the web application with name is web.xml.
  • It resides in WEB-INF directory.
  • Servlet container uses xml file to configure web application servlets, servlet config params, context init params, filters, listeners, welcome pages and error handlers.

How can we simplify Web.xml file contents?

  • With servlet 3.0 annotations, we can remove a lot of clutter from web.xml by configuring servlets, filters, and listeners using annotations.

What is CGI?

  • CGI stands for Common Gateway Interface.
  • CGI is a set of programs written on the server-side.
  • CGI is used to interact with Web Server with a client running on a web server.
  • It takes the incoming request and for every new request, it starts a new process.

What are the life cycle methods of the Servlet?

  1. init ()
  2. service ()
  3. destroy ()

What is Request Dispatcher?

  • Request Dispatcher creates an object that is responsible to receive requests from the browser or client and then navigates them to any resources like Servlets, JSP, or HTML which resides at the server-side.

What are Request Dispatcher methods?

  1. Forward():
    • In the forward() method, the client sends the request to the Servlet1.
    • The Servlet1 processes the request and then forwards the request to Servlet2.
    • The servlet2 processes the request and generates a response which in turn is sent back to the client as the final response.
  2. Include():
    • In include() method, the client sends the request to the Servlet1.
    • The Servlet1 processes the request and then includes the request and sends the request to Servlet2.
    • The servlet2 processes the request and again sends it back to Servlet1 and
    • The Servlet1 generates a response which in turn is sent back to the client as the final response.

What is WAR file?

  • A WAR file is basically referred to as a Web Archived file.
  • WAR contains all the files of your application like XML, servlets, JSP, HTML, configuration files.
  • WAR is used to deploy the application easily.
  • It is advisable to use a WAR file for deployment.

Servlet package names are?

  1. javax.servlet
  2. javax.servlet.http

Differentiate Static and Dynamic webpage?

  • The web page which is providing common information to all users is called Static page.
  • The web page which is dynamically generated based on the user’s request known as dynamic web page.
  • Servlet is mainly used for dynamic web pages.

When Servlet is loads?

  • When servlet container receives the first request from client.
  • Admin of the application manually loads the servlet.
  • When the web server gets started.

What is ServletConfig?

  • ServletConfig is an interface belongs to javax.servlet package.
  • It is used for passing the configuration parameters to the servlet.
  • Servlet container instantiates ServerConfig implicitly.

What is ServletContext?

  • Each web application has a common ServletContext.
  • All the servlets in the web application can access the ServletContext.
  • It has the web-application information & resources which are common and accessible to all the servlets present in the web application.

What is Web server and Application server?

  • A web server is responsible to handle HTTP request from the client.
  • Web server understands the HTTP language and runs on HTTP protocol.
  • Apache Tomcat Web Server is a server(container) that executes servlets and JSPs.
  • Application Servers provide additional features such as Enterprise JavaBeans support, JMS Messaging support, Transaction Management, etc.
  • So we can say that the Application server is a web server with additional functionalities to help developers with enterprise applications.

Differentiate GET() and POST() methods?

  • GET is a safe method where POST method is not.
  • We can send limited data with GET method.
  • We can set huge amount of information using POST method only.
  • GET method is not secure because data is exposed in the URL.
  • POST is secure because data is sent in request body and we can’t bookmark it.
  • GET is the default HTTP method whereas we need to specify method as POST to send request with POST method.
  • Hyperlinks in a page use GET method.

What is MIME Type?

  • MIME is the content type response header.
  • Server sends MIME type to client to let them know the kind of data it’s sending.
  • It helps client in rendering the data for user.
  • Some of the mostly used mime types are text/html, text/xml, application/xml etc.

Why Servlet instead of CGI?

  • Servlet technology was introduced to overcome the shortcomings of CGI technology.
  • Servlets utilize time and memory efficiently compare to CGI.
  • Servlets use benefits of multithreading and for each request, a new thread is created, that is faster than loading creating new Object for each request with CGI.
  • Servlets and platform and system independent where as CGI is dependent.

Among PrintWriter or ServletOutputStream which is efficient?

  • PrintWriter is a character-stream class whereas ServletOutputStream is a byte-stream class.
  • We use PrintWriter to write character based information such as character array and String to the response.
  • We use ServletOutputStream to write byte array data to the response.
  • We use ServletResponse getWriter() to get the PrintWriter instance.
  • ServletResponse getOutputStream() method to get the ServletOutputStream object reference.

What is the difference between GenericServlet and HttpServlet?

  • GenericServlet is protocol independent implementation of Servlet interface.
  • HttpServlet is HTTP protocol specific implementation.
  • Most of the times we use servlet for creating web application and that’s why we extend HttpServlet class.
  • HttpServlet class extends GenericServlet and also provides some other methods specific to HTTP protocol.

Why HttpServlet is abstract?

  • HttpServlet class provides HTTP protocol implementation of servlet.
  • HttpServlet not implemented service methods such as doGet() and doPost().
  •  We need to implemeent at least one of the service methods.

What are the phases of servlet life cycle?

  • There are four phases
    • Servlet loading
    • Servlet initialization
    • Request handling
    • Service removal

What is URL Rewriting?

  • We can use HttpSession for session management in servlets.
  • HttpSession uses Cookies.
  • If we disable the cookie in client browser then Servlet API can take the support for URL rewriting to manage session.

How can we encode URL?

  • We can encode URL with HttpServletResponse encodeURL() method and if we have to redirect the request to another resource and we want to provide session information, we can use encodeRedirectURL() method.

What is the importance of cookies?

  • Cookies are used a lot in web based communication.
  • Cookies are text data sent by server to the client and it gets saved at the client local machine.
  •  HttpServletRequest getCookies() method is provided to get the array of Cookies from the request.

How can we add cookie at client side?

  • HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in the response header.
  • We cannot read a particular cookie hence there are no getter methods for a cookie.

How filters work in servlets?

  • Servlet Filters are pluggable java components that we can use to intercept and process requests before they are sent to servlets and response after servlet code is finished and before container sends the response back to the client.

Where we use filters?

  • Logging request parameters to log files.
  • Authentication request for resources.
  • Formatting of request body or header before sending it to servlet.
  • Alter response by adding some cookies, header information etc.

Explain Servlet listeners?

  • We know that using ServletContext, we can create an attribute with application scope that all other servlets can access.
  • We can initialize ServletContext init parameters as String only in the deployment descriptor (web.xml).