wood burning stoves 2.0*
The moose likes Servlets and the fly likes How is the out.println() works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How is the out.println() works" Watch "How is the out.println() works" New topic
Author

How is the out.println() works

Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

I'm not getting ,how this out.println() works ,can someone explain ,just eager to know ,how it get's handled .

In my scenario i send data to servlet through request object , in servlet
|
| below (and i'm using out.println() to send the data) , i don't see the display of the data (the thing i have given in servlet) and it's moved to "result.jsp" without giving display to the servlet. !!



My question is

1) Is the response is sent when i use out.println() in the servlet , (if it was sent both request and response might have got de-referenced but i get "result.jsp" , i guess i don't know what's happening). Is "out.println() statement is working or not"
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

PrintWriter is a stream. Your calls to println() put messages on the stream to be output "later." They don't get sent to the browser right away. They get sent when you call flush() or close(). See this example. You use flush() when you want to write more in the same servlet (like in a download - you want to start downloading something while you get the rest of the data.) You use close() at the end.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

Also, you can't use out.println and forward to a JSP in the same example. It's one or the other as the JSP expects to start with a clean slate.
Arun Giridharan
Ranch Hand

Joined: Sep 30, 2010
Posts: 290

Got it!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How is the out.println() works
 
Similar Threads
getRequestDispatcher error in servlets
error for first web application : The requested resource (/Beer-v1/SelectBeer.do) is not available.
HF example Weird result - help please
Why the SnoopServlet in Tomcat 3.2.1 doesn't compile?
Request Resource not available JSP