1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Servlet : PrintWriter out = rsp.getWriter(); --what does this do??

Discussion in 'Java' started by sakthi.abdullah, May 8, 2007.

  1. sakthi.abdullah

    sakthi.abdullah New Member

    Joined:
    Mar 15, 2007
    Messages:
    29
    Likes Received:
    1
    Trophy Points:
    0
    This code is a part of simple HelloWorld Servlet file . Can anyone please explain the highlighted statement ....


    public void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    rsp.setContentType("text/html");
    PrintWriter out = rsp.getWriter();
    out.println("<html>");


    What does that statement do ??
     
    1 person likes this.
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    PrintWriter is an abstract class for writing to character streams. Methods implement are write(char[], int, int), flush(), and close().Print formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
     
  3. sakthi.abdullah

    sakthi.abdullah New Member

    Joined:
    Mar 15, 2007
    Messages:
    29
    Likes Received:
    1
    Trophy Points:
    0
    Thanks Pradeep.

    But Can you tell me how that downcasting works i.e calling a method from HttpResponse Object and assigning it to PrintWriter object
    PrintWriter out =rsp.getWriter();
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    The HttpServletResponse is an interface which can be used to write code to set an HTTP response header, set the content type of the response, acquire a text stream for the response, acquire a binary stream for the response, redirect an HTTP request to another URL, or add cookies to the response.
     
  5. sakthi.abdullah

    sakthi.abdullah New Member

    Joined:
    Mar 15, 2007
    Messages:
    29
    Likes Received:
    1
    Trophy Points:
    0
    Thanks again.. But please clarify a few things ..I m novice to both java and servlets!!!

    rsp.setContentType("text/html");
    PrintWriter out = rsp.getWriter();
    out.println("<html>");


    response object is used to set the header content type ,PrintWriter object to write to char streams ..But my question is "what happens in the mapping of PrintWriter out=rsp.getWriter()"
    Can you please give details a bit more?
     
  6. Prograamer_Yogi

    Prograamer_Yogi New Member

    Joined:
    Aug 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Because... getWriter() simply returns a "PrintWriter" object that can send character text to the client. so ofcourse u have PrintWriter's object as a return value which u r assigning to a 'out' which is ev1 a object of printWriter class :)
     
  7. cssd1983

    cssd1983 New Member

    Joined:
    Feb 29, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    One fairly simple implementation:

    PrintWriter getWriter() throws java.io.IOException {
    return new PrintWriter(socket.getOutputStream());
    }
     
  8. cssd1983

    cssd1983 New Member

    Joined:
    Feb 29, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    public java.io.PrintWriter getWriter()
    throws java.io.IOException
    Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.
    Calling flush() on the PrintWriter commits the response.

    Either this method or getOutputStream() may be called to write the body, not both.
     
  9. cssd1983

    cssd1983 New Member

    Joined:
    Feb 29, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Pradeep.Printwriter is a Public Class Not an Abstract Class. So Please Dun't post unless you are sure about your Answer.
     

Share This Page