posted 17 years ago
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 .