devxlogo

Set Attributes in a Servlet Request

Set Attributes in a Servlet Request

Usually, you set attributes in a session and the attributes are available through out the session. If you want to limit the scope of an attribute to just a request, you can set the attributes on a request object. This is how:

 request.setAttribute("attributeName",attributeValue);attributeName - This is a string , the names starting with java., javax.,sun., com.sun. are reserved names.attributeValue - This is any object.

You can forward this request to other servlets or jsps using requestDispatcher and make the attributes available there.
You can get the attributes in the target servlet/jsp using:

 getAttributeNames();getAttribute("attributeName");

Thus, you can increase the functionality of your request object not just by limiting it to get parameters, which is a normal practice. This also helps in reducing the load on session.

devx-admin

Share the Post: