An object, which can be set, get, or removed, is referred to as an Attribute. Attributes in Servlet can get or set using any of the below scopes:
- request: This is used while processing the results of a submitted form.
- Session: This is used to create the session by the Web Container when a user visits the web application.
- Application: This scope persists until the application is deployed.
[box type=”note” align=”” class=”” width=””]
Syntax to set an Attribute:
public void setAttribute(String name, Object obj)
[/box]
Methods used in Attributes:
- public void setAttribute(String name, Object object): This method will set Attribute provided object in the application scope.
- public Object getAttribute(String name): This method will return the attribute for the specified name.
- public Enumeration getInitParameterNames(): This will return the names of the context’s initialization parameters as an Enumeration of String objects.
- public void removeAttribute(String name): This will remove the attribute with the given name from the servlet context.
Request Scope:
- Request Scope starts when an HTTP request hits the Servlet and ends when the Servlet sends an HTTP response.
- Request Scope is denoted as javax.servlet.http.HttpServletRequest interface.
- Web Container uses the request object as an argument of type HttpServletRequest.
Session Scope:
- Session scope starts when a connection is established between the user and the web application and remains until the browser is closed.
- Session Scope is denoted as javax.servlet.http.HttpSession interface.
- Session object can be retrieved using request.getSession().
Application Scope:
- Application scope starts when the browser is open and stops when the browser is closed.
- Application Scope is denoted as javax.servlet.ServletContext interface.
- Application object can be retrieved using getServletContext() directly or by making an explicit call to getServletConfig().getServletContext().
Example to set an Attribute:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class First extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); ServletContext sc = getServletContext(); sc.setAttribute("user","ABC"); //setting attribute on context scope } }
Example to get an Attribute:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Second extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); ServletContext sc = getServletContext(); String str = sc.getAttribute("user"); //getting attribute from context scope out.println("Welcome"+str); // Prints : Welcome ABC } }
Example showing all the three scopes that is session, request and application in a single code:
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 applicationScope = (String)req.getServletContext().getAttribute("a"); // get session scoped attribute HttpSession session = req.getSession(); String sessionScope = (String)session.getAttribute("b"); // get request scoped attribute String requestScope = (String)req.getAttribute("c"); // print response resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.write("<html><body>"); out.write("<h2>Servlet attributes example: applicationScope, sessionScope and requestScope</h2>"); out.write("<p>applicationScope: " + applicationScope + "</p>"); out.write("<p>sessionScope: " + sessionScope + "</p>"); out.write("<p>requestScope: " + requestScope + "</p>"); } }