What are the differences between attributes and parameters?

In this post, you will learn:

  • What are Attributes?
  • Why are Attributes needed?
  • What are the differences between attributes and parameters?

In the last post, you were told that more than one servlet can participate in the processing of a request. In such a case participating servlets may need to share their processing result with each other. Attributes facilitate this sharing of intermediate state of servlets during request processing.

Differences between attributes and parameters:

1. Parameters are request data which are stored by the server in the ServletRequest object so that it can be read by the Servlets during processing.

Attributes are data which are stored by a Servlet in ServletRequest object so that it can be shared by others Servlets which are participating in the processing of the same request.

2. Parameters are only read by the Servlets i.e. Servlets can’t update or remove them. Attributes are added, removed, replaced and read by the Servlets.

3. Parameters are of String type. Attributes can be any type of objects.

4. Parameters are usually required in the processing of each request but attributes are required only when more than one Servlet participate in the processing of a request.

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.

  1. Document Verification
  2. Technical Interview
  3. HR Interview

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();

In addition to ServletRequest, two other interfaces of Servlet API, have these methods as you would come to know in later posts. In the next post we will cover practical example of RequestDispatcher.


Its time to recap the main concepts:

  1. Parameters are request data, which is stored by the server in the ServletRequest object.
  2. Attributes are data stored by the Servlets in the ServletRequest and two other Servlet API objects.
  3. Attributes are used by the Servlets to share data among them.
  4. ServletRequest and two other Servlet API interfaces provides methods for storing, reading and removing attributes.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *