Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

can I reference session variables in jsp subroutine?

I have a function:

<%!
static void myFunction(String something)
{
    String sessionVar = (String) session.getAttribute("sessionVar");
}
%>

This gives me the error:

An error occurred at line: 437 in the jsp file: /printAPchecks.jsp
session cannot be resolved


How do I reference session variables in subroutines?
Avatar of mccarl
mccarl
Flag of Australia image

You can't use the implicit session object directly in a method body. Try something like the following...
<%!

HttpSession sessionObject = session;

static void myFunction(String something)
{
    String sessionVar = (String) sessionObject.getAttribute("sessionVar");
}

%>

Open in new window

Avatar of Mark
Mark

ASKER

Looked promising, but I get:

An error occurred at line: 437 in the jsp file: /printAPchecks.jsp
session cannot be resolved

I typed it as you showed:

<%!
HttpSession sessionObject = session;

  :

%>

do I need to import something?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@mccarl, the problem with your idea is that all concurrent users will have to share the same sessionObject.
Ah yes, my bad, forget what I have said and go with rrz@871311's approach! :)
Avatar of Mark

ASKER

I'm going to experiment with this a bit more and get back with results.
Avatar of Mark

ASKER

You know, I'm not going to get around to playing with this any time soon. I solved the problem by passing the parameters I need to the subroutine from the caller and I'm just not getting around to playing with the subroutine/session-variable notion. I'm going to close this out and thank everyone for participating!