• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javabean reference

 
java alltheway
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a problem with a javabean. I can't get a reference in a servlet. This is what I do:
1- I open a jsp page with the following:
<jsp:useBean id="cal_bean" class="Calendar_Bean" scope="session"/>
Name = <%=cal_bean.getName()%> 1st time name is blank
2- the user may open a login window - which post to a servlet
3- in the servlet, I have the following code:
HttpSession session = req.getSession( false );
Calendar_Bean cal_bn = (Calendar_Bean)session.getAttribute ("cal_bean") ;
cal_bn.setName( str_uname ); got username using getParameter
4- when the login window is closed, I will to refresh the main page to display the user name.
Problem:
The username is always null even if I enter a name in the login window. Seems like the servlet doesn't have a reference to the javabean created in the jsp window. What's wrong with this?
Thanks,
Francois.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by java alltheway:
Hi all,
I have a problem with a javabean. I can't get a reference in a servlet. This is what I do:
1- I open a jsp page with the following:
<jsp:useBean id="cal_bean" class="Calendar_Bean" scope="session"/>
Name = <%=cal_bean.getName()%> 1st time name is blank
2- the user may open a login window - which post to a servlet
3- in the servlet, I have the following code:
HttpSession session = req.getSession( false );
Calendar_Bean cal_bn = (Calendar_Bean)session.getAttribute ("cal_bean") ;
cal_bn.setName( str_uname ); got username using getParameter
4- when the login window is closed, I will to refresh the main page to display the user name.
Problem:
The username is always null even if I enter a name in the login window. Seems like the servlet doesn't have a reference to the javabean created in the jsp window. What's wrong with this?
Thanks,
Francois.

After you do:
cal_bn.setName( str_uname );
try:
session.setAttribute("cal_bean", cal_bn);
It's possible that under some circumstances (and some appliation servers) that the bean is swapped out and then replaced from the swapped-out version if you don't re-set the attribute.
Kyle

------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
java alltheway
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kyle,
The setAttribute() line was there, but I was using request instead of session. Now it works..
Francois.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"java alltheway"
your name doesn't agree with the javaranch guidelines.
please take a moment and re-register after reviewing the
guidelines at http://www.javaranch.com/name.jsp
thanks for your cooperation.
- satya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic