This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Need an attribute to be available in different places (SOLVED) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Need an attribute to be available in different places (SOLVED)" Watch "Need an attribute to be available in different places (SOLVED)" New topic
Author

Need an attribute to be available in different places (SOLVED)

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I have a basic site with a login page, a new user registration, a welcome page, and an edit info page.

After logging in, a user gets a page with this:



The username bean is set on a login action

On that same page there is an edit profile link. When the user clicks that link, I need to send him to that page, and I would like that same welcome message to be available.

I have tried just copying and pasting the above to this subsequent page, but I get this: javax.servlet.jsp.JspException: Cannot find bean username in any scope

I have tried setting the scope attribute to "session" on both pages, as well as setting the login action-mapping to session, but it has not worked either. I thought that would be the solution, but apparently not.



The reason I need to do this is because on this edit profile page, I need to tell the java code what the username is, so it can change the database with the new input info based on whatever user is logged in


Here are what i think are the relevant jsp and java code:

LoginSuccess.jsp


profile.jsp


loginAction.java


ChangeProfileAction.java


True wisdom is in knowing you know nothing - Socrates
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I have tried setting the scope attribute to "session" on both pages, as well as setting the login action-mapping to session

And you also changed your Action to put the username in the session ? Meanning replacing request.setAttribute("username", sUserName);.


[My Blog]
All roads lead to JavaRanch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Generally , you can put user specific details [very few, not much] into session . so that, when ever you need them, you can take from session.

so ,In *LoginAction* create HttpSession and change this request.setAttribute("username", sUserName); to *session.setAttribute("username", sUserName);*

hth
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Oops! Christophe has been beaten me
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Christophe Verré wrote:
And you also changed your Action to put the username in the session ? Meanning replacing request.setAttribute("username", sUserName);.
.

I did not do that. I bet that is my problem, I will update the thread once I have a chance to try this out. So instead of request.setAttribute I need to use session.setAttribute. I really think that is the problem
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Hmm, no good ><

I changed the login Action to a session scope in the struts-config file.



Then on my loginSuccess page I changed this line :



And in my java code I changed the following code to accommodate session instead of request (code pointed out with <<<<<<<<<<<<<<<<<<<<<<)



But when I try to login now I just get a blank white page. It is not even getting as far as line 33, because nothing is being printed to the console. Any idea where I am going wrong?
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

OK, well that did not work, but it did spark my brain and I found a different solution. This may have been what you guys meant this entire time.

When the user logs in, within that login code I added:

where sUserName is that request attribute assigned upon login.

Them when I need to use this value in a different Class, I used

This worked just fine



I do have another question now however.

Can I use this ServletContext attribute "username" on a jsp page, and if yes how do I do so?

edit - nevermind, found the answer to this one myself: <%=application.getAttribute("username")%>




Thanks for the help
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Is there any reason you're not using EL?
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Because I have not used that tag library and don't know it's capabilities
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's not a tag library, it's JSP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need an attribute to be available in different places (SOLVED)
 
Similar Threads
servlet action is not availbe
Can you have a String type method return two String values, or do I need to use String array?
Compiler error in Action class - configuring data source in struts-config.xml. Please help!
SaveToken
Can't create connection with oracle