Tuesday 3 September 2013

attributes in Servlets

attributes

Now we got one more thing that called attributes where you can store and retrieve data using attributes as well .
what is the difference between attributes and parameters( i.e initialization parameters)?
Ans: we have just seen that using parameters you can store some data and you can retrieve the data in your servlet code again im saying that in attributes you can store and retrieve the data.

1. what are the types of your attributes types are : Context attributes, Request attributes, Session attributes. This says nothing but about the life of the attributes. So Context attributes basically can be access through out the whole web application, where as Request attribute is only available for that particular request and Session attribute is valid until the session is finished so in that particular session only you can access this session attribute.
 Coming to parameters we have only two types of parameters: Context parameters and Servlet Init parameter.
So what are the methods by which we you can set value to an attribute: we have got the setAttribute(String, Object) method this is basically similar to a hashMap() where you put a value to a hashMap() using the key value pair so the key is nothing but the first parameter String and second parameter is nothing but value which is set to that attribute that is the actual object you want to store in the attribute. So basically on what object so you call this setAttribute() method whether you want to call Session object , Request object or Context object, basically you can set a value to the attributes using the key value functionality. Coming to parameters how do you set a value to initialization parameters, you can not dynamically set a value to the initialization parameters in your servlet code so this is one big difference you have to remember, you can only set the value to the initialization parameters ones that is only at the time of the web.xml , you can put in the value for that initialization parameters in the web.xml file. So if you have some attribute value which changes often say you want to set a value now and later on the value can change and you want to set another value in that attribute, so you can not do that using parameters, you can only do that using attributes because this is only one time process parameters at web.xml , at deployment time. But where as attribute you can get and set the values any number of times.

Return type of the attribute is Object, so basically you can store any type of the object in the attribute so you can set any type of the object and basically you can retrieve that object in the attribute. Where as the return type of the parameter is String, so you can store only string in a parameter, this is another big difference between attributes and parameters. If you want to store anything apart from String, you can not do that using initialization parameters even though you want to set that value only once through of the life of the servlet, you can not do that using parameters because return type is String, there is a object you want to store you have to do via attributes. And you can not do that using parameters.

So the method to get attribute or parameter, for attribute you use getAttribute() method, we can call either Context, Session or Request objects. Where as the parameters we use getInitParameter(String) method and passing the init parameter key name, servle context or servlet config object

Q: When do we go for attributes and parameters?
Ans:if u want to put in something which is only used once through out the application, that is a string you can use Init parameters. But there is something which can change frequently and which is not an objects you go for attributes


         attributes         parameters
types  Context, Request, Session Context, Servlet Init
Methods to set:  setAttributes(String, Object) We can not set init parameters
ReturnType  Object String
methods to get:  getAttribute(String) getInitParameter(String)

No comments:

Post a Comment