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.6. Why to avoid storing bulky objects in session?Session objects are stored in server and it utilizes memory. So store only the needed information in sessions. Avoid bulky objects in session this can result in “out of memory error” resulting in a application crash.7. What are the advantages of using HTTP Session?Simple to use since it is managed in the server. It can function even when the cookie functionality is turned off in the browser.8. What are the disadvantages of HTTP Session?Since session objects are stored in the server the server memory required is more which is costly.9. What are the attribute scopes in servlet?Attribute scopes in servlet are:
- Application scope
- Session scope
- Request scope
10. Compare between different attribute scopes in servlet?
Application scope:
- It can be accessed by all servlet/JSP in the web application.
- It is available for the life time of the application.
- Achieved by storing in the ServletContext object.
Session scope:
- It can be accessed by all servlet/JSP in the web application.
- It is available for the life time of the session.
- Achieved by storing in the HTTPSession object.
Request scope:
- It is available to all servlet/JSP that have access to this specific request.
- Available for the life of the request that is until response send to the client.
- Achieved by storing in the HTTPServletRequest object.
No comments:
Post a Comment