• 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

getProperty not accessing correct object

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use an object with getProperty, the object is created within a scriplet. I am not getting the object created in the scriplet - can anyone please let me know why?
here is the Server Page
<%@ page import="CounterBean" %>
<jsp:useBean id="counter" class="CounterBean" >
<% counter = new CounterBean();
counter.setCount(44);
System.out.println("counter count:" + counter.getCount());%>
</jsp:useBean>
<h3>
the count should be 44 but is?
<jsp:getProperty name="counter" property="count" />
</h3>
and here is my CounterBean
public class CounterBean
{
private int count;
public int getCount()
{
return count;
}

public void setCount(int count)
{
this.count=count;
}
}
When running the JSP I get the following output
the count should be 44 but is? 0
I expect the count to be 44.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<jsp:useBean id="counter" class="CounterBean" >
By using the "useBean" tag you are already doing something like this:
CounterBean counter = new CounterBean();
when it jsp is invoked for the first time(Later on , the same object may be used, depending on the scope setting).
So there is no need to initialise counter object again!
Remove the line counter = new CounterBean() from the scriptlet, and it should give the desired functionality.

[This message has been edited by Sowmya Vinay (edited February 13, 2001).]
[This message has been edited by Sowmya Vinay (edited February 13, 2001).]
 
Dave Hathaway
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sowmya - thanks for the reply.
This will work but doesn't answer the question why can't I change the object reference of counter?
Sometimes I made need to change the object reference - if I wanted to say receive a counter from a vector of counters (list) which had been set by a servlet (setAttribute("list",list) - how I could I access this with getProperty.
I am getting the same problem having changed the object reference.
In this case...
<%@ page import="CounterBean" %>
<jsp:useBean id="counter" class="CounterBean" / >
<%
Vector v = (Vector)request.getAttribute("list");
Iterator i = v.iterator();
while (i.hasNext()){
counter = (CounterBean)i.next();
counter.setCount(44);
<h3>
the count should be 44 but is?
<jsp:getProperty name="counter" property="count" />
</h3>
<% } %>

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the Java source code that is generated. JSP is doing a lot of stuff you dont see.
Bill
 
Dave Hathaway
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally worked it out - to make the object visible to the bean setAttribute afterwards.
For Example:
<%@ page import="CounterBean" %>
<jsp:useBean id="counter" class="CounterBean" scope ="request" >
<% counter = new CounterBean();
counter.setCount(44);
request.setAttribute("counter",counter); %>
<h3>
the count should be 44 but is?
<jsp:getProperty name="counter" property="count" />
</h3>
</jsp:useBean>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic