Java Servlets Interview Questions & Answers

4 avg. rating (80% score) - 1 votes

Java Servlets Interview Questions & Answers

Experienced in Java Servlets? Then this is for you. Servlet technology is used to create web application (resides at server side and generates dynamic web page). Servlet technology is robust and scalable because of java language. Before Servlet, CGI (Common Gateway Interface) scripting language was popular as a server-side programming language. Servlets most commonly implement applications hosted on Web servers. Do search in wisdom jobs for Java Servlets job listings for full time and part time positions updated today. Check out interview questions page to get more information on the kind and level of questions you will come across during the interview. Wisdomjobs Java Servlets interview questions and answers provides you with complete guide and makes you prepared for winning job interview.

Java Servlets Interview Questions

Java Servlets Interview Questions
    1. Question 1. Can We Use The Constructor, Instead Of Init(), To Initialize Servlet?

      Answer :

      Yes. But you will not get the Servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

    2. Question 2. What Is Servlet Context?

      Answer :

      The Servlet context is an object that contains a information about the Web application and container. Using the context, a Servlet can log events, obtain URL references to resources, and set and store attributes that other Servlet in the context can use.

    3. Question 3. What Is A Servlet Filter?

      Answer :

      Servlet filters are pluggable Web components that allow us to implement pre-processing and post-processing logic in our Web applications.

    4. Question 4. What Is A War File?

      Answer :

      WAR stands for Web Archive. It is a compressed version of your web application. You can use this WAR file to deploy your web application.

    5. Question 5. How Would You Create Deadlock On Your Servlet?

      Answer :

      Calling a doPost() method inside doGet() and doGet()method inside doPost() wouleate a deadlock for a servlet.

    6. Question 6. Why Is Httpservlet Declared Abstract?

      Answer :

      1.The default implementations of the main service methods can not do anything and need to be overridden. This calls of the HttpServlet class to be declared as abstract.
      2.With its use the developers do not need to implement all the service methods.

    7. Question 7. Why Is A Constructor Needed In A Servlet Even If We Use The Init Method?

      Answer :

      1.Although the init method of the servlet initializes it, a constructor instantiates it.
      2.A developer might never explicitly call the servlet's constructor but a container uses it to create an instance of the servlet.

    8. Question 8. What Is Genericservlet Class?

      Answer :

      1.GenericServlet is an abstract class which implements the Servlet interface and the ServletConfig interface.
      2.Other than the methods included in above two interfaces, it also provides simple versions of the lifecycle methods init and destroy, and implements the log method declared in the ServletContext interface.
      3.Since this class is not specific to any protocol, it is known as generic servlet.

    9. Question 9. How Can The Session In Servlet Be Destroyed?

      Answer :

      There are two ways to destroy a session:
      1. Programatically : By using session.invalidate() method. It makes the container abandon the session on which the method is called.
      2. When the server shuts down.

    10. Question 10. What Is Lazy Loading?

      Answer :

      The servlets are not initialized by the container from the start. It happens when the servlet is requested for the first time. This is called lazy loading.

    11. Question 11. What Are The Mechanisms Used By A Servlet Container For Maintaining Session Information?

      Answer :

      For maintaining session information Servlet Container uses:
      . Cookies
      . URL rewriting
      . HTTPS protocol information

    12. Question 12. What Is The Procedure For Initializing A Servlet?

      Answer :

      - To initialize a servlet init() is used. 
      - init() initializes a java program.
      - A constructor can also be used to initialize a servlet.

    13. Question 13. What Is The Web Container?

      Answer :

      A Servlet and  JSP containers are collectively referred to as Web containers.

    14. Question 14. What Are The Uses Of Servletrequest?

      Answer :

      The ServletRequest gives information such as the names of the parameters passed by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. The input stream, ServletInputStream.

    15. Question 15. What Are The Uses Of Servletresponse Interface?

      Answer :

      ServletResponse allows the servlet to set the content length and MIME type of that response. It provides an output stream, ServletOutputStream and a Writer through which the servlet can send data.

    16. Question 16. How Http Servlet Handles Client Requests?

      Answer :

      An HTTP Servlet handles client requests through its service method. The service method supports standard HTTP client requests by dispatching each request to a method designed to handle that request.

    17. Question 17. What Is Pre Initialization Of A Servlet?

      Answer :

      A container doesn't initialize the servlets when it starts up. It initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the <load-on-startup> element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or pre initializing a servlet.

    18. Question 18. How Do Servlets Handle Multiple Simultaneous Requests?

      Answer :

      When a request comes in, the web server will start a new thread and the request is assigned to a thread, which calls a service method of the servlet.

    19. Question 19. What Is Servlet Chaining?

      Answer :

      Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet chaining, one servlet’s output is the input of next servlet. This process continues until the last servlet is reached. Its output is then sent back to the client. We are achieving Servlet Chaining with the help of RequestDispatcher.

    20. Question 20. How Will You Communicate From An Applet To Servlet?

      Answer :

      There are three ways to communicate from an applet to servlet and they are: HTTP Communication (Text-based and object-based) , Socket Communication and RMI Communication.

    21. Question 21. How Do You Communicate Between The Servlets?

      Answer :

       We can communicate between servlets by using RequestDespatcher interface and servlet chaining.

    22. Question 22. What Is The Difference Between Context Init Parameter And Servlet Init Parameter?

      Answer :

      Servlet init parameters are for a single servlet only. No body out side that servlet can access that. It is declared inside the <servlet> tag inside Deployment Descriptor, where as context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag <context-param> directly inside the <web-app> tag. The methods for accessing context init parameter is getServletContext ().getInitParamter (“name”) where as method for accessing servlet init parameter is getServletConfig ().getInitParamter (“name”);

    23. Question 23. What Are The Different Ways For Getting A Servlet Context?

      Answer :

      We will get ServletContext by calling getServletConfig ().getServletContext (). This is because a ServletConfig always hold a reference to ServletContext. By calling this.getServletContext () also we will get a ServletContext object.

    24. Question 24. What Is Http Tunneling?

      Answer :

      HTTP tunneling is used to encapsulate other protocols within the HTTP or HTTPS protocols. Normally the intranet is blocked by a firewall and the network is exposed to the outer world only through a specific Web server port, that listens for only HTTP requests. To use any other protocol, that by passes the firewall, the protocol is embedded in HTTP and send as HttpRequest.

    25. Question 25. What Are The Differences Between A Session And A Cookie?

      Answer :

      Session is stored in server but cookie stored in client. Session should work regardless of the settings on the client browser. There is no limit on the amount of data that can be stored on session. But it is limited in cookie. Session can store objects and cookies can store only strings. Cookies are faster than session.

    26. Question 26. Why Should We Go For Inter Servlet Communication?

      Answer :

      The three major reasons to use inter servlet communication are:
      a) Direct servlet manipulation - allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object)
      b) Servlet reuse - allows the servlet to reuse the public methods of another servlet.
      c) Servlet collaboration - requires to communicate with each other by sharing specific information (through method invocation).

    27. Question 27. What Is Client Side Refresh?

      Answer :

      The standard HTTP protocols ways of refreshing the page, which is normally supported by all browsers.
      <META HTTP-EQUIV="Refresh" CONTENT="5; URL=/servlet/MyServlet/">
      This will refresh the page in the browser automatically and loads the new data every 5 seconds.

    28. Question 28. What Is Server Side Push?

      Answer :

      Server Side push is useful when data needs to change regularly on the clients application or browser, without intervention from client. The mechanism used is, when client first connects to Server, then Server keeps the TCP/IP connection open.

    29. Question 29. What's The Servlet Interface?

      Answer :

      The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.

    30. Question 30. What Is The Difference Between Servletcontext And Servletconfig?

      Answer :

      The ServletConfig gives the information about the servlet initialization parameters. The servlet engine implements the ServletConfig interface in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet's init() method.
      The ServletContext gives information about the container. The ServletContext interface provides information to servlets regarding the environment in which they are running. It also provides standard way for servlets to write events to a log file.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Servlets Tutorial