aspose file tools*
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
Author

cant use out.println() inside a method declaration

saintmacai
Greenhorn

Joined: Jan 20, 2002
Posts: 10
hello!
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();
out.println("test");
for (int i=0; i<fileList.length; i++ )
{
JOptionPane.showMessageDialog(null,String.valueOf(fileList.length));
}
}
%>
i always recieved this message

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

thankss!
Madhav Lakkapragada
Ranch Hand

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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
David O'Meara
Rancher

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.
Dave.
David O'Meara
Rancher

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?
Dave.
ej dinglasan
Greenhorn

Joined: Jan 22, 2002
Posts: 7
dave,
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.

ellai(saintmacai)
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)
{
out2.println("test");
}


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: cant use out.println() inside a method declaration