Servlet response write vs print? Which is better?

后端 未结 2 918
囚心锁ツ
囚心锁ツ 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.

提交回复
热议问题