Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Values into variables, from beans or Session Attributes ?  RSS feed

 
Greg Kok
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
That's it ! Cheers mate !
 
Greg Kok
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Smith
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!