{"id":3428,"date":"2020-05-29T18:44:48","date_gmt":"2020-05-29T13:14:48","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3428"},"modified":"2020-12-14T15:00:25","modified_gmt":"2020-12-14T09:30:25","slug":"what-are-the-attributes-in-servlet","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-are-the-attributes-in-servlet\/","title":{"rendered":"What are the Attributes in Servlet?"},"content":{"rendered":"\n<p>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:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>request: This is used while processing the results of a submitted form.<\/li><li>Session: This is used to create the session by the Web Container when a user visits the web application.<\/li><li>Application: This scope persists until the application is deployed.<\/li><\/ol>\n\n\n<p>[box type=&#8221;note&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]<\/p>\n\n\n<\/p>\n<p><strong>Syntax to set an Attribute:<\/strong><\/p>\n<p>\n\n\n\n<\/p>\n<p>public void setAttribute(String name, Object obj)<\/p>\n<p>\n\n\n<p><\/p>\n<p>[\/box]<\/p>\n\n\n<p><strong>Methods used in Attributes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>public void setAttribute(String name, Object object): This method will <a href=\"https:\/\/www.h2kinfosys.com\/blog\/servlet-configuration-and-context\/\">set Attribute provided object<\/a> in the application scope.<\/li><li>public Object getAttribute(String name): This method will return the attribute for the specified name.<\/li><li>public Enumeration getInitParameterNames(): This will return the names of the context&#8217;s initialization parameters as an Enumeration of String objects.<\/li><li>public void removeAttribute(String name): This will remove the attribute with the given name from the servlet context.<\/li><\/ul>\n\n\n\n<p><strong>Request Scope:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Request Scope starts when an HTTP request hits the Servlet and ends when the Servlet sends an <a href=\"https:\/\/www.h2kinfosys.com\/blog\/servlet-request-and-response-objects\/\">HTTP response<\/a>.<\/li><li>Request Scope is denoted as javax.servlet.http.HttpServletRequest interface.<\/li><li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_container\" rel=\"nofollow noopener\" target=\"_blank\">Web Container<\/a> uses the request object as an argument of type HttpServletRequest.<\/li><\/ul>\n\n\n\n<p><strong>Session Scope:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Session scope starts when a connection is established between the user and the web application and remains until the browser is closed.<\/li><li>Session Scope is denoted as javax.servlet.http.HttpSession interface.<\/li><li>Session object can be retrieved using request.getSession().<\/li><\/ul>\n\n\n\n<p><strong>Application Scope:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Application scope starts when the browser is open and stops when the browser is closed.<\/li><li>Application Scope is denoted as javax.servlet.ServletContext interface.<\/li><li>Application object can be retrieved using getServletContext() directly or by making an explicit call to getServletConfig().getServletContext().<\/li><\/ul>\n\n\n\n<p><strong><em>Example to set an Attribute:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.io.*;\nimport javax.servlet.*;\nimport javax.servlet.http.*;\npublic class First extends HttpServlet {\n&nbsp;&nbsp;protected void doPost(HttpServletRequest request, HttpServletResponse response)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws ServletException, IOException {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.setContentType(\"text\/html;charset=UTF-8\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter out = response.getWriter();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ServletContext sc = getServletContext();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sc.setAttribute(\"user\",\"ABC\"); \/\/setting attribute on context scope\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Example to get an Attribute:&nbsp;<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.io.*;\nimport javax.servlet.*;\nimport javax.servlet.http.*;\npublic class Second extends&nbsp; HttpServlet {\n&nbsp;&nbsp;protected void&nbsp; doPost(HttpServletRequest request, HttpServletResponse response)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws ServletException, IOException {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.setContentType(\"text\/html;charset=UTF-8\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter out = response.getWriter();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ServletContext sc = getServletContext();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String str = sc.getAttribute(\"user\");&nbsp; \/\/getting attribute from context scope\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(\"Welcome\"+str);&nbsp; \/\/ Prints : Welcome ABC&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong><em>Example showing all the three scopes that is session, request and application in a single code:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import javax.servlet.ServletConfig;\nimport javax.servlet.ServletContext;\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport javax.servlet.http.HttpSession;\nimport java.io.IOException;\nimport java.io.PrintWriter;\n@WebServlet(\"\/get-attributes\")\npublic class GetAttributesServlet extends HttpServlet{\n&nbsp;&nbsp;&nbsp;&nbsp;protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get application scoped attribute\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String applicationScope = (String)req.getServletContext().getAttribute(\"a\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get session scoped attribute\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpSession session = req.getSession();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String sessionScope = (String)session.getAttribute(\"b\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ get request scoped attribute\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String requestScope = (String)req.getAttribute(\"c\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ print response\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp.setContentType(\"text\/html\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter out = resp.getWriter();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.write(\"&lt;html&gt;&lt;body&gt;\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.write(\"&lt;h2&gt;Servlet attributes example: applicationScope, sessionScope and requestScope&lt;\/h2&gt;\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.write(\"&lt;p&gt;applicationScope: \" + applicationScope + \"&lt;\/p&gt;\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.write(\"&lt;p&gt;sessionScope: \" + sessionScope + \"&lt;\/p&gt;\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.write(\"&lt;p&gt;requestScope: \" + requestScope + \"&lt;\/p&gt;\");\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3432,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[847,849,850,845,848,846],"class_list":["post-3428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-get-attribute","tag-http-request","tag-http-response","tag-java-attributes-in-servlet","tag-remove-attribute","tag-set-attribute"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3428"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3432"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}