java2db

ServletConext Attributes in servlet ( Attribute scope )



ServletConext Attributes are global attributes of a web application. These are visible and accessible in all web resource programs(servlets/JSPs) of a web application.ServletConext Attributes are not specific to a request (Like request attributes)  or browser window (Like session attributes).

 

ServletConext Attributes in servlet
ServletConext Attributes in servlet ServletConext Attributes are also name (String) and value (Object) pairs.

ServletConext Attributes in servlet All servlets/JSPs can access the ServletConext Attributes whether they may or may not participate in servlet chaining.

ServletConext Attributes in servlet ServletConext Attributes allocates memory in the ServletContext object.

ServletConext Attributes in servlet ServletConext Attributes are condition less global attributes of a web application.

 

ServletConext Attributes in servlet Create ServletConext Attributes :

syntax:

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

Example:

ServletConext context= getServletConext();

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

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

 

Session attributes in servlet with exampleModify ServletConext Attributes :

Syntax :

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

Example :

ServletConext context= getServletConext();

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

 

ServletConext Attributes in servlet Remove ServletConext Attributes :

Syntax :

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

Example :

ServletConext context= getServletConext();

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

 

ServletConext Attributes in servlet Access ServletConext Attributes :

Syntax :

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

Example :

ServletConext context= getServletConext();

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

 

ServletConext Attributes in servlet Complete example for ServletConext Attributes :

ServletConext Attributes in servlet Application Directory Structure :

The sendRedirect

ServletConext Attributes in servlet index.jsp

ServletConext Attributes in servlet SetAttributeServlet .java

ServletConext Attributes in servlet GetAttributeServlet.java

ServletConext Attributes in servlet IndividualTestServlet.java

ServletConext Attributes in servlet web.xml

 

ServletConext Attributes in servlet Execution Result :

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

create a servlet Change 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 comes with all details .
The sendRedirect

 
Now , call the IndividualTestServlet with it’s URL (http://localhost:8080/ServletContextAttributes/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/ServletContextAttributes/indServlet) of IndividualTestServlet in a different browser(Example: Google Chrome) . Again the response comes with the data even though 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 )
Session attributes in servlet with example (Attribute scope)
The sendRedirect (servlets are in different web applications)
   [will not be published]



^ <