Basic Servlet Attributes Thread Safety Interview Questions Answers Explained

Servlet Attributes Thread Safety  

An attribute is used to communicate some value from one servlet to another. Depending upon the scope of the attribute we can access the attributes at parts of the application.An attribute can be set at either of the scopes as follows :

1.Application

2.Session

3.Request

 

Now in order to find out which attribute/s is/are thread safe,we can ponder upon with following discussion.

1.Application Attributes : An application attribute is a value set at application level. The code to set the attribute at application level is :

getServletContext().setAttribute(“name”,”value”);

As these attributes are accessible throughout the application,any servlet or any other web entity can update or get these attributes from various parts of the application. Since application attributes can be accessed or modified by any servlet in the web application,these are not thread safe.

 

2.Session Attributes : A session attribute is a value set at session scope. Session is used to retain user specific data. The code to set the attribute at application level is :

req.getSession().setAttribute(“name”,”value”);

When we talk about thread safety of session attributes,at one glimpse it deceives to appear as thread safe. In cases where we have the new tab feature in the modern browsers,the same session for the web application is opened in the new tab. In such case, if session attribute are updated from one tab will be reflected in the another tab with the same session. Thus we can say that session attributes are also not thread safe.

3.Request Attributes : A request attribute is a value set at request scope.he code to set the attribute at application scope is :

req.setAttribute(“name”,”value”);

As we know that each request is a unique object , whatever attribute we set at request scope cant be updated or get from another request. Thus request attributes are thread safe.

Conclusion : Thus request attributes are thread safe and application and session attributes are not.

Number of View :1126

Tags: , , , , , , , , ,

  1. No comments yet.

SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache