java2db

Session attributes in servlet with example (Attribute scope)



Session Attributes are global attributes (They can access in all servlets/JSP in a web application) but they are specific to a browser window (Created browser window by request).

Session attributes in servlet
Session attributes in servlet Session attributes are the name (String) and value (Object) pairs.

Session attributes in servlet All servlets/JSPs can access the session attributes whether they may or may not participate in servlet chaining.

Session attributes in servlet Session attribute allocates memory in the HttpSession object.

Session attributes in servlet Session attributes are specific to a browser (browser scope) .

 

Session attributes in servlet Create session attributes :

syntax:

<HttpSession_Obj> . setAttribute(“<Attribute_Name>”,”<Attribute_Value>”) ;

Example:

HttpSession session = request.getSession();

session .setAttribute(“name”,”java2db”);

Attribute value must be an object . But we passed a String value , so it applies auto boxing .

 

Session attributes in servletModify session attributes :

Syntax :

<HttpSession_Obj> . setAttribute(“<Attribute_Name>”,”<New_Attribute_Value>”) ;

Example :

HttpSession session = request.getSession();

session .setAttribute(“name”,”java2db.com”);

 

Session attributes in servlet Remove session attributes :

Syntax :

<HttpSession_Obj> . removeAttribute(“<Attribute_Name>”) ;

Example :

HttpSession session = request.getSession();

session.removeAttribute(“name”,”java2db.com”);

 

Session attributes in servlet Access session attributes :

Syntax :

(Type)<HttpSession_Obj> . getAttribute(“<Attribute_Name>”) ;

Example :

HttpSession session = request.getSession();

String name = (String)session.getAttribute(“name”);

 

Session attributes in servlet with example Complete example for session attributes :

Session attributes in servlet with example Application Directory Structure :
The sendRedirect

Session attributes in servlet with example index.jsp

Session attributes in servlet with example SetAttributeServlet .java

Session attributes in servlet with example GetAttributeServlet .java

Session attributes in servlet with example IndividualTestServlet .java

Session attributes in servlet with example web.xml

 

Session attributes in servlet with example Execution Result :

Test URL : http://localhost:8080/SessionAttributes/index.jsp

create a servletChange the IP and Port according to your Web/Application server’s location IP and Port.

Enter the URL in the browser (Example : Firefox)  address bar and hit enter button.

Fill the details and click Submit button.

The sendRedirect

The response will display the details that what we entered.
The sendRedirect

 

 

Now , call the IndividualTestServlet  with it’s URL (http://localhost:8080/SessionAttributes/indServlet) in the same browser window (or in new tab of the same browser) and press enter button.  Even though IndividualTestServlet  not participated in the servlet chaining , it can access the session attributes.See the below screen.
The sendRedirect

 

 

Finally , enter the same URL (http://localhost:8080/SessionAttributes/indServlet) of IndividualTestServlet  in a different browser(Example: Google Chrome) . This time the response comes with null values. Because we created session attributes in one browser(Firefox)  and requesting from another browser(Google Chrome).
The sendRedirect




Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Request attributes in servlet with an example ( Attribute scope )
ServletConext Attributes in servlet ( Attribute scope )
The sendRedirect (servlets are in different web applications)
   [will not be published]



^ <