File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Bean Scope?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Bean Scope??" Watch "Bean Scope??" New topic
Author

Bean Scope??

Naresh Babu
Greenhorn

Joined: Mar 20, 2001
Posts: 29
Hi,
I am using A.jsp, B.jsp and i want to use the bean created in A.jsp in B.jsp.But, in B.jsp its giving always as null.
A.jsp
<%@ page class="classname" %>
<jsp:useBean id="mybean" scope="session" class="classname" />
<jsp:setProperty name="mybean" property="property1" param="param1" />
B.jsp
<%@ page class="classname" %>
<jsp:useBean id="mybean" scope="session" class="classname" />
<jsp:getProperty name="mybean" proprety="property1" />
Please , suggest me any options to make "mybean" working in B.jsp.
Thanks
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Naresh,
We can make A.jsp to set a session var and abother b.jsp grab the var's value set by previous a.jsp. Here is the sample code I tested. Don't bother about the package of the bean. I just used a sample file which used for another Javaranch user's question.
If you want a plain bean, just remove the package statements in .java file and from all .jsps and place bean's .class file directly under ../appnName/web-inf/classes directory.


Is this what you wanted ?
regds
maha anna
[This message has been edited by maha anna (edited March 20, 2001).]
Naresh Babu
Greenhorn

Joined: Mar 20, 2001
Posts: 29
Thanks! Maha for the reply.I am using the code which
you have given.But , output is "null" for
<%=worksheet_bean.getPassword()%> (in test2.jsp).
In test1.jsp, i am able to retreive the value.
My problem, is the "worksheet_bean" (created in test1.jsp)
cannot be accessed in test2.jsp.
Thanks

Naresh Babu
Greenhorn

Joined: Mar 20, 2001
Posts: 29
Thanks! Maha for the reply.I am using the code which
you have given.But , output is "null" for
<%=worksheet_bean.getPassword()%> (in test2.jsp).
In test1.jsp, i am able to retreive the value.
My problem, is the "worksheet_bean" (created in test1.jsp)
cannot be accessed in test2.jsp.
Thanks


[This message has been edited by Naresh Babu (edited March 20, 2001).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Are you using Tomcat? I tested with both scope='session' and scope='application' and got the results in test2.jsp both times.
By default all pages (.jsps) participate in session, which means
<%@page session="true" %> <% -- Default -- %>
Anyhow try to put this line as first code in test1.jsp and test2.jsp and let us know.
regds
maha anna
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
If the above change doesn't work try this method. If this way works, you should get "Maha Soorya Anna Naresh" when you first call test1.jsp and latter call test2.jsp.

Naresh Babu
Greenhorn

Joined: Mar 20, 2001
Posts: 29
I am using Allaire . I tried including session="true" in both
the files.
If i use the setAttribute , i am able to retreive the value.But i want to
user setProperty and getProperty methods.
Thanks!

[This message has been edited by Naresh Babu (edited March 20, 2001).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
If the servlet engine is written according to servlet 2.2 spec it should work the way we expect. I suspect the implementation.
What's your goal? a.jsp should freshly create a bean when called eachtime /first time and other jsps should be able to read the session bean's values ?
Please give some more info of your need so that we can find a work-around.
regds
maha anna
Naresh Babu
Greenhorn

Joined: Mar 20, 2001
Posts: 29
Sorry, i was not able to reply immediately. The Allaire server which i am working on , has support for servlet 2.2.

My goal:In our project we have different pages
where the user enters data.At the end of all
jsp pages the data entered by user in previous
jsp pages has to be written to a text file.
If we are going for attributes , for a single
client there would be 70-80 attributes in a session.
It will be difficult to retreive the attributes.

So, i thought if we use bean and store the data
in bean,then we can insert all the data whenever required.
Please , suggest me if there is a better option.
Thanks! for your response.

Thanks
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Naresh,
Since those setProperty and getProperty's don't work properly in Allaire, here is a work-around which comes to mind.
Let the bean's constructor take HTTPRequest as argument and init itself like this.


I haven't tested this code. There could be some syntax errors. But you get the logic!
regds
maha anna
Naresh Babu
Greenhorn

Joined: Mar 20, 2001
Posts: 29
Thanks! for the reply.I got it now , when i use
value="XXX" in setProperty.Previoulsy ,
i user param="param1" where param1 is a text field.
So, i think i will retreive the value from textfield
and the use value="XXX" instead of param="param1".

I am v.sorry , for the trouble.I will try the code
which u have given.
Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean Scope??