• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of out.write() method

 
Pierre Peron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pierre Peron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pierre Peron:
And so I don't really know how to use JSTL.


Perfect time to learn!
 
Pierre Peron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic