Methods used to bind objects on HttpSession instance & get objects - Servlets

Q.  Which methods are used to bind the objects on HttpSession instance and get the objects?
- Published on 22 Jul 15

a. setAttribute
b. getAttribute
c. Both A & B
d. None of the above

ANSWER: Both A & B


  • Nirja Shah   -Posted on 15 Dec 15
    - An attribute in servlet is an object that can be set, get or removed from one of the following scopes:
    1. request scope
    2. session scope
    3. application scope
    - The servlet programmer can pass information from one servlet to another using attributes. It is just like passing object from one class to another so that we can reuse the same object again and again.

    - There are following 4 attribute specific methods. They are as follows:
    1. public void setAttribute(String name,Object object): It sets the given object in the application scope.

    2. public Object getAttribute(String name):Returns the attribute for the specified name.

    3. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.

    4. public void removeAttribute(String name):Removes the attribute with the given name from the servlet context.

    - We can bind the objects on HttpSession instance and get the objects by using setAttribute and getAttribute methods.

Post your comment / Share knowledge

Enter the code shown above:
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)