Results 1 to 15 of 15
- 04-09-2010, 01:14 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
Use 1 variable in different bean/classes
Hi,
As a university project I was told to develop a Hangman game.
The game itself is OK and working, but I am having some difficulties in some "extras" that I had to include. I have to display the Highest Scores and a Chat using persistent tables. Both are partially working, by that I mean, the Chat records/shows the comment and the Highest Scores records/shows the score but on both I can't show/record the name!! The name's value is always = null;
Explaining better my project:
- I have a login page which uses a LoginBean to check/validate the name and password entered. (here I have the get and set for name/password)
- From my GameBean and ChatBean I need to pass the value of the LoginBean's name to record into my tables. I am using on GameBean and ChatBean the following code to access the name on LoginBean:
And then using the variable name as parameter!!Java Code:String name; LoginBean loginBean = new LoginBean(); name = loginBean.getName();
It doesn't appear as an error but the value on name is always NULL!! All my jsp pages shows the correct value when I refer to loginBean.getName() though!!
Why is not working? What am I doing wrong?
Thanks!!
- 04-09-2010, 01:17 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
What that bit of code is doing is creating a brand new LoginBean (presumably with null attributes), then assigning one of those attributes (name in this case) to a String called "name". That attribute is null, so "name" is null.
- 04-09-2010, 01:22 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
I thoght that could be the problem!! :(
How can I access the variable name in the LoginBean from my other classes then?
I have tried the @Resource but it keep throwing me an error about mapped-names required (or something similar to it!!).
Thanks
- 04-09-2010, 01:27 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Where does you LoginBean instance live?
- 04-09-2010, 01:30 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
- 04-09-2010, 01:33 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
OK, when the user logs in, where and how do you create a login bean, and what do you do with it?
You mention it's in a JSP page, which means I get the impression you're just using it to validate the user and not actually holding onto the information.
- 04-09-2010, 01:47 PM #7
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
My first page is a login.jsp with 2 inputTexts referring to the LoginBean (name and password) and the validator:
and a CommandButton which takes you through to the next page after running a validator:Java Code:<h:inputText value=#"{loginBean.name}"></h:inputText > <h:inputSecret value=#"{loginBean.password}"> <f:validator validatorID="userLogin.validatePassword"></f:validator> <h:inputSecret>
I hope it makes sense!!Java Code:<h:commandButton value="Login" action="login"></h:commandButton>
Anyway, on the other jsp pages I use the outputText and refer to the loginBean.name and all of them return the correct name
Thanks for your help!!!! :)Java Code:<h:outputText value="#{loginBean.name}"></h:outputText
- 04-09-2010, 02:09 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
OK, so the loginBean is part of the request now.
This is why people really should learn about servlets before doing JSPs...it would give a good insight into what's actually going on.
Once that second page has been rendered back to the browser the login bean is gone.
Normally you'd have a servlet (or whatever equivalent beast depending on the framework being used) which would handle the login, and then store the login bean in the session on the server. You would then access this whenever you needed the user details.
- 04-09-2010, 02:16 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
can you please recommend a tutorial on servlets?
And a last question, why does the name appears ok in my other jsps even though they have been rendered back to the browser? Should it not be appearing as an error message considering that the login bean is gone?
Once again thanks for your help!!
- 04-09-2010, 02:20 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Oh...it does?
SEE the glaring holes in my JSP knowledge!
:)
Maybe it is sticking it in the session then?
OK, assuming that's the case, where do you do stuff with the GameBean and the ChatBean?
Oh, and a Servlet tutorial from Sun.
- 04-09-2010, 02:36 PM #11
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
the Login takes you to a "main menu" page where you can access the Chat, the Game,etc...
The GameBean is accessed from the newGame.jsp. It basically controls the whole HangMan game itself, by that I mean, it creates the word to be guessed,receive the letter chosen by the user, check the user's letter against the word, display results/messages, record the scores and will navigate you to a different page depending if you have win or not.
my code on the newGame.jsp is:
The loginBean.name in there return the correct name. And the GameBean is "actioned" in the commandButton.Java Code:<f:view> <f:loadBundle basename="com.userLogin.messages" var="msg"/> <h:form> <h:outputLabel value="#{msg.welcome} #{loginBean.name}"></h:outputLabel> </h:form> <h:form> <h:outputText value="#{gameBean.message}"/> </h:form> <h:form> <h:inputText id="userChosenLetter" label="User Letter" value="#{gameBean.userLetter}"></h:inputText> <h:commandButton id="submit" action="#{gameBean.guessLetter}" value="Submit" /> </h:form> </f:view>
The ChatBean is the same principle but from the chat.jsp.
Thanks for the tutorial. I will have a look. I hope I don't have to change too much my application to fit it in (if needed!!). Not much time left to hand in the project I am afraid.
- 04-09-2010, 02:57 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
I would say you're trying to do too much using JSPs.
They're really intended for displaying stuff, not actually processing anything, even though there are tags that sort of do processing.
Have you done anything with the "<%" in JSP? That is, sticking Java code into the page?
I now have to wash my mouth out...;)
- 04-09-2010, 03:03 PM #13
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
Actually I thought my jsp page was being used mainly for display as the only thing that it does is to fire the GameBean.guessLetter (which does the whole calculation, etc!!)
Anyway, nope. Never done anything with Java code in the page!! I really am quite new to Java and OOP!! But if there is any "quick" code that can get the variable working at this point and time I am happy to try!! ;) :)
Any specific code that you could indicate? or any tutorial that I can go through to find out?
- 04-09-2010, 03:09 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Well, all you really need to do is pass the name into whatever call you're making to persist your data...not that I'm too clear how that works.
But being Friday afternoon I am close to out of here I'm afraid.
- 04-09-2010, 03:19 PM #15
Member
- Join Date
- Apr 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Shared variable for other classes
By Cemi in forum New To JavaReplies: 20Last Post: 04-09-2010, 11:42 PM -
variable scope between classes
By newbie123 in forum New To JavaReplies: 6Last Post: 03-03-2010, 11:20 PM -
UREGENT-accessing session bean ok but entity bean
By parimal in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 08-28-2008, 01:34 PM -
How to use Inner bean definitions via nested bean elements
By Java Tip in forum Java TipReplies: 0Last Post: 03-30-2008, 10:03 AM -
Passing variable information between classes
By zen_to_go in forum New To JavaReplies: 1Last Post: 10-30-2007, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks