• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javabean reference

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kyle,
The setAttribute() line was there, but I was using request instead of session. Now it works..
Francois.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic