• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Values into variables, from beans or Session Attributes ?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to use the value a Session Attribute I've defined and set in a previous JSP, in a variable on my current JSP, as follows :
<%! String thisUser;%>
<% thisUser=session.getAttribute("username"); %>
I get the following error :
[
incompatible types
found : java.lang.Object
required: java.lang.String
thisUser=session.getAttribute("username");
^
1 error
]
How do I go about this ?
I will also need to load the values set in a bean, into a variable.
I get similar errors.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Two things:
1) You've declared thisUser as a String. What type does session.getAttribute return? Connect the dots.
2) By declaring thisUser as a class variable, you've set up this page to share the instance of thisUser across all pages served regardless of the session. I can't believe that this is what you really want to do.
 
Greg Kok
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's it ! Cheers mate !
 
Greg Kok
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(A post from Winston worked for what I am trying to do... The suggestion was to use <% thisUser=(String)session.getAttribute("username"); %>
I realize this may not be the best way to do this, or even a good way, but like any other eager greenhorn, I'm here to get ideas and learn.
I'm creating a small JDBC JSP app. An initial login screen stores user info once-off (in the bean or Session Attribute) and directs to a split frame page. The top frame lists available query selections. Clicking a selection runs the associated JSP in the bottom frame. Each JSP gets the user info previously stored, connects, returns the result set and disconnects.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good place to start learning is, not only here, but at Sun's site. Do a search for JSP architecture, and you'll find some good examples/tutorials on the MVC (Model View Controller) architecture - which is a good framework to follow if you're developing web-based apps that utilitize a database.
Basically, you'll get the input from the user (username/password), forward this info to a JSP utility page, or Java Servlet, which in turn contacts the database, looks up the user, the user's privileges, etc., and returns the info back to the client.
I usually have a Bean controlling the database utilities, and storing the user information. This way, you can easily model a user throughout the session simply by including the useBean tag:

WS
 
reply
    Bookmark Topic Watch Topic
  • New Topic