• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

out.print in JSP

 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I am pretty new to JSP so fogive me if this is an easy answer, but I haven't seem to of read of it yet. I am declaring a method in my JSP page that writes different results according to different actions. But I am getting an error that says:
"Undefined variable or class name: out"
Here is a piece of my code:

Any direction would be greatly appreciated.
Bill
[This message has been edited by bill bozeman (edited February 06, 2001).]
 
Sowmya Vinay
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bill bozeman:
[B]Alright, I am pretty new to JSP so fogive me if this is an easy answer, but I haven't seem to of read of it yet. I am declaring a method in my JSP page that writes different results according to different actions. But I am getting an error that says:
"Undefined variable or class name: out"
Here is a piece of my code:

Any direction would be greatly appreciated.
Bill
[This message has been edited by bill bozeman (edited February 06, 2001).][/B]

The object "out" that writes into a JSP page's output stream is implicitly available in JSP page. However, within the servlet that the JSP gets compiled into, it is declared as a local variable within the service method of that servlet. So the scope of the implicit object "out" is within the service method, which means that it is not available in the methods specifically declared by you within <%! %> tags.
However, in order to use the "out" object explicitly in methods declared by you, you will have to include it in the signature of the method and when calling this method, pass the implicit object "out" as an argument.
Modify your method as
<%!
void displayImage( javax.servlet.jsp.JspWriter out,String nav )
{
out.print( "<img src='" + path + dir + nav + ext + "' name='" + nav + "' width='101' height='16' alt='" + alt + "' border='0'>" );
}
%>
Whenever this method needs to be called, pass "out" object( like : displayImage(out,"Bill"))
Hope this helps!
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Sowmya, that makes a lot of sense. I thought it had to do with me calling it inside the declarative tags <%! %> but I wasn't sure. Good idea on how to get around it too.
I'll give it a go.
Bill
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is an interesting problem. Honestly, I'm not sure why that doesn't work. I haven't had this problem because I usually don't declare methods in my JSP's.
I've only been working with Java and JSP for about 3 months but I hope I can give you some ideas.
When the JSP is compiled into a servlet by the container the servlet instantiates an object similar to the following:
JspWriter out = pageContext.getOut();
When you use out.print(), out.println(), etc you are calling this object.
For some reason this object isn't available inside the {} of your declaration.
Let me know how you resolve the problem.
Andrew
------------------
!_I_Know_Kung_Fu_!
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Things happens so fast here on the ranch
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Andrew, what Sowmya said worked great. Just thought I would let you know.
Bill
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I started my first post there was no other responses.
As soon as I saw the other response it made sense.
Your method can't see the JspWriter so you have to pass one in.
I love this place.
 
sandy gupta
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew U know about the scope of an Object Dun U ... This is all bout teh scope thing...In <%! %> tag, whatever u write comes before any of the service methods and is the declaration tag so if u want to access the out object here it is a good idea to create a out object of your own before that and then use it...............
Hope that helps
 
Rama Chandra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in JSP out is an implicit variable of type PrintWriter we can
use it directly without declaring it. This PrintWriter is derived from the response argument of the service method by
method response.getWriter().
Whatever code we write in JSP goes into the Service method of
complied servlet. So out is available in JSP without declaration.
But you are using the out variable in declared method. The declared method is placed outside the service method of the servlet. So the implicit variable is not available in the method, hence the error.

I hope this clarified the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic