Showing posts with label Advanced Servlet Interview Questions and Answers. Show all posts
Showing posts with label Advanced Servlet Interview Questions and Answers. Show all posts

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.

Thursday, October 15, 2015

Advanced Servlet Interview Questions and Answers

1. How to set values in HTTP Session?
We can set values in session object using setAttribute() method of the HTTPSession interface.
HTTPSession session = request.getSession();
session.setAttribute(String attributeName, Object value);
Let's take a web mail application where user Name and location are stored.
HTTPSession session = request.getSession();
session.setAttribute(“User”,”Tim”);
session.setAttribute(“Location”,”Bangalore”);
2. How to retrieve attributes from session?
We can retrieve attributes from the session object using getAttribute() method of the HTTPSession interface.
HTTPSession session = request.getSession();
Object var = session.getAttribute(String attributeName);
Taking a web mail application where user Name and location are stored and retrieving the values.
HTTPSession session = request.getSession();
String userName = (String)session.getAttribute(“User”);
String location = (String)session.getAttribute(“Location”);
3. How to remove attributes from session?
Values can be removed from the session object using removeAttribute() method of the HTTPSession interface.
HTTPSession session = request.getSession();
session.removeAttribute(String attributeName);
Example: Taking a web mail application where user Name and location are stored. Assume we need to remove the userName attribute “Tim” from the session, the following API needs to be fired on HTTP request object.
HTTPSession session = request.getSession();
session.removeAttribute(“User”);
4. How to invalidate a session?
Session invalidating is the process of unbinding the session object from the user thereby removing all the previously stored data in the session and freeing the memory. To invalidate a session we call the invalidate() method.
5. When session invalidate() method is used?
This is typically used when user logs off from a web application to free up the memory utilized by the session object.
HTTPSession session = request.getSession();
Session.invalidate();