Servlet Attribute

In this tutorial you will learn about the Servlet Attribute and its application with practical example.

Servlet Attribute

An attribute in servlet is an object that is used to share information in a web application. Attributes is also allow user to share information among the Servlet themselves, so that we can reuse the same object again and again. It can be SET and GET one of the following scope:

Table Of Contents

Request
Session
Application

Servlet Attribute Methods

public void setAttibute(String name, Object object):- This method is used to set the attribute.

public void getAttribut(String name):- This method is used to get the value of given attribute.

public void removeAttribute(String name):- This method is used to remove the attribute with the given name of attribute.

Example:-

Let’s understand this thing by an example, we have to create Two servlet file. From first We will set the attribute and in second we will get that attribute by it’s parameter.

ServletContextAtt1

ServletContextAtt2

Output:-

Output of above java files will be like two images.

1. Output of first servlet will be like below image. Here we have set the attribute.

image001

2. Output of the second servlet will be like below image.In which we used the getAttribute() method.

image003

 

In this tutorial we have learn about the Servlet Attribute and its application with practical example. I hope you will like this tutorial.