Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:usebean confusion

 
geet rahane
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all !!!

Q.from Free scwcd mock exam for jsp by Nikos

Consider this code.

What is the output when the form is submitted? (1 correct answer)

1. I like null!
2. I like Head First Servlets and JSP!
answer 1

why not the answer 2.?
cant property set by input.jsp not retrieve in output.jsp ...as scope given is request......i think the request remains the same in both jsp..
please help....thanks in advance
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the request remains the same in both jsp.


And what makes you think so?? When you press the click here link on input.jsp, a new request is sent to the server to access output.jsp. So the Book bean set in the request to input.jsp, is not available. Remember, each time you request something from the server, a new request is sent...
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The trick here is in <jsp:usebean > tag which creates the object as an attribute in the mentioned scope. In this case <jsp:setProperty > is the counter part of request.setAttribute (if the scope is selected as request).

As you might be aware that all the request attributes set in a jsp remain in the same jsp and are lost as soon as the request object available in that jsp is lost. You can refer the following section in useBean syntax

request - You can use the Bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another file. You can use the request object to access the Bean, for example, request.getAttribute(beanInstanceName).


If you want the attribute to be available in another jsp as well you shall have to save the object in session. like in following secion





Regards
Salil Verma
 
geet rahane
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit and Salil!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic