• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of out.write() method

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic