1. This scope is maintained by ServletContext object
  2. This scope will start at the time of context object creation(i.e at the time of application deployment() and ends at the time of context object destruction(i.e at the time of application un deployment).
  3. The information stored in application scope is by default available to all components of application.

ServletContext interface defines the following methods to perform attribute management in application scope.

  1. public void setAttribute(String name,Object value)
  2. public Object getAttribute(String name)
  3. public void removeAttribute(String name)
  4. public Enumeration getAttributeNames()

Example:

Displaying all attributes present in the application scope.

Create servlet

package com.java2learn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContextAttributeDemo extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		ServletContext context = getServletContext();
		context.setAttribute("Java2Learn", "JAVA");
		Enumeration e = context.getAttributeNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			out.println(name + "---" + context.getAttribute(name));
		}
	}
}

Create web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>WebApp</display-name>

	<servlet>
		<servlet-name>ContextAttributeDemo</servlet-name>
		<servlet-class>com.java2learn.ContextAttributeDemo</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ContextAttributeDemo</servlet-name>
		<url-pattern>/contextAttributeDemo</url-pattern>
	</servlet-mapping>
</web-app>

Output:

applicationScope

Note:
context scoped attributes list never be empty.Web container will always add some default attributes like AnnotationProcessor,jsp_classpath etc..

Example:

Finding out number of online users.

package com.java2learn;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserCountDemo extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		ServletContext context = getServletContext();
		Integer count = (Integer) context.getAttribute("userCount");
		HttpSession session = req.getSession();
		if (session.isNew()) {
			if (count == null) {
				count = 1;
			} else {
				count++;
			}
			context.setAttribute("userCount", count);
		}
		out.println("no.of online users are" + "----" + count);
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>WebApp</display-name>

	<servlet>
		<servlet-name>UserCountDemo</servlet-name>
		<servlet-class>com.java2learn.UserCountDemo</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>UserCountDemo</servlet-name>
		<url-pattern>/userCountDemo</url-pattern>
	</servlet-mapping>
</web-app>

Output:
OnlineUsers

MultiThreading Issues

  1. Requested scoped attributes can be accessed only by current thread and other threads are not allowed to access,hence request scope attributes are always thread safe
  2. Session scoped attributes can be accessed by multiple threads of the same session simultaneously,hence these are not thread safe.
  3. Within the same session we can send multiple requests simultaneously by using “multiple windows”
  4. Context Scoped attributes can be accessed by multiple threads simultaneously,hence these are not thread safe.
  5. Instance and static variables of a Servlet can be accessed by multiple threads simultaneously,hence not thread safe
  6. For every thread a separate copy of local variables will be created and these can be accessed by only current thread.hence these are thread safe.
member is thread safe?
Request scoped attributes Yes
Session scoped attributes No
Application scoped attributes No
Instance variables No
Static variables No
Local variables Yes

Difference between Parameters and Attributes

Parameters Attributes
Parameters are read only i.e within the servlet we can access, but we are not allowed to modify Attributes are not read only i.e based on our requirement we can create a new attribute,we can remove existing attribute or we can modify the values
Parameters are deployed time constants Attributes are not constants we can modify their values
Parameters are key value pairs where both key and value are String objects only Attributes are also key value pairs where key is String but value can be any object type
At the time of retrieval not required to perform typecasting.We can assign it’s value directly to String Variable. At the time of retrieval we should perform typecasting.