Posts Tagged ‘application’

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 :1571

Tags: , , , , , , , , ,

No Comments


Types Of Attributes In Servlet Servlet Scopes And Attributes Saving Data In A Servlet Attributes Interview Question

Types Of Attributes In Servlet

 

An attribute is an object used to pass some value from one servlet to another.An attribute in a servlet can be set at various scopes depending upon the visibility in the web application.As we know that there are three scopes at servlet level in the application.Theses scopes are :

1.Request

2.Session

3.Application

An attribute can be set on either of the scopes depending on the situation we want access the attribute value either at request,session or application level.

When we want the value of an attribute o be accessed at request level we use request attributes and likewise for session and application.

Types of attributes in servlet can be explained on the basis of scope it belongs to :

1.Request Attributes

2.Session attributes

3.Application Attributes

 

1.Request Attributes can be set or get as demonstrated below in the code :

req.setAttribute("name","value");
req.getAttribute("name");

2.Session Attributes  can be set or get as shown below :

req.getSession(false).setAttribute("name","value");
req.getSession(false).getAttribute("name");

3.Application Attributes can be set or get as below :

getServletContext().setAttribute("name","value");
getServletContext().getAttribute("name");

Now comes the question where to use what?

Request Attributes : If we want to retain or communicate  a value across servlets where request does not change,we use request attributes.This can be the situation where include or forward is happening between the servlets in a web application.In such cases where we want to communicate a value across servlets we can use request attributes which when set from one servlet can be get in other servlets.

Session Attributes : In cases where user specific data has to be retained or communicated, we use session attributes.We use session attributes when we do not want a user’s data to be interfered by other users.It should be noted that these attributes can be retrieved  even when redirect is happening provided the session is maintained .

 

Application Attributes : In cases where some value has to be set at application level and all the entities in the web application can read that value we use application attributes.

 

 

 

Number of View :980

Tags: , , , , , , , , , ,

No Comments



SetPageWidth
Content Protected Using Blog Protector By: PcDrome.

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