Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Enthuware Mock Exams Qs

 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the follwing code:

//in file UserData.java
public class UserData
{
...data fields and methods...
}

//In file LoginServlet.java
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
{
UserData userdata = loginUser(req);
req.getSession().setAttribute("userdata", userdata); //1
}
}

You need the UserData object, which is added to the session, to know whenever it is added to the session. What is the right way to accomplish this?


Options

Select 1 correct option.

1. Make the class UserData implement HttpSessionBindingListener


2. Make the class UserData implement HttpSessionListener
This interface is used when you are interested in session creation and destruction.

3. call some method on UserData object after //1.
Looks promising but you would have to call it in all the places. And this would be nonstandard too.

4. Make the class UserData implement HttpSessionAttributesListener


5. This cannot be done!



As per my understabding of qs the ans will be 1 and 4 , as we can acheive the desired resultwith both of these listener.

But by the site the ans is only 1 , so what will be the correct ans .
 
Daniel Dalton
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpSessionAttributesListener has to be declared in web.xml because it's intended to be implemented by container level classes.

The container doesn't use reflection to work out which objects implement this interface, so you cant use it on an attribute. i.e ONLY HttpSessionBindingListener is the correct answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic