Solved

Where does PrintWriter object come from ?

Posted on 2004-09-16
9
411 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
more than one jdk and one jre 1 50
Unhandled exception type Exception 18 42
What browser will run Java? 7 65
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

831 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