What is meant by Session tell me something about HttpSession?
A web client makes a request to a web server over HTTP. As long as a client interacts with the server through a browser on his or her machine,this interaction is called as session. HTTP is a stateless protocol. A client's each request is treated as a fresh one with no info of client to the server and the moment browser is closed, session is also closed. In an online shopping or web cart kind of application where session related information is critical for successive purchases made by a client, there have been suggested several techniques for session tracking in Java Servlets as mentioned below:
-Hidden form fields
-URL Rewriting
-Cookies
-HttpSession object
HttpSession is an interface, which belongs to javax.servlet.http. * package This provides a facility to identify a user across the several pages' requests by looking up the HttpSession object associated with the current request.
This is done by calling the getSession method of HttpServletRequest. If this returns null, you can create a new session, but this is so commonly done that there is an option to automatically create a new session if there isn't one already. Just pass true to getSession. Thus, your first step usually looks like this:
HttpSession session = request.getSession (true);
To ensure the session is properly maintained, this method must be called at least once before any output is written to the response.
You can add data to an HttpSession object with the putValue() method:
public void HttpSession.putValue(String name, Object value)
This method binds the specified object value under the specified name. Any existing binding with the same name is replaced.
To retrieve an object from a session, use getValue():
public Object HttpSession.getValue(String name)
This methods returns the object bound under the specified name or null if there is no binding.
You can also get the names of all of the objects bound to a session with getValueNames():
public String[] HttpSession.getValueNames()
This method returns an array that contains the names of all objects bound to this session or an empty (zero length) array if there are no bindings.
Finally, you can remove an object from a session with removeValue():
public void HttpSession.removeValue(String name)
This method removes the object bound to the specified name or does nothing if there is no binding. Each of these methods can throw a java.lang.IllegalStateException if the session being accessed is invalid.
More on session tracking in servlets...
-Hidden form fields
-URL Rewriting
-Cookies
-HttpSession object
HttpSession is an interface, which belongs to javax.servlet.http. * package This provides a facility to identify a user across the several pages' requests by looking up the HttpSession object associated with the current request.
This is done by calling the getSession method of HttpServletRequest. If this returns null, you can create a new session, but this is so commonly done that there is an option to automatically create a new session if there isn't one already. Just pass true to getSession. Thus, your first step usually looks like this:
HttpSession session = request.getSession (true);
To ensure the session is properly maintained, this method must be called at least once before any output is written to the response.
You can add data to an HttpSession object with the putValue() method:
public void HttpSession.putValue(String name, Object value)
This method binds the specified object value under the specified name. Any existing binding with the same name is replaced.
To retrieve an object from a session, use getValue():
public Object HttpSession.getValue(String name)
This methods returns the object bound under the specified name or null if there is no binding.
You can also get the names of all of the objects bound to a session with getValueNames():
public String[] HttpSession.getValueNames()
This method returns an array that contains the names of all objects bound to this session or an empty (zero length) array if there are no bindings.
Finally, you can remove an object from a session with removeValue():
public void HttpSession.removeValue(String name)
This method removes the object bound to the specified name or does nothing if there is no binding. Each of these methods can throw a java.lang.IllegalStateException if the session being accessed is invalid.
More on session tracking in servlets...