• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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);.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Christophe has been beaten me
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason you're not using EL?
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because I have not used that tag library and don't know it's capabilities
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a tag library, it's JSP.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic