Servlets Tutorial Questions Answers

Servlets Tutorial Questions Answers

Servlets Tutorial
  1. What is Servlet?

    Servlet is a server-side Java program written as per Servlet API specifications invoked against a client request on the Web using HTTP protocol.

  2. Who executes a Servlet?

    Servlet is executed by a Web server.

  3. What is a Web server?

    Any server connected to Internet is known as Web server. Similarly, a Web client also. A Web server can execute any Web communication programs like Servlets, JSP, ASP, CGI/PERL etc.

  4. Then, what is a Web container?

    Web container is a software working on the Web server to execute Web related programs.

  5. What is Servlet container?

    A servlet container is a Web container which executes only Servlets. Likewise, a JSP container executes only JSP programs.

  6. What are the responsibilities of a Servlet container?
    1. 1. It is responsible to execute the servlet from loading stage to garbage collection.
    2. It must call the callback methods whenever required for the smooth execution of the Servlet like service() or doGet() etc.
    3. It must maintain the life cycle of a servlet by calling init(), service() and destroy() (known as life cycle methods) implicitly.
    4. To get a Servlet container load a Web server like Tomcat, Weblogic or WebSphere etc.
  7. What is difference between web server and application server?

    Web server, like Tomcat, executes only Web applications like Servlets, JSP, ASP or CGI/PERL. Application server executes applications of any sort like EJBs, JMS, Web services etc. Sometimes an application server, like Weblogic, may execute Web programs also like Servlets. Here, application server is also a Web server. A very small difference exists between them. To put it otherway, a Web server is an application server that executes only Web applications. Similarly, a database server executes a database.

  8. What is GenericServlet and HttpServlet?

    To write a servlet, the Programmer should extend either GenericServlet or HttpServlet.

  9. What is web.xml file and its importance?
    What is deployment descriptor?

    When a Servlet is loaded against a Web client request, the container creates a Servlet object, reads the data from web.xml file and as per the data, configures the Servlet.

    1. web.xml file is a configuration file where a Servlet’s configuration properties can be set.
    2. web.xml file is known as deployment descriptor as it describes the deployment particulars of a Servlet.
    3. Programmer can write the initialization parameters (properties specific for a servlet) in the web.xml file. Writing the data in
    4. Writing data in XML file is known as declarative programming which is very easier than writing in a text file and is mostly used now-a-days by many frameworks and tools.
    5. Mainly, Programmer uses web.xml file to give alias name to a servlet.
  10. What is HttpServletRequest and HttpServletResponse in servlets?

    HttpServletRequest and HttpServletResponse are two interfaces objects passed to service() method by container. HttpServletRequest job is to receive data from the client and HttpServletResponse job is to send data to client. Know more at HttpServletRequest and HttpServletResponse?

  11. How sendRedirect() is useful in servlets?

    sendRedirect(String file) is a method of HttpServletResponse used to send a file to client.

  12. What is <servlet-mapping> tag?

    It is an XML tag written in web.xml file. It is used to map the actual name of the servlet with alias name. Client is never given the actual name to invoke the servlet, instead given the alias name for security reasons. In the following entry, roses is the alias name for Validation servlet.

  13. What is the difference between GET and POST of method attribute of <form> tag?

    Observe the usage first.

    <form method="get" action="something">

    The data sent through using get is not secret as it can be seen in browser’s history by others. It is not same with POST. With GET, the maximum data can be sent is 1 KB only and with POST any amount of data.
    Read for more differences 12 Differences – GET vs POST – Which to prefer?.

  14. What is ServletConfig?

    For each Servlet under execution, a ServletConfig object is created by the Servlet container and is used by the Programmer to read the Servlet specific data declared (written) in web.xml in the form of tags.

    1. ServletConfig is an interface from javax.servlet package.
    2. The getServletConfig() method of GenericServlet returns an object of ServletConfig.
    3. The ServletConfig object created by the Web container for a specific Servlet cannot read other servlet’s init-param data.
    4. ServletConfig object is specific for a servlet. Other way to say, if 100 servlet objects are being executed in the container, 100 ServletConfig objects are also created implicitly used by the container to communicate with the servlet.
    5. When the container destroys the Servlet object, along with it its corresponding ServletConfig object also destroyed.
    6. Programmer can make use ServletConfig object to read tags of the Servlet.
    7. <init-param> tag data is known as initialization parameters and are used to initialize the Servlet with some special properties.
    8. The method of ServletConfig object used to read is "String getInitParameter(String)".
    9. The ServletConfig data is written within <servlet> tag.
  15. What is ServletContext?
    How to do inter-servlet communication?

    We know earlier, the ServletConfig object is specific (or local) to a single servlet and in contrary the ServletContext represents all the servlets. Other way to say, the ServletContext object is a global object that can be accessed by all the servlets under execution of an application (say, banking application). Remember, an application does not represent a single program. An application has a task and to achieve the task number of programs are required systematically integrated.

    1. ServletContext is an interface from javax.servlet package and its object can be obtained with getServletContext() method of ServletConfig.
    2. That is, if a servlet would like to create an object of ServletContext, it must come through ServletConfig object only as follows.

    3. A ServletContext object being global, any servlet can access the data or modify the data or place the data.
    4. The ServletContext object comes into existence when the container starts and destroyed when the container stops execution.
    5. The ServletContext object is used to communicate between all servlets (inter-servlet communication)
    6. Data can be placed with the ServletContext object with "void setAttribute(String, Object)" method and can be retrieved later with "Object getAttribute(String)" method defined in ServletContext itself.
    7. Another style of placing data is with <context-param> tag declared in web.xml file outside any <servlet> tag. Remember, that <init-param> is written within <servlet> tag.

  16. What is RequestDispatcher?
    1. RequestDispatcher is an interface from javax.servlet package.
      RequestDispatcher is used to communicate between two servlets (one-to-one) to exchange data in between.
    2. It comes with only two methods of include() and forward().
    3. Its object can be obtained either from ServletRequest or ServletContext with the method getRequestDispatcher().
  17. What is the difference between include() and forward() methods?

    Both are the methods defined in RequestDispatcher interface. They differ in their functionality (job). include() method gets the response of both the servlets (of calling and called) to the client and the forward() gets only that of called. Client gets the response from the same servlet he requested but in forward(), he gets from a different servlet and is transparent to client. It is clearly discussed in 16 differences between include and forward – Decide which when to use?

  18. What is the difference of obtaining RequestDispatcher object from ServletRequest and ServletContext?

    getRequestDispatcher() method exists in both the interfaces of ServletRequest and ServletContext that returns an object of RequestDispatcher. The RequestDispatcher object obtained differ very slightly in the way we give the address of the other servlet. It is clearly discussed in Difference between RequestDispatcher from ServletRequest and ServletContext

  19. What is stateless and connectionless protocol?

    HTTP protocol is a stateless protocol for the reason it does not maintain a database of client names vs responses delivered. It is so made to save hard disk space on the server.
    At the same time, it is also connectionless for the reason when the response is delivered, the connection established earlier is closed. If the same client would like to send another request, it should establish a new connection again. This is so developed to distribute equally the server time and resources to all the clients. More discussed in Stateless and Connectionless HTTP Protocol.

  20. What is the problem with stateless and connectionless protocol like HTTP?

    On a single connection, number of times data cannot be exchanged between Web client and Web server. This is an especially needed phenomenon in e-commerce (online business Web sites) using shopping carts.

  21. What is session?

    The interactive time between client and server within one connection is known as session.

  22. What is session tracking?
    What is session management?

    Preserving the data exchanged that can be used in the later part of the program is known as session tracking or session management.

  23. How many ways session management can be done in Servlets?

    There are 4 ways.

    1. Hidden fields (now-a-days, ignored due to increased network traffic and heavy processing on the server)
    2. URL rewriting (now-a-days, ignored due to increased network traffic and heavy processing on the server)
    3. Using HttpSession interface (used in an application other than e-commerce)
    4. Using Cookies (used especially in e-commerce applications)
  24. What is HttpSession?

    1. It is an interface from javax.servlet.http package.
    2. Overloaded getSession() method of HttpServletRequest returns an object of HttpSession.
    3. HttpSession interface is used by Programmer to maintain session tracking in an application like Hit count.
  25. What is a Cookie and how does Cookies work in Servlets?
    1. In servlets, Cookie is a class from javax.servlet.http package.
    2. A Cookie constructor can store two string values like item name Lux and quantity 10.
    3. Two important methods of Cookie are getName() and getValue() to retrieve the two string values.
    4. Cookie is created on server-side and sent to client for storage. The server can get back the cookies from the client at anytime.
    5. Another two methods used with cookie programming are addCookie() of HttpServletResponse and getCookies() of HttpServletRequest. Read a shopping cart example.
  26. What is life cycle of Servlet?

    Different states in which a servlet exists between its object creation (by container) and object garbage collection is known as life cycle.

  27. What are servlet life cycle methods?

    The life cycle includes three methods – init(), service() and destroy(). These three methods are known as life cycle methods and servlet should exist in one of these methods in its all the time of execution.

  28. How to use the Servlet life cycle methods?

    Programmer can create objects and initialize variables in init() method, use them in service() method and close the them in destroy() method. For example, open database connection (create Connection and Statement objects) in init(), use the connection in service() and close the connection in destroy() method. See a program on life cycle methods Life Cycle Example with JDBC.

  29. What is WAR file?

    WAR stands for Web Application ARchive file. It is simply a JAR file but contains only Web components like Servlets, JSP, web.xml file, Java Beans for database connections etc.

  30. What is SingleThreadModel in Servlets?

    Java is multithreaded and thus also Servlets. One servlet process loaded can honor number client requests where each client is treated as a separate thread. If you would like the servlet to honor only one client, then implement SingleThreadModel interface to your servlet signature. Know more on What is SingleThreadModel in Servlets?

  31. What is setContentType() method?

    It is a method of ServletResponse inherited by HttpServletResponse. Using this method, the servlet informs the client browser in what format the data sent by the server should be formatted. Fully discussed in response.setContentType() Example.

  32. What is MIME type?

    MIME stands for "Multipurpose Internet Mail Extensions", originally developed for Internet usage. Later the MIME types are used for different purposes by the Programmers like informing the client browser how to dispplay the data sent by server hereto. Know more at What is MIME type in Servlets?

  33. How the Servlet knows what MIME type is used by client?

    Use getMimeType() method defined in ServletContext interface to find what MIME type is used by client.

Pass your comments and suggestions to improve this Servlets Tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *