Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

How to convert Object to String

Options
  • 14-04-2005 4:47pm
    #1
    Registered Users Posts: 315 ✭✭


    I am usiing httpsession tracking and i am trying to convert the session object to a string to allow it to be used further on in the program. I have tried everything but cannot get it to work:

    The session information is passed correctly as it displays the name of the person who has logged in using the code below.

    Hello <% = session.getAttribute("Name");%>

    I want to converrt the ("Name") to a String so i can use it later on in the program. Can anyone please help me as i have tried everything and have had not luck. I am using jsp by the way.


Comments

  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    Have you tried casting it to a String ?

    String name = (String)session.getAttribute("Name");


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    deadl0ck wrote:
    Have you tried casting it to a String ?

    String name = (String)session.getAttribute("Name");
    Think that needs more parentheses to get the precedence right

    (String)(session.getAttribute("Name"))


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    MrPinK wrote:
    Think that needs more parentheses to get the precedence right

    (String)(session.getAttribute("Name"))

    The extra parentheses around session.get... are not needed


  • Registered Users Posts: 315 ✭✭s_gr


    Thanks for all the replies, but i am still having problems. I type casting as suggested:

    String x = (String)session.getAttribute("Name");
    System.out.println("Hello " + x);

    Hello null - output


    The system.out keeps returning a null value, even though the session.getAttribute("Name); is working on the same page as it is passing the "Name" from the first page. Does anyone else know where i might be going wrong.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Hmm, weird. Have a look at what's happening in the debugger.


  • Advertisement
  • Registered Users Posts: 315 ✭✭s_gr


    I checked the debugger window but there does not seem to be an problems. I cannot understand why it continually returns a null value. Does anyone have any suggestions that i can try or alternative syntax.

    Thanks for all replies so far.


  • Closed Accounts Posts: 92 ✭✭tempest


    s_gr wrote:
    I want to converrt the ("Name") to a String so i can use it later on in the program.

    Where in the program do you want to use it?

    later in the same jsp, in a different jsp, in a servlet?

    Where is the println code? In the same jsp???


  • Registered Users Posts: 4,185 ✭✭✭deadl0ck


    What happens if you have :

    System.out.println("Hello " + session.getAttribute("Name"));


  • Registered Users Posts: 315 ✭✭s_gr


    tempest wrote:
    Where in the program do you want to use it?
    later in the same jsp, in a different jsp, in a servlet?

    Where is the println code? In the same jsp???

    Tempest,

    Basically i have a login in page which passes the session value to the next jsp page when login is sucessful. It then displays the login name using the

    Welcome <%= session.getAttribute("userName")%>

    In the same jsp i want to be able to use the ("userName") value to search a database. The database side is fine if i supply the userName directly, but when i try to automatically use the session userName,it keeps returning a null value. This is really strange.


  • Registered Users Posts: 315 ✭✭s_gr


    deadl0ck wrote:
    What happens if you have :

    System.out.println("Hello " + session.getAttribute("Name"));

    This also returned a null value. In theory there should be no reason why i cant do this as the session value is already on the same jsp page and i just want to use it to search a database. Each session will have a different userName as different people login in so hence different search of database required.

    Thanks


  • Advertisement
  • Closed Accounts Posts: 25 dan_pretty_boy


    Hi,

    remember that getAttribute is case sensitive, r u sure its the right name.

    Best way is to print out all the attribues that are in the session

    regards
    Danny


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Is it definetely not null? Are you sure it's being set?

    Ya do what Danny said, use the session.getAttributeNames() method to print out all the attributes?


  • Registered Users Posts: 315 ✭✭s_gr


    Hi,

    remember that getAttribute is case sensitive, r u sure its the right name.

    Best way is to print out all the attribues that are in the session

    regards
    Danny

    Got it to work finally by changing variable names and printing out all the attributes as suggested. Worked fine.

    Thanks you all your help & everyone's suggestions


Advertisement