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.
Sowmya Vinay
Greenhorn
Joined: Feb 01, 2001
Posts: 24
posted
0
<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
Joined: Dec 11, 2000
Posts: 4
posted
0
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> <% } %>
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
Look at the Java source code that is generated. JSP is doing a lot of stuff you dont see. Bill
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>
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.