Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing bean with Servlet & JSP

 
Tom John
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic