Forums Register Login

Use of out.write() method

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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



Perfect time to learn!
+Pie Number of slices to send: Send
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!!!
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1319 times.
Similar Threads
How to sort a list of lists
selection box problem
problem with fileUpload
Using lists in a jsp
Which is parsed first in a JSP page - EL, taglib or scriplets
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 05:37:05.