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
Maven Dependency
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.javabycode; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/set-attributes") public class SetAttributesServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Set application scoped attribute req.getServletContext().setAttribute("email", "email is application scoped attribute"); // Set session scoped attribute HttpSession session = req.getSession(); session.setAttribute("email", "email is session scoped attribute"); // set request scoped attribute req.setAttribute("email", "email is request scoped attribute"); // send redirect to other servlet req.getRequestDispatcher("get-attributes").forward(req, resp); } } |
Here is an example of how you can get servlet attributes with request, session and application scope.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javabycode; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/get-attributes") public class GetAttributesServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get application scoped attribute String emailappScope = (String) req.getServletContext().getAttribute("email"); // Get session scoped attribute HttpSession session = req.getSession(); String emailSessionScope = (String) session.getAttribute("email"); // Get request scoped attribute String emailRequestScope = (String) req.getAttribute("email"); // Print response on browser resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.write("<html><body>"); out.write("<h2>Display servlet attributes with request, session and application scope</h2>"); out.write("<p>applicationScope: " + emailappScope + "</p>"); out.write("<p>sessionScope: " + emailSessionScope + "</p>"); out.write("<p>requestScope: " + emailRequestScope + "</p>"); } } |
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
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
Refresh Servlet Attributes with URL http://localhost:8080/servlet-attributes/get-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 (327 downloads)
Thank you for the great tutorial.
It explains the servelet 3.0 api very well.