• Post Reply Bookmark Topic Watch Topic
  • New Topic

Predefined variable "out" in declaration

 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all.
I use predefined variable "out" in my *.jsp to print text.
But I don't understand this problem:
When variable "out" is in scriptlet it is no problem and when "out" is in declaration I get this error.

Could U help me? thank.
My code is here.
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out is a reference to the output stream that is open to the client- in a declaration you are in a responseless context, it code that is defined without the context of a request and response being available so you cant have an out - out is a dynamic resource that is only available while the request is valid. What you want to do instead is define a method that returns a string which you can output from the scriplet
 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U for your answer.
"return a string" is a good idea but I want "trace" function, and detect temporary-value of function-scope variable more time in the function.
for example:
void foo(){
String localstr = "abc";
...
localstr = localstr + instant_str;
out.println(localstr);
...
out.println(localstr.substring(1, 10));
...
//etc...
}
Sorry, my english is terrible.
Originally posted by chanoch wiggers:
out is a reference to the output stream that is open to the client- in a declaration you are in a responseless context, it code that is defined without the context of a request and response being available so you cant have an out - out is a dynamic resource that is only available while the request is valid. What you want to do instead is define a method that returns a string which you can output from the scriplet
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just pass the writer into the new method:
 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx.
It's great and easy.
I'll use it.
Originally posted by Frank Carver:
Or just pass the writer into the new method:
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The out variable is an implicit variable created from the JSP and used in the _jspService method of the servlet that is created from the JSP. Anthing that you put with scriplet tags in a JSP is directly put into the _jspService method so if you use out in a scriptlet it is ok because out is declared and defined within the _jspService method. A declaration on the other hand is inserted into the servlet outside of the service method, so if you use out in a declaration then there is no out variable to be used. That is the whole purpose of the declarations so that you can add methods and variables to the JSP that are not included implicitly.
Your declaring amethod in the declaration andit is just like a normal method in any other class it has to have access to the variable before it can be used. since you didn't declare it in the mehtod itself then out must be an instance or static variable. When in fact out is a local variable to the _jspService method.
Check out the .java file from the servlet generated from your jsp and you should be able to see this.
hope that helps
 
dar
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U.
It's a good explanation.
good luck.
Originally posted by Dave Vick:
The out variable is an implicit variable created from the JSP and used in the _jspService method of the servlet that is created from the JSP. Anthing that you put with scriplet tags in a JSP is directly put into the _jspService method so if you use out in a scriptlet it is ok because out is declared and defined within the _jspService method. A declaration on the other hand is inserted into the servlet outside of the service method, so if you use out in a declaration then there is no out variable to be used. That is the whole purpose of the declarations so that you can add methods and variables to the JSP that are not included implicitly.
Your declaring amethod in the declaration andit is just like a normal method in any other class it has to have access to the variable before it can be used. since you didn't declare it in the mehtod itself then out must be an instance or static variable. When in fact out is a local variable to the _jspService method.
Check out the .java file from the servlet generated from your jsp and you should be able to see this.
hope that helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!