Sunday, October 18, 2015

Advanced Servlet Interview Questions and Answers

11. What is a servlet event listener?
Listeners are used to perform some logic on trigger of certain events. Listeners are java classes that is used to implement the event handling mechanism. Example for Listener:
  • Listener class can be used for auditing user login information’s.
  • Listener classes can be used to clean up session objects and free memory when user logs off.
12. Classify the type of events in an web application?
There are three kind of events:
  • Servlet Context Events: These are events triggered when there is a change in the ServletContext object.
  • HTTP Session Events: These are events triggered when there is a change in the Session object.
  • Servlet Request Events: These are events triggered when there is a change in the request object.
13. When are events triggered?
The events are triggered under two circumstances:
  • Lifecycle changes: If there is a lifecycle change on the object. For example, when session created (or) initialized (or) destroyed.
  • Attribute changes: Any attribute stored /changed in the session. For example, when a attribute “message” is stored in the session.
14. Who triggers the events?
Whenever one of the changes in lifecycle or attribute happens the events are triggered by the web container which can be captured by listeners to perform a logic.
15. What are the steps to develop a servlet listener?
  • Step 1: Create the listener class. Listener class is done by implementing the required servlet listener interface by a normal java class and overriding all the methods.
  • Step 2: Listener class is registered with the deployment descriptor file.
16. What is a servlet filter?
Servlet filter are programs that runs on the server which intercepts HTTP request/response for transformations/processing/filtering.
17. How does filter work?
Steps of operation of a servlet filter:
  • Step 1: Intercepts the HTTP request and preprocess the request.
  • Step 2: Invokes the requested resource with the preprocessed HTTP request.
  • Step 3: Intercepts the HTTP response and transforms it.
  • Step 4: The transformed HTTP response will be sent back to the client.
18. What are the usage of filters?
  • Used for authenticating all HTTP request coming to the server. Example: All requests to the application will be authenticated and authorized against stored user credentials.
  • All the HTTP requests can be logged and audited in a flat file or database for tracking users of a web application. Example: The user credentials can be logged as “Ron logged in at 8:00 PM and he transferred 10,000 dollars from the account.
  • To perform some formatting of HTTP response, say display a message in the header (or) bread crumbs for all the pages of the application. Example: All the HTML pages displayed in the client should have the navigation breadcrumb like “Home Page> Savings Account> Account Summary”
19. Name and define important filter interfaces?
  • Filter: Contains the methods for filtering the request/response.
  • FilterChain: Contains the methods for chaining the filters.
  • FilterConfig: It is an object used by a servlet container to pass information to a filter during initialization.
20. What are the steps to create a filter?
  • Step 1: Create a class that implements the filter interface.
  • Step 2: Implement the doFilter method with the pre/post processing logic.
  • Step 3: Invoke the doFilter method of the FilterChain object.
  • Step 4: Register the filter with the appropriate servlets.

No comments: