File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes cant use out.println() inside a method declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "cant use out.println() inside a method declaration" Watch "cant use out.println() inside a method declaration" New topic

cant use out.println() inside a method declaration


Joined: Jan 20, 2002
Posts: 10
how come i cant use the method out.println inside a method declaration.
heres my code
void extractFileStructure (String root, File absPath)
{ String fileList[] = absPath.list();
for (int i=0; i<fileList.length; i++ )
i always recieved this message

C:\Blazix\jspdir\providend\ cannot resolve symbol
symbol : variable out
location: class desisoft_jsp_reportexplorer_jsp1011755969799
out.println("wala lang");
1 error

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
your name deosn't agree with the javaranch guidelines. Please take a moment and re-register after reviewing the guidelines at
thanks for your cooperation.
- satya

Take a Minute, Donate an Hour, Change a Life
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

In general (and this is just a basic description), all code you put in a JSP goes into the service method when it gets turned into a Java class.
There is some additional information that gets created in the service method to make life easier for you you, like request and response objects, sessions and the output stream.
When you use a declaration tag to define your own method, it doesn't have access to the variables local to the service method unless you pass them to it...

So the out exists in the scriptlet since it is in the scope of the method (that it gets turned into) and this gets passed to the method.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I've been asked to extend the solution.
Previously we had this:

When this gets turned into a class (ie a servlet) it will look something like this: (this is a gross simplification, but it highlights our point)

The point is that you can see that the out variable is local to the service method. The myMethod method cannot see it unless it gets passed as a parameter.
If the out variable was defined as an instance variable, both methods would be able to see it, but then the Servlet would not be Thread-safe (and this is another topic)
Is this clearer?
ej dinglasan

Joined: Jan 22, 2002
Posts: 7
i'm sorry but im quite new to jsp and servlets can yuo please modify my previous code and show how i can use the out.println method.
thanks. I hope your yet annoyed with me.

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You may be new to JSP, but I will assume you are not new to Java and that you have a JSP or Java textbook. Just pass the outputstream reference as a parameter to your method.
void extractFileStructure (String root, File absPath, OutputStream out2)

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: cant use out.println() inside a method declaration
It's not a secret anymore!