Solved

Where does PrintWriter object come from ?

Posted on 2004-09-16
9
413 Views
Last Modified: 2013-11-24
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
Comment
Question by:parkhill04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
9 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12079022
HttpResponse object has it's own PrintWriter object  implementation. So to get that, we need to use response.getWriter()
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12079128
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12079241
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 12080066
The PrintWriter instance is created by the servlet container for you.
0
 

Author Comment

by:parkhill04
ID: 12146848
"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
 
LVL 92

Accepted Solution

by:
objects earned 60 total points
ID: 12204776
>  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
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 60 total points
ID: 12206009
>>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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question