wood burning stoves 2.0*
The moose likes Servlets and the fly likes Sharing bean with Servlet & JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sharing bean with Servlet & JSP" Watch "Sharing bean with Servlet & JSP" New topic
Author

Sharing bean with Servlet & JSP

Tom John
Greenhorn

Joined: Jun 29, 2001
Posts: 10
Hi, I'm trying to use a bean to store some info from a servlet & then access the info later from a jsp. It seems however, that the servlet & the jsp are creating their own instances of the bean.

// Servlet code
LoginBean login = new LoginBean();
login.setUserID(username);
ServletContext context = getServletContext();
context.setAttribute("login", login);
sURL = response.encodeRedirectURL("http://john/attend/AA.html");
response.sendRedirect(sURL);
// NOTE: AA.html is a frameset, the jsp that needs to access
// the bean is in one of those frames

// JSP code
<jsp:useBean id="login" class="Login.LoginBean" scope="application"/>
<jsp:getProperty name="login" property="userID"/>

A bit more background: the bean initializes the 'userID' variable and then the servlet changes 'userID' through its setter method. This works fine as I can print the new value of 'userID' from within the servlet. The problem is that when I access 'userID' from the jsp the initalized value is displayed, not the new value (thats why I'm assuming two seperate instances are being created).
Any help would be appreciated.
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Hi, notice that when you�re doing:
LoginBean login = new LoginBean();
in your servlet and than:
<jsp:useBean id="login" class="Login.LoginBean" scope="application"/>
in your jsp, you�re creating 2 instances of the bean if you want to populate a bean in your servlet and then recover the values from the bean you can put the bean in the session and then recover the bean from the session in your jsp.
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
I tried this and what you have works correctly. As long as you reference the bean in the jsp using the id, by the same name you created it in the servlet, and use the same scope, you should get the same bean. I am using JRun 3. Following is my code:
// servlet
LoginBean login = new LoginBean(); // sets user to Brian
System.out.println("user: "+login.getUser());
login.setUser("Frank");
ServletContext context = getServletContext();
context.setAttribute("login", login);
String sURL = res.encodeRedirectURL("http://localhost:8100/BeanTest.jsp");
res.sendRedirect(sURL);
// login bean
public class LoginBean {
private String user = "Brian";
public LoginBean() { }
public String getUser() { return user; }
public void setUser(String user) { this.user=user; }
}
// jsp page
<jsp:useBean id="login" class="LoginBean" scope="application"/>
<jsp:getProperty name="login" property="user"/>
the result is: Frank
HTH
Brian
Tom John
Greenhorn

Joined: Jun 29, 2001
Posts: 10
Thanks Brian,
I'm still not seeing the correct result in my browser. I'm using Apache/Tomcat and I've got LoginBean.class in Tomcat_dir\lib\Login (because 'Login' is the package name). I wonder if I need to move the login package to another directory. I had tried initially putting it in the same dir with the rest of the class files, but it seemed that the jsp page couldn't find it.
I hope I'm not just missing something silly. Any other suggestions would be appreciated.
Thanks again,
Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sharing bean with Servlet & JSP