Solved

Where does PrintWriter object come from ?

Posted on 2004-09-16
9
412 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 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