Results 1 to 15 of 15
  1. #1
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Question 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:

    Java Code:
    String name;
    LoginBean loginBean = new LoginBean();
    name = loginBean.getName();
    And then using the variable name as parameter!!

    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!!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    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.

  3. #3
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    Where does you LoginBean instance live?

  5. #5
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Where does you LoginBean instance live?
    Sorry, I am a beginner on Eclipse/Java... what do you mean by where the instance live?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    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.

  7. #7
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    My first page is a login.jsp with 2 inputTexts referring to the LoginBean (name and password) and the validator:
    Java Code:
    <h:inputText value=#"{loginBean.name}"></h:inputText >
    <h:inputSecret value=#"{loginBean.password}">
    <f:validator validatorID="userLogin.validatePassword"></f:validator>
    <h:inputSecret>
    and a CommandButton which takes you through to the next page after running a validator:
    Java Code:
    <h:commandButton value="Login" action="login"></h:commandButton>
    I hope it makes sense!!

    Anyway, on the other jsp pages I use the outputText and refer to the loginBean.name and all of them return the correct name
    Java Code:
    <h:outputText value="#{loginBean.name}"></h:outputText
    Thanks for your help!!!! :)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    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.

  9. #9
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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!!

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    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.

  11. #11
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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:
    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 loginBean.name in there return the correct name. And the GameBean is "actioned" in the commandButton.

    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.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    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...;)

  13. #13
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    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?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default

    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.

  15. #15
    maneuk is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks!! I'll have a look and see if I can find anything!!

    Have a nice weekend!! :)

Similar Threads

  1. Shared variable for other classes
    By Cemi in forum New To Java
    Replies: 20
    Last Post: 04-09-2010, 11:42 PM
  2. variable scope between classes
    By newbie123 in forum New To Java
    Replies: 6
    Last Post: 03-03-2010, 11:20 PM
  3. UREGENT-accessing session bean ok but entity bean
    By parimal in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-28-2008, 01:34 PM
  4. Replies: 0
    Last Post: 03-30-2008, 10:03 AM
  5. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 08:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •