Simply stated, your class doesn't know anything about the session variable. Doesn't know what it is, what it points to, what class it represents, where it is, etc. If I wrote the following:
public class test { public static void main(String[] args) { System.out.println(myVariable); } }
That wouldn't compile because myVariable is unknown. It's not defined anywhere. Likewise with your session variable.
The "non-static method..." error your getting when you tried to change that line is because what it's trying to do is call the getAttribute() method of the HttpSession CLASS, *not* of an instance of that class. The only way that works is if getAttribute() is static, which it's not (refer to the javadocs if you need to convince yourself).
It's just like what would happen if I wrote the following code:
public class test { public static void main(String[] args) { System.out.println(myMethod()); } public void myMethod() { return "Hello" } }
That wouldn't compile either and would result in the same error. Remember, static methods don't have access to non-static members (methods or properties) because static means it exists independant of any instantiated object, it's part of the class itself.
So, if I wanted to get my example above working, I'd have to do:
test t = new test();
... before the System.out.println call, and also change myMethod() to t.myMethod().
So, to bring this back to your particular case... you need to pass an instance of a valid HttpSession object to your listFormDetail() method, named session, then your original code should work as-is (oh yeah, and don't forget that getAttribute() returns an Object, so you'll need to cast to a String).
But, as someone else (Dakota Jack I think) said, it's cleaner design to pass the userID itself to this class, since this class is probably considered part of your model (can't be sure without seeing the big picture, but I'd bet it is), and you want to keep your model separate from the control layer, insulate it from the fact that it's working as part of a webapp. Passing an HttpSession object means you can't reuse this class outside a webapp (easily at least).
As for any other dimension of this question, regarding general design or JDBC code design and so on, I'm going to stay away from all that. I think I've answered the question you initially asked, but feel free to inquire further if not.
-- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com
Jim Douglas wrote:
I can connect and authenticate via a database no problem. My problem is I can't compile because of this line,
String userID = session.getAttribute("userID"); - gives an error "cannot find symbol variable session"
So I changed it to this,
String userID = HttpSession.getAttribute("userID");
...and I get an error message that says, "non-static methods getAttribute(java.lang.String) cannot be referenced from a static context"
The answer may be my syntax on that line, I'm not sure.
(This is the code, it's not a servlet)
public class SQL92FormDetailDAO implements FormDetailDAO {
private Connection connection; public SQL92FormDetailDAO(Connection connection) { this.connection = connection; }
public List listFormDetail() { List items = new ArrayList(); Statement statement =null; try { statement = connection.createStatement(); String userID = session.getAttribute("userID"); String query = "select name, formdetail from " + "forms where userID = " + userID;
Thank for the great responses! -Jim
From: Larry Meadors <[EMAIL PROTECTED]> Reply-To: Larry Meadors <[EMAIL PROTECTED]> To: Struts Users Mailing List <user@struts.apache.org> Subject: Re: Attributes, Parameter or Class Date: Sat, 8 Jan 2005 17:06:34 -0700
Sorry Jim, I have to agree with the other posters...this is a really unclear question.
I think what you are asking is this: When a user logs in, i want to put the user id and password somewhere that i can always find it easily.
If so, put it in session scope. It will be there until the session expires, and you can get to it from a JSP or servlet.
If you need it available from everything in the web app, you could use something like a filter in conjunction with ThreadLocal to do that...but I do not think that is such a great idea. IMO, keeping it in session, and passing it to your model is a cleaner and more maintainable design.
Larry
On Sat, 08 Jan 2005 19:25:15 +0000, Jim Douglas <[EMAIL PROTECTED]> wrote:
> I have an LogonForm, LogonAction and when a user successfully logs on, I set
> Attributes for "userID" and "userName".
>
> How would be the best way to make this information available to the Web App
> regardless of whether I need the data from within a JSP, servlet or class
> file(for example, building a dynamic query)
>
> This is what I was trying to do and raised this issue(I can't get it to
> work)
>
> public class SQL92FormDetailDAO implements FormDetailDAO {
>
> private Connection connection;
> public SQL92FormDetailDAO(Connection connection) {
> this.connection = connection;
> }
>
> public List listFormDetail() {
> List items = new ArrayList();
> Statement statement =null;
> try {
> statement = connection.createStatement();
> String userID = session.getAttribute("userID");
> String query = "select name, formdetail from "
> + "forms where userID = " + userID;
>
> Thanks,
> Jim
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]