Servlet response write vs print? Which is better?

后端 未结 2 919
囚心锁ツ
囚心锁ツ 2021-01-28 06:28

I am having a string which needs to be sent as response from a servlet & I am having two approaches to send response back from it.

First is using PrintWriter.

<
相关标签:
2条回答
  • 2021-01-28 06:34

    I am having a string specifies that you wish to write a string so i suggest the PrintWriter object

     //prints text data to screen (browser)
     PrintWriter printer = response.getWriter();
     printer.print(string);
     //print again if you wish
     printer.print("Welcome blahblah");
     //close afterwards if you don't mind.
     printer.close();
    

    so if you wish to write text to browser, PrintWriter works just fine, fast and simple.

    0 讨论(0)
  • 2021-01-28 06:42

    Use an OutputStream for binary data, and a Writer for text.

    0 讨论(0)
提交回复
热议问题