Scope Objects

Collaborating web components share information by means of objects that are maintained as attributes of four scope objects. You access these attributes by using the getAttribute and setAttribute methods of the class representing the scope. The table below lists the scope objects.


Scope Object  Class  Accessible from 
Web context javax.servlet.ServletContext Web components within a web context.
Session javax.servlet.http.HttpSession Web components handling a request that belongs to the session.
Request Subtype of javax.servlet.ServletRequest Web components handling the request.
Page javax.servlet.jsp.JspContext The JSP page that creates the object.

Application Context

Application scope starts from the when a web application is started until stopped or the web application is reloaded. Parameters/attributes within the application scope will be available to all requests and sessions.


A session scope starts when a client (web browser) establishes connection until the browser is closed or timeout. Session scope share conversional state across multiple requests within the same client.


Request scope starts when an HTTP request sent and ends when servlet container deliver an HTTP response to client.

Page context

The PageContext is JSP specific so we won’t mention in this tutorial.

Step 1 Create New Dynamic Web Project

Step 1.1 Create New Project

Go to File->New->Others… Select Dynamic Web Project under Web category then click Next. Type ServletDemo or whatever name you want in the Project name field.

Step 1.2 Configure Target Runtime

  • Click New Runtime… button under Target Runtime.
  • Select Apache Tomcat v7.0. Click Next.
  • Click Browse button to specify Tomcat Server installation directory. The select the folder then click Ok.
  • Click Installed JREs… button to add/remove or edit JRE definition. Remove existing one; then click Add button to configure new one. Select Standard VM; then click Next. Click Directory button to select the root folder of JDK installation. Select the folder then click Ok. Click Finish.
  • Check the JDK then click Ok.
  • Change JRE to the JDK then click Finish.
  • Click Finish the finish the project creation.

Step 2 Create Servlet Class

  • Right click to the project, select New -> Servlet from the menu.
  • In Java package, enter
  • In Class Name, enter UsingScope.
  • Click Finish.
  • Implement doGet() method.

The code below will demonstrate how scoped variables work.

Step 3 Clean & Build

Clean & build the project to clean old classes file (if any) & force the IDE to recompile java code.

  • Go to Project on menu bar; select Clean…
  • Select Clean all projects (default)
  • Click Ok to proceed clean & build.

Step 4 Configure Tomcat

Select Servers window; in case it’s not there, go to Windows -> Show View-> Others… Select Server -> Servers; then click Ok.

If this is the first time you are creating a server, in the Servers area you will see a link as No servers are available click this link to create a new server. Click the link.

Define a New Server window will be opened, In Select the server type. Select Apache -> Tomcat v7.0 Server. Then click Finish.

Step 5 Deploy The Application

  • Right click to the Tomcat Server select Add and Remove.
  • Move the application (ServletDemo) from Available column to Configured. Then click on Finish to deploy it to tomcat server.

Step 6 Start Tomcat Server

Right click on the tomcat server and choose Start or simply click on the green start icon to start the server.

Step 7 Run The Servlet

We assume that you install tomcat on default port (8080) and project name is ServletDemo. Open web browser and type:

Run the first servlet



All attributes are same name but they are stored in different scopes & different values. All so the their lifetimes are different.

Note: If you’ve already created a dynamic web project & tomcat server up & running. You can skip step 1 – create new project. And to deploy & run the servlet; follow the below steps:

  • Stop the tomcat server
  • Clean & build the project to re-compile the application
  • Start the tomcat server
  • Open web browser and run the URL.