• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print data from void java method  RSS feed

 
Michael Lars
Greenhorn
Posts: 20
Java jQuery Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some void methods on some java class, that using print() can print info on console.

How can I print this info also on a servlet?

Thank you!
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the things you can do in your Java class, the same things you can also carry out in your servlet, unless I am misinterpreting your question. That means you can very well print to standard output stream.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are asking how to get info out to the browser, you'd do that through the response; not System.out.

Or are you asking how to do logging?
 
Michael Lars
Greenhorn
Posts: 20
Java jQuery Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm using response. I want to output info on the browser from a void java method that is not called direct, but is called from other java method.



Thank you!
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that method would need access to the response, or to its output stream. The usual way in Java to provide a method with access to an object is to pass said object as a parameter.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although passing container assets such as the response is generally frowned upon. Why can't the method return the info that needs to be emitted? Why is it a void method if it needs to return data?

Sounds like you may need to sit back and ponder your design a bit.
 
Michael Lars
Greenhorn
Posts: 20
Java jQuery Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: Why can't the method return the info that needs to be emitted? Why is it a void method if it needs to return data?

Sounds like you may need to sit back and ponder your design a bit.


Because it was a java project, converted to servlet, and this method was printing several information on console.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there is a reason that it cannot be refactored to better fit into the servlet environment? Otherwise, you're going to suffer from the square peg, round hole syndrome.

It would be best to have the method return data rather than try to emit output itself. But if there's a reason that it needs to emit its own output, it should be passed an OutputStream rather than the response itself.
 
Michael Lars
Greenhorn
Posts: 20
Java jQuery Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
It would be best to have the method return data rather than try to emit output itself. But if there's a reason that it needs to emit its own output, it should be passed an OutputStream rather than the response itself.


Here is a simply example, similar to my method! Is there any way I can store on some object and then pass it to the servlet?


Thanks a lot!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a very meaningful example, so it's impossible to say what would be best. But the constructs that used most to store data in web apps tend to be List, Maps and arrays; frequently storing beans with the data.

What works best greatly depends upon the nature of the data, and with the way that it needs to be displayed.
 
Michael Lars
Greenhorn
Posts: 20
Java jQuery Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It's not a very meaningful example, so it's impossible to say what would be best. But the constructs that used most to store data in web apps tend to be List, Maps and arrays; frequently storing beans with the data.

What works best greatly depends upon the nature of the data, and with the way that it needs to be displayed.


I have to displayed just chars data from for loops!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is this "chars data" coming from? What is its nature? How does it need to be display?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!