aspose file tools*
The moose likes JSP and the fly likes how to access same javabean in previous web page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to access same javabean in previous web page?" Watch "how to access same javabean in previous web page?" New topic
Author

how to access same javabean in previous web page?

Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
Hi,all
I have a screen web page a.jsp with a submit button, I used a javabean in a.jsp with name say "bean1", the submit button will bring to another web page b.jsp, in b.jsp I need to access the data in "bean1" , how to do that ? by javabean's "scope" attribute ?
Thanks a lot for the help, sample codes welcome.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Yes, you use the scope attribute. It depends on how you wanna use the bean; Within a session , application, request or page... I've got a simple example but is too long to paste it here. drop me an email and i'll send it to you
britt50423@yahoo.com
cheers


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
BTW, check page 44 jsp specification 1.2
It explains when you should use them.
Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
Yes, I got it from the spec..
Thanks.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi,
You can have something like this (your webapp's name is "sample"):
start.html
<HTML>
<BODY>
<form method=get action="/sample/a.jsp">
Hello string: <input type=text name=hello> <input type=submit>
</form>
</BODY>
</HTML>
a.jsp
<HTML>
<BODY>
<jsp:useBean id="helloBean" scope="session" class="HelloBean">
<jsp:setProperty name="helloBean" property="*" />
</jsp:useBean>
<jsp:forward page="b.jsp" />
</BODY>
</HTML>
b.jsp
<HTML>
<BODY>
<% HelloBean helloBean = (HelloBean)session.getAttribute("helloBean");
%>
<%= helloBean.getHello() %>
</BODY>
</HTML>
HelloBean.java
public class HelloBean implements java.io.Serializable
{
private String hello;

public HelloBean()
{
hello = "Hello world!";
}

public String getHello()
{
return hello;
}

public void setHello(String h)
{
hello = h;
}
}
-anthony
ralph roper
Greenhorn

Joined: Nov 25, 2001
Posts: 25
Does the bean really need to implement serializable ?
I thought all you needed to do was use the bean in the session scope.
Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
Anthony,
instead of
<% HelloBean helloBean = (HelloBean)session.getAttribute("helloBean");
%>
in your b.jsp
I still use
<jsp:useBean id="helloBean" scope="session" class="HelloBean">
in b.jsp
it give me the same helloBean created in a.jsp
as for the "Serializable", I never used in my bean defination, any benefit by implementing this ?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi Sylvia,
Some servers set a maximum number of sessions to be held in memory before older sessions are swapped out to a storage provider. They keep track of sessions that have not been used recently and use a session persistence mechanism to serialize the session data to storage (a file or database).
If a session was swapped out and requested again, it is read back into memory and used. This setting keeps the amount of memory used by sessions in check.
If the session data is not serializable, this server-dependent persistence mechanism may throw an error.
-anthony
[ March 25, 2002: Message edited by: Anthony Villanueva ]
 
 
subject: how to access same javabean in previous web page?