Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to access same javabean in previous web page?

 
Sylvia Wang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, check page 44 jsp specification 1.2
It explains when you should use them.
 
Sylvia Wang
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I got it from the spec..
Thanks.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic