aspose file tools*
The moose likes JSP and the fly likes Use of out.write() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Use of out.write() method" Watch "Use of out.write() method" New topic
Author

Use of out.write() method

Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
I've implemented a little function in my jsp. This function is used to do some operations and then I'd like to print the results. The function is in <%! global declarations %> so I cannot use the following code <%= value %> to write a value in my page.
I tried to use the following code: out.write(value); but it creates an error when compiling.

Here is a simple part of my code:

<%! public void explore(List<Object> my_list) {
Iterator<Object> iter = my_list.iterator();
while(iter.hasNext()){
...
out.write(value);
...
}
%>

<html>
<head>
<title>test</title>
</head>
<body>
...
<%= explore(list1) %>
...
<%= explore(list2) %>
...
</body>
</html>

Can somebody see what's my error?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The JSP implicit variables (request, application, out, etc...) are only available in the service method sections of the JSP (not in <%! ...%> blocks).

If you want to print to the page from one of your methods, you'll have to pass a reference to the printWriter to the function. If this seems ugly, it is.
The standard approach, these days, is to conduct all of your business logic in your servlets and/or beans and use JSTL to iterate over the results in the JSP.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
Thanks a lot Ben!
It doesn't makes many time that I use JSP. And so I don't really know how to use JSTL.
Could you please tell me what is the better solution according to you? What do you mean when you say that I have to pass a reference to the printWriter to the function?

Regards
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Pierre Peron:
Thanks a lot Ben!
It doesn't makes many time that I use JSP. And so I don't really know how to use JSTL.
Could you please tell me what is the better solution according to you? What do you mean when you say that I have to pass a reference to the printWriter to the function?

Regards






I haven't tried it so it might need tweeking but that's the basic jist.

As far as what's better: I'm a staunch advocate of MVC and, as a practice, never put any business logic in my JSPs for anything but the most trivial apps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Originally posted by Pierre Peron:
And so I don't really know how to use JSTL.


Perfect time to learn!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
Of course when I have enougth spear time I'll learn JSTL.
But for the moment with the fifa world cup...

Thanks a lot for your help!!!
 
wood burning stoves
 
subject: Use of out.write() method