An attribute in servlet is an object that can be set, get or removed from one of the following scopes:
Application Scope ServletContext sc=getServletContext(); sc.setAttribute("user", "Mithilesh"); sc.getAttribute("user"); sc.removeAttribute("user"); Note: sc -context object user - attribute name Mithilesh -attribute value getAttribute-getting an attribute removeAttribute-remove attribute -------------------------------- Request Scope- -------------------------------- request.setAttribute("user","Mithilesh"); request.getAttribute("user"); request.removeAttribute("user"); Note request- ServletRequest object
There are following 4 attribute specific methods. They are as follows:
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","mithilesh"); //setting attribute on context scope } }
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(); //getting attribute from context scope String str = sc.getAttribute("user"); out.println("Welcome"+str); // Prints : Welcome Mithilesh } }
Differences between attributes and parameters:
Following analogy will make the difference clear between attributes and parameters. Lets say you went to XYZ company for walk-in. When you reached there, you were told that the selection process has three rounds.
For the first round, you presented you documents for verification. The verifier gave a number to your document file, wrote verified and signed on it.
This numbered file was sent to the technical interviewer, who called you according to your file number, took the interview and wrote the grade on the file and signed on it.
This file was sent to the HR manager for interview. This file contained two types of information. First, information provided by you through your documents. This information is read only, It is parameter. Second, information added by the verifier and technical interviewer so that it can be shared with the HR Manager. It is attribute. If only one person would be responsible for the selection, no attribute was required. I hope that now you would have understood the differences between the attributes and parameters well.
Now we should know how we can use attributes. Well, ServletRequest interface provides following methods for attributes.
setAttribute(): this method is used by a Servlet to set an attribute in request scope i.e in ServletRequest object. It has following syntax:
public void setAttribute(String name, Object value);
getAttribute(): this method is used by a servlet to obtain the value of an attribute from request scope. It has following syntax:
public Object getAttribute(String name);
getAttributeNames(): this method is used by a servlet to obtain the name of all request scope attributes. It has following syntax:
public Enumeration getAttributeNames();
removeAttribute(): this method is used by a servlet to remove a request scope attribute. It has following syntax:
public void removeAttribute();
The servletconfig object refers to the single servlet whereas servletcontext object refers to the whole web application.