Solved

Where does PrintWriter object come from ?

Posted on 2004-09-16
9
407 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
The PrintWriter instance is created by the servlet container for you.
0
 

Author Comment

by:parkhill04
Comment Utility
"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
Comment Utility
>  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
Comment Utility
>>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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now