• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with jsp:getProperty

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my jsp I am using <jsp:getProperty> to get the value of a bean
Here is the code sample
<jsp:useBean id="test" scope="session" class="TestBean"/>
<tr>
<td width="60%" align="right"><b><font color="#000080">First Name #</font></b>
</td>
<td width="40%"><input type="text" name="fName" size="20"
value="<jsp:getProperty name="test" property="firstName" />"></td>
</tr>

I try to use the <jsp:getProperty> tag to get the value
but it keeps initiating a "blank" bean.
As a temporary workaround, I am using <% out.println(TestBean.getFirstName()) %>
to achieve the same effect and it works.
Ideally, I would like to use <jsp:getProperty name="beanName" property="beanID">
And some reason its not working
The firstName is declared as a variable in a Bean and and it also
has a corresponding get method
Guru's Please help me
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your examples are a tad confusing.
In your first example (the one that doesn't work), you refer to the bean with an id of test.
In your second (the scriptlet example), you use TestBean which is the class name.
What's up with that?
Is getFirstName() a class (static) method?
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Its not a static method. Sorry for creating any confusion.
I am creating instance of TestBean class
TestBean testBean= new TestBean();
and trying to get the value like this <% out.println(testBean.getFirstName()) %>
Thanks
Maria
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try instead using the <jsp:useBean id-"test".../> first and then try to acces it via the scriplet code like thgis <%= test.getFirstName()%> and see what result it gives you.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try instead using the <jsp:useBean id-"test".../> first and then try to acces it via the scriplet code like thgis <%= test.getFirstName()%> and see what result it gives you.
That won't work.
The "usebean" tag creates an object and stores it in the named context. It does not create a scriptlet variable. Likewise, just creating a scriptlet variable does not create or affect session beans.
To access a "usebean" object from a scriptlet you need to do something more like:
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I found. beanInstanceName "mine" is accessable in scriptlets & expressions.

JSP:
<jsp:useBean id="mine" scope="session" class="examples.MyBean" />
<%
out.println(mine.getCount());
%>
<%= mine.getCount() %>
<input type="text" name="myName" size="100"
value="<jsp:getProperty name="mine" property="count" />">
JAVABEAN :
package examples;
import java.io.*;
public class MyBean implements Serializable
{private String count = "58";
// Constuctors
public MyBean() { }
public MyBean(String count) {
this.count = count;
}
public String getCount(){
System.out.println("Count "+count
);
return this.count;
}
public void setCount(String count){
this.count = count;
}
}
 
Maria Peter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your help.
I use <jsp:getProperty> in all of my JSP and it just works fine.
I am having this problem only when
jsp property name(count) is different from text box name(myName)
for example

<jsp:useBean id="mine" scope="session" class="examples.MyBean" />
<%
out.println(mine.getCount());
%>
<%= mine.getCount() %>
<input type="text" name="myName" size="100"
value="<jsp:getProperty name="mine" property="count" />

Do you think is this a problem?

In all other JSP jsp propery name and text box name are the same ex
<jsp:useBean id="mine" scope="session" class="examples.MyBean" />
<%
out.println(mine.getCount());
%>
<%= mine.getCount() %>
<input type="text" name="count" size="100"
value="<jsp:getProperty name="mine" property="count" />
According to me it should not make any difference.
But some reason it is not working
Please help
Thanks in advance
Maria
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic