Servlet Request Session Application Scope Attributes

In this tutorial we will introduce about Servlet Request Session Application Scope Attributes. There are three different scopes: request scope, session scope and application scope. Attributes can be objects and are used to pass data between two requests. It is essential to pass data between different servlets in a web application. This is sixth post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.

Now we start to build the example for using servlet request session application scope attributes:

Project structure

Servlet Request Session Application Scope Attributes

Maven Dependency

Setting Servlet attributes: application Scope, session Scope, request Scope
Servlet Attributes can contain any object that you want to store. And Servlet attributes are used to pass data between two requests. There are all three different scopes of servlet attributes.

Request Scope: Request scope start from the moment an HTTP request accesses a servlet in web container and end when the servlet is done with delivering the HTTP response.
Session Scope: A session scope starts when a client (e.g. browser window) establishes connection with web application till the point where the browser window is closed.
Application Scope: Context scope or application scope starts from the point where a web application is put into service (started) till it is removed from service (shutdown) or the web application is reloaded. Attributes within the application scope will be available to all requests and sessions.

Here is an example of how you can get servlet attributes with request, session and application scope.

Deploy this example on application server
Here is the result when run this example.

Without setting attributes
URL: http://localhost:8080/servlet-attributes/get-attributes

Servlet Request Session Application Scope Attributes

Setting Servlet attributes
URL: http://localhost:8080/servlet-attributes/set-attributes
then forward to URL http://localhost:8080/servlet-attributes/get-attributes

Servlet Request Session Application Scope Attributes width=Servlet Request Session Application Scope Attributes

Refresh Servlet Attributes with URL http://localhost:8080/servlet-attributes/get-attributes

Servlet Request Session Application Scope Attributes

That’s all. You can also find other servlet example in the series of Java Servlet Example.

Download complete source code, click link below

Servlet-Attributes-Example.zip (51 downloads)

One thought on “Servlet Request Session Application Scope Attributes

Leave a Comment

*

Please share it if you found this useful
Hide Buttons