17

I am new to Servlets.

Please, tell me about this line, and its use in the Jersey Framework + RESTful web services.

PrintWriter out = response.getWriter();
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
user3498842
  • 171
  • 1
  • 1
  • 6

7 Answers7

21

In servlets the output can be either character or byte. for character data (i.e text) u can use PrintWriter for others use ServletOutputStream

PrintWriter: prints text data to a character stream. 

getWriter :Returns a PrintWriter object that can send character text to the client.
Wahab
  • 520
  • 5
  • 24
6

in this case, the servlet binded with the url-pattern (previously set) is called.

The method being called depends on the kind of request (doGet, doPost, doPut).

The method normally receives the request and response objects, we then call the .getWriter() method for the response obj that gets us the stream on which we can write our output.

response.getWriter() returns a PrintWriter object that can send character text to the client.

Calling flush() on the PrintWriter commits the response.

Yassine S.
  • 1,061
  • 1
  • 13
  • 22
5

res.getWriter(); returns the object of PrintWriter Class, in which print(String args) method is declared to print anything on the browser's page as a response.

5

Simple just have a look again

Printwriter out = response.getWriter()

Now, here Printwriter is a class which simply converts the bytes into normal characters which we want to show as a response to the client's browser.So first you define out as a Printwriter object and through the GetWriter method we get the instance.

Aimery
  • 1,559
  • 1
  • 19
  • 24
Meezan Malek
  • 110
  • 1
  • 5
3

It's simple you see print writer is actually a class of servlet it gets response from app server to the real server using the get writer method and uses out as object

-2

PrintWriter=respose.getWriter(); use to print at server .getWriter() returns character text to the client. The java PrintWriter class belong java.io.PrintWriter. For intances writing int, long, and other primitive data formatted as text rather as their byte value.

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
-4

dnt be confused its simple as we had created object OUT of class PRINTWRITER nd simply returned as response by using GETWRITER METHOD.