Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

populating bean values from jsp  RSS feed

 
Vic Newman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to have a beans values automatically popluated from a jsp page or is request.getParameter("xxx") the only way. For example I have a page with 3 fields. name, city and state. I have a bean with name, city and state. When the page is submitted to the servlet I would like the bean values for name, city and state to be populated. Is that possible or do you have to use getParameter("name"), getParameter("city") and getParameter("state") ni the servlet to retrieve the values and set in the bean.

would <jsp:setProperty name="beanname" property="*"/> automatically set the variables.

thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vic Newman:

would <jsp:setProperty name="beanname" property="*"/> automatically set the variables.


No. If the above appears in the JSP, it's getting evaluated long before the form is even presented to the user. Remember that the JSP is evaluated on the server in order to create the HTML sent to the browser. Putting code on the JSP to deal with the form elements before the page is even presented to the user puts the cart before the horse.

After the user fills out the form and submits to the servlet, then using the getParameter methods is the appropriate means to grab the submitted values and set them into your bean.
 
Vic Newman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also set the value inside the JSP and have the form post to itself, call the servlet/controller and forward to another (output) JSP.

Something like this will set all post variables to the "entry" bean.



You might also want to look into BeanUtil from the Jakarta Commons and here

http://pdf.coreservlets.com/

Chapters 13 and 15
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would he want to revert from a Model 2 architecture to Model 1?
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, I like your analogy

"Putting code on the JSP to deal with the form elements before the page is even presented to the user puts the cart before the horse."

This is a very common question repeatedly gets asked by the JSP/Servlet beginners. It would be nice to have some diagrammatic explanation for the beginners at JavaRanch.

You need to understand the HTTP paradigm first. You can set a request attribute in a servlet and then can use it in a JSP, but you cannot do it from JSP to Servlet, because the first request goes out of scope and the servlet will be receiving a new request.

The JSPs were created to get rid of the unsightly and unmaintainable out.println("<html>..</html>");. Your browser can understand only HTML. So when you make a request no-1 from the browser, your request parameters are passed to your servlet in the web container. The servlet, controls the flow and does some stuff and finally forwards the request to your JSP. The JSP engine converts the JSP to a servlet with all the out.println("<html>...</html>"); statements. finally output html sent back to the client from the _jsp_converted servlet and your request no 1 is complete (request/response). Any attributes you have set in the JSP have gone out of scope now. When the request no 2 invoked the same process repeats. If you set it in a session scope then it will be available until the session is invalidated.

Hope it explains it.
[ April 04, 2006: Message edited by: ak pillai ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!