Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Where does PrintWriter object come from ?

When programming Java servlets, the interface HttpServletResponse extends the interface ServletRespose which has the method getWriter().  This method returns a PrintWriter object.

A typical code would be :
import java.io.*
import javax.servlet.*
import javax.servlet.http.*

public class ServNm extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("Hello World") ; } }

When was the PrintWriter object created ? There was no 'new PrintWriter' statement written.
And since java.io.* has been imported, why could you not use the code :
PrintWriter out = new PrintWriter;
out.println("Hello World");  ?
0
parkhill04
Asked:
parkhill04
  • 4
  • 2
2 Solutions
 
sudhakar_koundinyaCommented:
HttpResponse object has it's own PrintWriter object  implementation. So to get that, we need to use response.getWriter()
0
 
sudhakar_koundinyaCommented:
component.addKeyListener(new MyKeyListener());
   
    public class MyKeyListener extends KeyAdapter {
        public void keyPressed(KeyEvent evt) {
            // Check for key characters.
            if (evt.getKeyChar() >= '0' && evt.getKeyChar() <='9' ) {
                process(evt.getKeyChar(),value);
            }
        }
    }

String strValue="";
public void process(char c, int code)
{
   if(code<=9)
   {
         if(strValue.length()==1) return;
         strValue=""+c;
   }
   else    if(code<=99)
   {
         if(strValue.length()==2) return;
         strValue=""+c;

   }
}


strValue holds integer as String

using Integer.parseInt(strValue) u can get Integer
0
 
sudhakar_koundinyaCommented:
sorry,

I posted the second comment accidentaly on ur link

Your answer is
 HttpResponse object has it's own PrintWriter object  implementation. So to get that, we need to use response.getWriter()

Now to ur other query

PrintWriter is a way to print content to destination device

So we need to bind destination device to PrintWriter object. it could be file or console or ur browser.

Response object has targeted destination device as browser.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
The PrintWriter instance is created by the servlet container for you.
0
 
parkhill04Author Commented:
"HttpResponse has its own PrintWriter object implementation." - The HttpResponse (HttpServletResponse ?) can implement a PrintWriter object, but when does the PrintWriter object become an object ?  

"The PrintWriter instance is created by the servlet container for you." -  When does this happen, and by what means ? I know the servlet container creates an HttpServletResponse object and passes it on to the service method, but how does the PrintWriter object get created ? I had never heard of the servelt container creating a PrintWriter object automatically.
0
 
objectsCommented:
>  When does this happen, and by what means ?

same way any object is created by calling a constructor.
probabl;y happens the first time getWriter() is called, may depend on the container though.
0
 
sudhakar_koundinyaCommented:
>>The HttpResponse (HttpServletResponse ?)
That is HttServletResponse only

it may be instantiated  at constructor level or may be in it's private methods or directly at first call of getWriter method.

I suggest you to look at the source code of HttpServletResponse. You get some idea regarding how the PrintWriter object is created and instantiated.

Regards
Koundinya
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now