Pr.Pg Next Pg

Servlet Interview Questions and Answers Part 3

 

21. Explain the life cycle of Servlet.

  • Loaded(by the container for first request or on start up if config file suggests load-on-startup), initialized( using init()), service(service() or doGet() or doPost()..), destroy(destroy()) and unloaded.

 

22. When is the servlet instance created in the life cycle of servlet? What is the importance of configuring a servlet?

  • An instance of servlet is created when the servlet is loaded for the first time in the container. Init() method is used to configure this servlet instance.

  • This method is called only once in the life time of a servlet, hence it makes sense to write all those configuration details about a servlet which are required for the whole life of a servlet in this method.

 

23. Why don't we write a constructor in a servlet?

  • Container writes a no argument constructor for our servlet.

 

24. When we don't write any constructor for the servlet, how does container create an instance of servlet?

  • Container creates instance of servlet by calling Class.forName(className).newInstance().

 

25. Once the destroy() method is called by the container, will the servlet be immediately destroyed? What happens to the tasks(threads) that the servlet might be executing at that time?

  • Yes, but Before calling the destroy() method, the servlet container waits for the remaining threads that are executing the servletís service() method to finish.

 

26. What is the difference between callling a RequestDispatcher using ServletRequest and ServletContext?

  • We can give relative URL when we use ServletRequest and not while using ServletContext.

 

27. Why is it that we can't give relative URL's when using ServletContext.getRequestDispatcher() when we can use the same while calling ServletRequest.getRequestDispatcher()?

  • Since ServletRequest has the current request path to evaluae the relative path while ServletContext does not.

 

28. What is Servlets and explain the advantages of Servlet life cycle?

  • 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.

  • 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.

 

29. What are the general advantages and selling points of Servlets?

  • Servlets exhibit the following characteristics:

  • Can handle multiple requests concurrently.

  • Can synchronize requests can forward requests to other servers and servlets.

  • Portable (Servlets are written in the Java programming language and follow a standard API)

  • They are Secure (Like CGI, they donít run shell and memory overflow, etc is handled by Java) Inexpensive

  • (There are a number of free or very inexpensive Web servers that are good for development use or deployment of low- or medium-volume Web sites)

 

30.Whats the advantages using servlets over using CGI?

  • CGI programs run outside the webserver. So a new process must be started to execute a CGI program.

  • CGI programs are designed to handle a single request at a time.

  • After that they return the result to the web server and exit.

  • On the other hand servlets can handle multiple requests concurrently. They run within web servers.

  • They generate dynamic content that is easier to write and faster to run.


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\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-3.php on line 252

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\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-3.php on line 252

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

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\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-3.php on line 258