Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Servlet : PrintWriter out = rsp.getWriter(); --what does this do?? (http://www.go4expert.com/forums/servlet-printwriter-rspgetwriter-t4225/)

sakthi.abdullah 8May2007 10:21

Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
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 ??

pradeep 14May2007 14:36

Re: Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
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.

sakthi.abdullah 15May2007 09:07

Re: Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
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();

pradeep 15May2007 09:54

Re: Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
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.

sakthi.abdullah 15May2007 12:03

Re: Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
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?

Prograamer_Yogi 3Aug2010 23:16

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

Originally Posted by sakthi.abdullah (Post 12419)
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?

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 :)

cssd1983 29Feb2012 14:13

Re: Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
One fairly simple implementation:

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

cssd1983 29Feb2012 14:14

Re: Servlet : PrintWriter out = rsp.getWriter(); --what does this do??
 
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.

cssd1983 1Mar2012 12:46

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

Originally Posted by pradeep (Post 12379)
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.

Pradeep.Printwriter is a Public Class Not an Abstract Class. So Please Dun't post unless you are sure about your Answer.


All times are GMT +5.5. The time now is 18:22.