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?
LVL 1
MarkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
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

0
MarkAuthor Commented:
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?
0
rrzCommented:
You need to pass the variable into your method as a parameter. You should look at the translated code that JSP engine created. In Tomcat, it can found in the work folder.
<%!
   public void myFunction(String something, String sessionVar)
{
   //do something with it here
}
%>
<%
   //in scriptlet, get var and pass it into your method when you call it
   String sessionVar = (String) session.getAttribute("sessionVar");
   myFunction(String something, String sessionVar);
%>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Looked promising, but I get
Sorry, I was a bit off my game there. You can go with rrz@871311's solution, of if you are accessing a lot of session variables, to make what I suggested above work do the following...

In your method declaration block do this...
<%!

HttpSession sessionObject;

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

%>

Open in new window

And then when you call this 'myFunction' method, make sure you set the sessionObject first. So in your scriplet block do this...
<%

sessionObject = session;


...

myFunction("blah");

...

%>

Open in new window

0
rrzCommented:
@mccarl, the problem with your idea is that all concurrent users will have to share the same sessionObject.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ah yes, my bad, forget what I have said and go with rrz@871311's approach! :)
0
MarkAuthor Commented:
I'm going to experiment with this a bit more and get back with results.
0
MarkAuthor Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.