Tuesday 11 November 2008

Servlet, JSP


1. What are methods of Servlet and Why the servlet has init() method?Servlet is Basic interface that contains these methods
  1. init(ServletConfig)
  2. service(ServletRequest, ServletResponse)
  3. destroy()
  4. getServletConfig()
  5. getServletInfo()

Then implemented abstract classes are GenericServlet and HttpServlet.
Now constructor can not be define in interface, but init can have as skeleton in interface,
so any servlet using the interface knows that what need to be override to initialize a servlet.

Q. which pattern is this? Templating pattern

2. What is necessary to override init() method of Servlet?
There are no necessary conditions to override this particular method. In case you are overridding init(ServletConfig config), then make sure to call super.init(config).

3. Context Object pattern
A context object encapsulates web server specific HTTP information in more general and portable
form. It is used when:
  1. you have components and services that need access to the system information.You want to decouple application components and services from the protocol specifics of system information.
  2. you want to expose only relevant APIs within a context.
  3. Use a context object to encapsulate state in a protocol-independent way to be shared throughout your application.
4. Life cycle of servlet and jsp
Servlet : init(), service(), destroy()
JSP: translation, and above all

5. Difference between in static and dynamic include
  1. The syntax for static include is and the syntax for dynamic include is
  2. Static include is an inline inclusion. i.e., the contents of the file will be included at translation phase. It’s something like a copy and paste. In case of dynamic include the response of the file will be included to the original response at runtime. The file will be processed separately and only the response will become the part of the original files’ response.
  3. Static include cannot have a dynamic filename. This is because the servlet container needs the files for inclusion, at translation phase itself. 
  4. Static include cannot accept a parameter dynamic can.
  5. Static includes are faster than dynamic includes.
  6. In case if the resource lost occurs, container will throw 500(Internal Server Error) for static, though 404(Page Not Found Error) for dynamic include

6. Difference between forward and sendRedirect
  1. When you invoke a forward request, the request is sent to another resource on the server, without the client being informed. This process occurs completely within the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. 
  2. In case of sendRedirect browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. 
  3. Redirect is slower than forward
  4. forward is method of RequestDespatcher and sendRedirect is of request 
7. Inversion Of Control (IoC) and Dependency Injection (DI) are concepts generally associated with the Spring Framework.  The easiest way to think of Inversion Of Control is what Rod Johnson (founder of the Spring framework) calls the Hollywood Principal, "don't call us we'll call you:.  IoC uses interfaces to acquire and release resources a prime example being JDBC connections.  This also part of the reason Spring works so well with Object/Relational Mapping (ORM) tools such as Hibernate, iBatis, and TopLink.

DI is a more specific version of IoC.  DI pushes application dependencies at runtime.  DI comes in several flavors in Spring:
    1.  Setter Injection - uses Java Bean setters (and getters) to get dependencies
    2.  Contructor Injection - dependencies come from constructor arguments
    3.  Method Injection - where the container is used to implement dependencies


8. Explain Struts navigation flow


When we deploy our application in the server, at first the container reads the information from web.xml file.Here ActionServlet object will be created and init() of ActionServlet will be called.Here ActionServlet is the backbone to the whole application.
When client send a rewuest using .jsp extension , getters() and reset() of FormBean will be called. When client fill the form and press on submit button, then setters() and validate() will be called.
If the data is not valid ,then the form redirects to another page which is specified in struts-config.xml file. If the data is valid , then only Action class object will be created.

In Action class , have execute() which have return type of ActionForward. We can specify the business logic in Model and provide that object in execute(). After completion of business logic execution , then it forwards to another page ( either success or failure) , whichis specified in struts-config.xml file.
Action classes of struts uses Command pattern and overall as a web flow, it uses MVC pattern.