• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cant use out.println() inside a method declaration

 
saintmacai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic