Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

servlet: writing to servlet stream that is closed

Thread view: 
Pawel - 12 Apr 2006 10:58 GMT
Hallo
I have the following Servlet's service code:

   public void service( HttpServletRequest req, HttpServletResponse res )
       throws ServletException, IOException {

       PrintStream out = new PrintStream( res.getOutputStream() );

       try {
         while(true) {
           out.println( "" );
           // then sleep for 10 sec
       }
       } catch ( IOException e ) {
           System.out.println( "problem interacting with servlet caller. Reason: " + e.getMessage() + ". Exiting..." );
           return;
       }
   }

When I invoke that servlet from my browser I can see new lines, but when
I close it, and servlet tries to write to `out' it does not throw an exception
, it just keeps writing (!?).
I wonder how to detect that the browser was closed after my servlet was woken up.
Do You know the answer ?

Greetings.
Filip Larsen - 12 Apr 2006 12:08 GMT
> When I invoke that servlet from my browser I can see new lines, but when
> I close it, and servlet tries to write to `out' it does not throw an exception
> , it just keeps writing (!?).
> I wonder how to detect that the browser was closed after my servlet was woken up.

I have a system where a servelet feeds data to a java client and here
the servlet (using Tomcat 4) gets a SocketException soon after the
client is terminated.

In a very few cases (like 1 out of 1000 disconnects and only in the
production environment) the servlet thread either hangs or terminates so
that the feed loop in the servlet is not terminated by an exception. To
handle this situation a central monitoring using timeouts is employed to
properly release resources for the servlet in question.

Also, the client never receive any exception if the server is terminated
so here the servlet sends ping on regular intervals if it has nothing
else to send to allow client to be able to detect disconnects. This may
of course not be a concern in your setup.

Regards,
Signature

Filip Larsen

William Brogden - 12 Apr 2006 14:44 GMT
> Hallo
> I have the following Servlet's service code:
[quoted text clipped - 24 lines]
> woken up.
> Do You know the answer ?

simple - a PrintStream never throws an exception, it swallows them all
- look at the JavaDocs
- perhaps you could use the checkError method.

while( !out.checkError() )

Bill


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.