MY mENU


Friday, 16 March 2012

Servlet Interview Questions-Part2


Servlet webapplication Architecture?


Why is Servlet so popular?   

 Because servlet  are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web server.


How can you say servlet is a single instance & multiple threads component?
             When you give multiple request to a java class acting as servlet, the web-server/applications server creates only one object of servlet but multiple threads will be started on that object representing multiple requests. Due to this server is called single instance & multiple threads based server side java component.
                          
Why Generic Servlet is abstract class ?         
 GenericServlet class implements Servlet interface and it gives definition of all the methods present in Servlet interface except service() method. service() method is abstract in Generic Servlet class, that's why Generic Servlet is an abstract class.

 Why HttpServlet is abstract class?
                   (OR)
 All methods in HttpServlet is concrete then why HttpServlet is abstract class?
we can declare a class as abstract even though the class  contains all concrete methods(with implementations). HttpServelt is also has all implemented methods  but they do nothing i.e Java People provided default implementation , that is not usefull. And also doGet() and doPost()  methods are implemented to return an error message  as  “ GET not supported, or POST not supported”. Due to cause of this the HttpServlet extending class  must need to override at least one of  HttpServlet class   method and there he need to write actual business logic.

What happens if I call destroy() method from service() method?
 Servlet object will not be destroyed & logic of destroy() method executes as a ordinary method logic.
Conclusion :
 When events are raised on servlet object then automatically life cycle methods will execute. And by calling life cycle methods events will not be raised on the servlet object.

What happens if I call init() method in the service() method?
 init() method executes as a ordinary java method but servlet object will not be created.

Can we call destroy() method inside the init() method is yes what will happen?
 Yes we can call like this but  if we did’t not override this method(destroy()) container will call the default destroy() method and nothing will happen . after calling this if any we have override default() method then the code written inside is executed.

What is Idempotent Behavior?
                        (OR)
       doPost() is idempotent nature or not ?
 When request  is submitted from form before completion of the request processing , if another request comes from same form and same browser window logic placed in doGet() method executes two times this behavior is called Idempotent Behavior. Idempotent Behavior means allowing double submission and this is a bad behavior and it has to be prevented. So better not to keep sensitive business logic like Credit Cards processing and other database operations in doGet() method.
 doPost() method is not idempotent so it does not allows double submit ions. so it is recommended to place sensitive business logic in doPost() method.

When Servlet gets First request then what will happen ?
                    (or)
Why First Request processing will take more time with compare to other requests?
 When servlet gets 1st request , all of following steps will execute ……
 Step 1.    Web container/web server loads servlet class from  WEB-INF/classes of   deployed web-application & creates object for it.
            Class c = class.forName(“yourservletname”);
            c.newInstance();
step 2 .     Zero Argument Constructor will execute.
Step 3.     Servletconfig object will be created for servlet object
Step 4.     init(-) method executes.
Step 5:     service (-,-) method executes & response goes to browser.
from next request onwords (second request onwords) the above "step 1 to  4 “ will not occur directly "step 5" step will be execute. That's why first request will take more time.

No comments:

Post a Comment