Pr.Pg Next Pg

Servlet Interview Questions and Answers Part 5


41. List out difference between a JavaBean from a Servlet?

  • Servlets are Java based analog to CGI programs, implemented by means of a servlet container associated with an HTTP server.

  • Servlets run on the server side. Beans are reusable code components written in Java that one can use in a variety of programming environments.

  • JavaBeans are to Java what ActiveX controls are to Microsoft. Javabeans can run on server side, client side, within an applet etc.

  • So, both have nothing in common except Java.


42. Explain how servlets differ from RMI. Explain the advantages and disadvantages of each technology.

  • RMI (Remote Method Invocation) are a means of client server communication.

  • In this, the client invokes a method on the server machine and the server machine process returns the result back to the client. We need to run RMI registry to use RMI.

  • Servlets are used to extend the server side functionality of a website.

  • They communicate with various application on the server side and respond to the request made by the client.

  • Servlets are modules that run within the server and receive and respond to the requests made by the client.

  • Servlets retrieve most of the parameters using the input stream and send their responses using an output stream.



43. Define servlet mapping.

  • Servlet mapping controls how you access a servlet.

  • It is recommended that you donít use absolute URLs.

  • Instead usage of relative URLs should be done.

  • If you try to deploy the application with a different context root, you might have to change all the urls used in all the JSP programs.

  • Relative URLs is the solution so that you can deploy your application with different context root with out changing the URLs.


44. What is Java Servlet? 

  • A servlet is a java class / a program that is a server side component and runs in web container.

  • The servlet generates the dynamic content. The content is dependent on the client request. 


45. Explain the purpose of Servlet interface. 

  • The javax.servlet.HttpServlet / javax.servlet.Servlet is the interface that is to be implemented by all the servlets.

  • The servletís code / actions / implementation is done by implementing this interface.

  • The developers need to implement this interface when the application demands , or their application can not / chooses not to inherit from the GenericServlet or HttpServlet.

  • For instance the RMI / CORBA that simulates the servlets will need to directly implement this interface.

  • The life cycle methods , one init(),one or more service() and one destroy() will be implemented, and invoked by the network service.

  • In addition to the life cycle methods, this interface can be used to return information about itself such as version, author, copyright etc. 


46. Explain the underlying method of Servlet interface. 

  • The init() method

  • The service() method

  • The destroy() method

  • The getServletConfig()

  • The getServletInfo() method

1.init() method:

  • This method initializes the servlet.

  • This method invokes only once and automatically invoked by the servlet engine / container.

  • This method is used and guarantees the actions that are to be performed before the service() method is invoked.

  • This method is an overloaded method with no parameter and one ServletConfig object.

  • Incase of a fatal error during initialization, the init() method throws UnavailableException object.

2.service() method:

  • To carry a single request sent by a client, this method is used.

  • This method implements / performs the paradigm of request and response.

  • The request object contains the information of the service request including the client sent parameters.

  • The response object is used to send the response to the client.

  • The quality of handling these two methods rely on the underlying network environment.


  • Invoked just before the stopping the operations by the servlet.

  • It performs the resources cleaning process such memory, files, threads removal and ensures the persistence state is synchronized by the servletís current in-memory state.

  • The destroy() method invoked only once before the unloading the servlet.


  • This method is used to return the servlet config object.

  • This object contains the initialization parameters and startup configuration for this servlet.

  • This method returns the ServletConfig object to init() method and the init() method will store this object in order to return ServletConfig object if needed.


  • This method returns a string that contains the information of the servlet such as version, copyright, author.

  • This information is used as administrative tool and be displayed by the servlet engine.

  • This method returns this information in plain string form, as the information is servlet engine specific. 


47. What is GenericServlet class? 

  • This class implements Servlet and ServletConfig interfaces.

  • This class can be extended by a servlet. It is a common practice that a servlet class extends protocol-specific, such as HttpServlet class.

  • With the help of using life cycle methods, it makes simpler and easier to develop servlets applications.

  • To write a generic servlet, the developer need only to override the service() method. Additionally, the log method of ServletContext interface is also implemented by GenericServlet. 


48.What is HTTPServlet class? 

  • The HTTPServlet class provides an abstract class that is to be sub classed which is suitable for a web site.

  • The subclass of HTTPServlet class must override any one of the following methods:

  • doGet(), doPost(), doPut(), doDelete(), init(), destroy(), or getServletInfo().

  • The service() method overriding has almost no reason because the service() method handles the HTTP requests by handler methods of HTTP request type such as doxxxx() methods. 


49.What is Servlet context? 

  • Servlet context is created by the container and can be viewed as a shared memory resource for all the servlets for a web application.

  • A servlet context object can be placed in the memory and all other servlets can used this information.

  • For example log files information, dispatching requests etc.

  • Only one servlet context is available for one web application that uses servlets.

  • The web application environment information can also be passed to the init() method.

  • The web container instantiates the servlet context at the time of deploying a web application. 


50.Explain session tracking in Java Servlet. 

  • The state of requests for the same user is being maintained by the session of a servlet.

  • Session tracking is a mechanism that is tracked and maintained by such requests by the user.

  • These sessions are being shared by the servlets and accessed by a client.

  • This provides a convenience to the web applications that uses multiple servlets.

  • For example a shopping cart application uses the session tracking, which tracks the requests of the user for various mobile devices.

  • In java servlets this task is being maintained by:

  • Cookies, URL rewriting, session objects(HttpSession) and hidden fields.

Pr.Pg border                                              Next Pg

Warning: include_once(bottompad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-5.php on line 458

Warning: include_once() [function.include]: Failed opening 'bottompad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-5.php on line 458

Warning: include_once(rightpad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-5.php on line 464

Warning: include_once() [function.include]: Failed opening 'rightpad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-5.php on line 464