0
votes

I haven't been able to answer these 2 questions in a written interview I had, can everyone help me?

QUESTION 1) Suppose you want a Servlet to send persistent data to a JSP page, such that different users get different data, which of the following sentences is true?

--a) You can use the instruction: request.getSession().setAttribute(E,F); The JSP page will read the information using the action jsp:useBean with scope="page" and id="E".

--b) You can use the instruction: request.getSession().setAttribute(E,F); The JSP page will read the information using the action jsp:useBean with scope="session" and id="E".

I thought b) could be the right answer but I'm not sure if I completely understand what "persistent data" refers to..

QUESTION 2) If a servlet sends a bean to a JSP page, possible changes to the bean made inside the JSP page will always be visible to other JSP pages or servlets that will access the bean in future. True or False?

I guess it's false but I just can't get a good explanation about it..

Thanks in advance guys!

2

2 Answers

2
votes

Question 1: none of the above. There is no reason to store the beans in a session attribute. It should be stored in a request attribute instead. And the view shouldn't use jsp:useBean at all. That was the way to do it 17 years ago. The JSP EL should be used instead. But of course, trying to find the bean in page scope won't work, since it has been stored in session scope.

Question 2: First of all, a JSP shouldn't modify beans that it received from a servlet. A JSP is a view, supposed to display the model it receives from the controller. Not to modify it. Even if it does modify the bean, it all depends what the scope of the bean is. If the bean has been created by the servlet, stored in a request attribute and passed to the JSP, it's not visible from any other component, and will be eligible to GC as soon as the request has been handled. If it's a singleton that is passed by the servlet to several pages, then all the pages will get the same singleton object, and the changes will thus be visible (assuming synchronization is used correctly), to all the JSPs.

Frankly, the questions say a lot more about the employer than the answers say about you: the employer doesn't seem to understand what he's doing, and uses its tools as if nothing had changed since 1999.

0
votes

For the question 1 : the instruction: request.getSession().setAttribute(E,F); this is session scope, the attribute is set in the session so the right answer is B I think persistent data mean that this data is persistent as long as the session