Assume session is an HttpSession, and is not referenced anywhere else in Servlet

Given that a web application consists of two HttpServlet classes, ServletA and ServletB, and the
ServerletA.service method:
20. String key = “com.example.data”;
21. session.setAttribute(key, “Hello”);
22. object value = session.getAttribute(key);
23.
Assume session is an HttpSession, and is not referenced anywhere else in ServletA.
Which two changes, taken together, ensure that value is equal to “Hello” on line 23? (Choose two)

Given that a web application consists of two HttpServlet classes, ServletA and ServletB, and the
ServerletA.service method:
20. String key = “com.example.data”;
21. session.setAttribute(key, “Hello”);
22. object value = session.getAttribute(key);
23.
Assume session is an HttpSession, and is not referenced anywhere else in ServletA.
Which two changes, taken together, ensure that value is equal to “Hello” on line 23? (Choose two)

A.
ensure that the ServletB.service method is synchronized

B.
ensure that the ServletA.service method is synchronized

C.
ensure that ServletB synchronizes on the session object when setting session attributes

D.
enclose lines 21-22 in synchronized block:
synchronized(this) (
session.setAttribute(key, “Hello”);
value = session.getAttribute(key);
)

E.
enclose lines 21-22 in synchronized block:
synchronized(session) (
session.setAttribute(key, “Hello”);
value = session.getAttribute(key);
)



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Rudi

Rudi

I don’t know what to answer…
Somebody can Help ?

Sam

Sam

C+E young men

giuseppe

giuseppe

C,E because the shared object is the session

Chandusha

Chandusha

only Request attributes and local variables are thread safe., while one servlet is updating an attribute,others can not update it.as session attributes are not thread safe.,to ensure the thread safety we should synchronize session object on the block of code that sets and gets the attributes.