In servlet we are getting the session Object in terms of HttpSession by calling getSession() method with the HttpServletRequest object.

What is a session ?
Session is a object which uniquely identifies the active user so that each activity of that user can be traced or identified internally with the session object. here in servlet we are achieving this by creating HttpSession object.

Important points regarding HttpSession

1. It is created by calling getSession() with HttpServletRequest object.
2. Its scope is till we call invalidate() method or set the timer by calling setMaxInactiveInterval(int val).
3. Session Object is handled by developer only, container is not responsible for creating and destroying.
4. Session uniquely identifies active users.
5. Every active is get stored in the session object of that user.
6. By creating HttpSession object a sessionID is generated by the container which fed in the client browser in the form of cookie with the first response after creating httpSession object.
7. SessionID is responsible to identify the particular request from a particular client machine.
8. SessionID is managed internally by the container only.

How to create Httpsession Object?
It is created by calling getSession() with HttpServletRequest object.

protected void service(HttpServletRequest req, HttpServletResponse res) {
    
             .....
    HttpSession session=req.getSession(); //Session Created

}

We are calling with the HttpServletRequest object because getSession() is defined in it.

How to destroy HttpSession object?
It can be destroyed by two ways :-

1. Explicitly by calling invalidate() with the HttpSession object.

HttpSession session=req.getSession(); //Session created
        .......
session.invalidate() // Session Destroyed

 2. Implicitly by calling setMaxInactiveInterval(int val) with HttpSession object. It works as the timer that means if a user will be inactive for the time specified as its parameter then the Session will be destroyed automatically.

HttpSession session=req.getSession(); //Session created
        .......
session.setMaxInactiveInterval(int val) //Time set for Inactive 
                                        period of active member. 

How to get the SessionID?
We can get the sessionID which is internally created by the container with the creation of HttpSession by calling getId() which is defined in the HttpSession interface as a abstract method and returns Id as a string. 


HttpSession session=req.getSession(); //Session created
        .......
String id = session.getId(); // Getting SessionID

How to set the values/list object in the Session object?
It can be done by using the setAttribute(String , Object) which stores the information in the key and value format.
1st parameter is string which acts as a key an can be any user-defined string.
2nd parameter is a object which acts a value which we want to store in the HttpSession object. 


HttpSession session=req.getSession(); //Session created
        .......
List<String> list= new ArrayList<String>();//list Object
list.add("JAVA");
list.add("C");
list.add("PHP");
session.setAttribute("NAME",list); //List object is stored in 
                                     the session with Key "NAME" 

Again list object can be retrieved from the Session object by calling setAttribute(String) with the HttpSession Object and passing the key as a parameter.

HttpSession session=req.getSession(); //Session created
        .......
List<String> = (List<String>)session.getAttribute("NAME");
                            //Retrieving list Object from 
                               Session object

Note: Need to typecast because getAttribute() returns Object class object.
          Session is used to uniquely identifying active user
          SessionID is used to uniquely identifying request coming from particular client machine. 



0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic