permaculture playing cards*
The moose likes Servlets and the fly likes Javabean reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Javabean reference" Watch "Javabean reference" New topic
Author

Javabean reference

java alltheway
Greenhorn

Joined: Oct 03, 2001
Posts: 5
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

Joined: Aug 10, 2001
Posts: 3892
    
    5
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.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
java alltheway
Greenhorn

Joined: Oct 03, 2001
Posts: 5
Thanks Kyle,
The setAttribute() line was there, but I was using request instead of session. Now it works..
Francois.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

"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

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javabean reference
 
Similar Threads
problem without logout, how to solve it
Session is not working
Session Problem
simple problem with web.xml
Why will this not work?