Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to assign value to java bean using <jsp:setProperty> in jsp?  RSS feed

 
Muneeb Kiani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I want to assign value dynamically to javabean through "<jsp:setProperty>" tag. i want to asign value like this
.
but it give me error. "request.getParameter("user-name)" return a value from previous form. please tell me how can i initiate my bean dynamically. thanks
 
anchit pancholi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using quotes(" ") on value parameter.

<jsp:useBean id="b" class="com.fyp.MyBean" scope="session" />
<jsp:setProperty name="b" property="password" value= "<%= request.getParameter("user-name")" />


Thanks
Anchit
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and still you can improve as in



but Again, instatiation and set values should be done on Servlet . what you have to do in jsp is just *get* values.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:

Shouldn't that be ${param['user-name']}? Because ${param.user-name} would resolve into the value of an attribute name being subtracted from a parameter called user.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. It's a poor idea to use parameter names that do not conform to Java identifier rules for this reason.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely Correct Rob.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason why



would not work?

If you're going to use this tag, then I would suggest using it to its full potential.

If you're going to use EL, I would recommend the <c:set> tag instead of <jsp:setProperty>

ie
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the setProperty action, while still in JSP is a JSP1 dinosaur that should be replaced with JSTL.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!